Hi all,
With "The Well-Grounded Java Developer" deadlines approaching and my arms starting to feel the strain I'll be taking a break from writing any blog posts for a few months. Not that it's been real busy here anyhow, but I thought you should know :-).
Cheers,
Martijn
I'm the CEO (was CTO) of jClarity (www.jclarity.com) - a new Java/JVM performance analysis company! You'll find a vast majority of my recent blog posts there (http://www.jclarity.com/blog). This is my infrequent personal blog with mad musings on travelling, IT (Java and Open Source in particular) and anything else that wanders its way into my head.
Showing posts with label book. Show all posts
Showing posts with label book. Show all posts
Wednesday, 2 February 2011
Monday, 23 August 2010
Dependency Injection in Java 7
Hi all,
So apart from recovering from a long bout of the stomach flu, enjoying good food again at a "Come dine with me" night (our parents would be so proud) and planning a rush trip to Vegas I've managed to get out a first draft of the Dependency Injection chapter for the Well Grounded Java 7 Developer.
Java 7 unifies some of the basic standards that the various Dependency Injection frameworks (Spring, Guice, PicoContainer etc) have, making it easier for developers to move between the frameworks as needed.
A reminder of what DI is with regards to Java[1]
So without further preamble, here's a short section from the chapter, let me know what you think!
So apart from recovering from a long bout of the stomach flu, enjoying good food again at a "Come dine with me" night (our parents would be so proud) and planning a rush trip to Vegas I've managed to get out a first draft of the Dependency Injection chapter for the Well Grounded Java 7 Developer.
Java 7 unifies some of the basic standards that the various Dependency Injection frameworks (Spring, Guice, PicoContainer etc) have, making it easier for developers to move between the frameworks as needed.
A reminder of what DI is with regards to Java[1]
The javax.inject package specifies a means for obtaining objects in such a way as to maximize re-usability, testability and maintainability compared to traditional approaches such as constructors, factories, and service locators (e.g., JNDI). This process, known as Dependency Injection, is beneficial to most nontrivial applications.
So without further preamble, here's a short section from the chapter, let me know what you think!
Inject annotation
The @Inject annotation interface can be used in three places to indicate where you'd like a dependency to be injected. Below are the types of members that can be injected, in the order that they are processed at runtime:
1. Constructors
2. Methods
3. Fields
You can annotate a constructor with @Inject and expect its parameters to be provided at runtime by your configured IoC container e.g:
@Inject public MurmurMessage(Header header, Content content)
{
this.header = header;
this.content = content;
}
In this case both the Header and Content parameters would be injected at runtime. The specification allows for 0+ parameters to be injected for constructors, so injecting a zero-parameter constructor is still valid.
WARNING As per the specification there can only be one constructor in a class with an @Inject annotation, this makes sense as the JRE would not be able to decide which injected constructor took precedence.
You can annotate a method with @Inject and like a constructor, expect its 0+ number of parameters to be injected at runtime. There are some restrictions in that injected methods cannot be declared abstract and cannot declare type parameters of their own[1]. The short code sample below demonstrates the use of @Inject with a setter method, a common technique when using Dependency Injection.
@Inject public void setContent(Content content)
{
this.content = content;
}
This technique of method parameter injection is especially powerful when it comes to providing service methods with the resources they need to do their job. For example you could pass a DAO argument to a finder service method that was tasked to retrieve some data.
TIP It has become a default best-practice to use constructor injection for setting mandatory dependencies for a class and to used setter injection for non-mandatory dependencies, e.g. fields that already have sensible defaults.
It is also possible to inject fields (as long as they are not final), however the practice is not common. The syntax again is quite simple.
public class MurmurMessenger
{
@Inject private MurmurMessage murmurMessage;
...
}
You can read further about the @Inject annotation in the Javadoc, where you can discover some nuances about what types of values can be injected and how circular dependencies are dealt with.
Cheers,
Monday, 9 August 2010
A Little Directory love
Hi all,
As you can see, with the new Java 7 APIs this particular task is much simpler than it used to be and this theme is prevalent in the rest of the NIO.2 APIs.
So my next little snippet also comes from Chapter 2 of the book where we deal with the new NIO.2 (Non-Blocking I/O 2) APIs being introduced to Java 7. In particular I'm showing a small code snippet to showcase some of the new support for directories that Java 7 has. The code sample certainly needs a little tidy-up by using the new Automatic Resource Management (ARM) features in Java 7, but for now the example will suffice.
Dealing with Directories
The java.nio.file.DirectoryStream interface and its implementing classes allow you to:
Dealing with Directories
The java.nio.file.DirectoryStream interface and its implementing classes allow you to:
- Iterate over entries in a directory.
- Deal with large directory structures.
- Filter entries whilst processing using regular expressions and MIME based content detection.
- Walk the tree structure in order to perform recursive move, copy, delete operations.
DirectoryStream stream = null;
try
{
{
// Define the directory
Path dir = Paths.get("c:\\workspace");
Path dir = Paths.get("c:\\workspace");
// Declare a filtering stream
stream = dir.newDirectoryStream("*.java");
stream = dir.newDirectoryStream("*.java");
// List each .java file in that directory
for (Path entry: stream)
{
System.out.println(entry.getName());
}
}
catch (IOException e)
{
{
System.out.println(entry.getName());
}
}
catch (IOException e)
{
// Poor man's exception handling
e.printStackTrace();
}
e.printStackTrace();
}
As you can see, with the new Java 7 APIs this particular task is much simpler than it used to be and this theme is prevalent in the rest of the NIO.2 APIs.
There are several other use cases which we will show case in the "NIO.2 in action" section.as well as on our upcoming website (to be revealed later).
So are we there yet? In terms of File and Directory interaction in Java 7 there's certainly a massive improvement, so I'll say "Yes" once Java 7 is released.
Cheers,
So are we there yet? In terms of File and Directory interaction in Java 7 there's certainly a massive improvement, so I'll say "Yes" once Java 7 is released.
Cheers,
Martijn
Thursday, 5 August 2010
The Well Grounded Java 7 Developer
Hi all,
I've obviously not posted for quite some time and here is the reason why.....
It's been a long time coming, but I'm very happy to announce that Ben Evans and I have been contracted to write "The Well Grounded Java 7 Developer" for Manning publications. We've been spending the last month frantically getting the first few chapters out and a whole host of other book related activities, but now that I'm in full chapter writing mode I'll be adding regular posts to this blog.
A majority of my future posts will focus on interesting areas in this book and firstly I'd like to show a reworked extract from the new Date and Time API section.
Modeling Date and Time
The new API models time as a sequence of consecutive instants separated by fixed durations[1]. Java 7 maps these concepts directly on to classes, here's a more detailed explanation:
- The javax.time.Instant class represents a specific point on a discrete time-line e.g. January 23rd, 1996 at 09:00:00,0 UTC, the day that Java 1.0 was released.
- The javax.time.Duration class represents a section of elapsed time in nanoseconds, e.g. The 400 nanoseconds it takes for a PIC12C672-04 Microchip to execute an instruction set.
So as you can imagine, any point of time can be modeled by simply applying a Duration (positive or negative) to a starting Instant.
...
...
Calendering
With regards to calendaring, the main Calendar is ISOChronology, but other calendars such as CopticChronology and ThaiBuddhistChronology are supported. The Chronology interface can be extended from to provide other implementations.
...
...
Timezone support
Timezone support is also provided via three core classes:
- LocalDateTime – Represents date/time without an offset or a time zone.
- OffsetDateTime - Represents date/time with an offset but not time zone.
- ZonedDateTime - Represents date/time with an offset and a time zone.
...
...
[1] This phrase is almost 100% uplifted from an excellent early primer article by Jesse Farnham, see http://today.java.net/pub/a/today/2008/09/18/jsr-310-new-java-date-time-api.html for details
Wednesday, 19 May 2010
Exciting projects on the go!
Hi all,
This is another mainly Java/Technology related post, although it's more of an overview of what I'm up to. This is as much to do with getting it sorted in my head as anything else but people have asked what I'm involved in, so here goes:
1.) Reviewing an upcoming book on the Hudson Build Server (a favourite build tool of mine). I'm definitely behind on helping out on this one, I really need a few solid hours of quiet time to review the material produced to date.
2.) Reviewing an upcoming book on Java 6 certification. I'm trying to learn as much about the book review process as possible before hopefully co-authoring my own, so that's my main motivation for this one. I'm still on the fence about certification, I dislike most of it but if it encourages people to study then that's not all bad.
3.) Negotiating (along with a colleague) a book deal with a big tech book publisher, we're getting closer to some sort of a deal I think! This is the big exciting one for me, I've always wanted to try an author part of book if not an entire one so I really hope we can make this happen.
4.) Moderating on the Javaranch, which is going through a bit of an SEO overhaul at the moment. I'm also hopefully getting my wife ("a most excellent Graphic Designer" and that's definitely not just according to me) to help give it a facelift.
5.) Community leading two open source projects (PCGen and Ikasan EIP). These I wish I could spend a few extra regular hours on a week. PCGen was the project that lead me into Open Source in the first place and the community spirit is just freakin awesome. No matter how many times I take sabbaticals away from it, I always miss it far too much and just have to go back :).
Ikasan is the new kid on the block that I'm trying to turn into a "successful open source project" as part of my day job. It's got a great deal of potential, but I need to spend a good deal more time with the community in order to realise it.
6.) Co-organising the London Java Community and the London Graduate and Undergraduate Development Community which currently involves helping organise an Unconference for the LJC and an not yet announced talk for the GDC.
7.) Writing several talks for conferences (sssh it's a secret)
With several of items above, I work with some pretty amazing and motivated people and that's why I pretty much stay involved, it's just plain fun and rewarding to boot!
So there you have it, my tech related life in a nutshell :).
Cheers,
Martijn
As an aside, I spotted the statement "If you want something done, give it to a busy person" on the web a couple of days ago while I was lying in bed nursing a bout of the man flu (which my wife was ever so patient with). I realised that it holds pretty darn true for all of psychotically busy friends and colleagues that I know, they always seem the most organised and on to it.
Labels:
book,
Conference,
GDC,
Java,
JUG,
LJC,
Unconference
Subscribe to:
Posts (Atom)