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.

Monday, 23 August 2010

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.

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.

WARNINGAs 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.

TIPIt 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.

Monday, 9 August 2010

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:

Thursday, 5 August 2010

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, seehttp://today.java.net/pub/a/today/2008/09/18/jsr-310-new-java-date-time-api.html for details

The book will be loaded with more in depth explanations and of course plenty of code samples. If you're interested in becoming a reviewer then please let me know!

Followers

Google+ Followers

Conferences

About Me

I'm Dutch born Kiwi who's the CEO (formerly CTO) of jClarity - a Java/JVM/Cloud performance tooling start-up in London. Outside of jClarity I herd Cats in various Java and open source communities and am constantly humbled by the vast seas of creativity and passion found there!

Currently I reside in London with my kick-boxing wife where I co-lead the LJC - London's Java User Group (a JCP EC member), run a couple of open source projects & try to find time for a pint at my local.

I love meeting interesting people in unusual places and believe it's important to be an engaging traveller as opposed to an annoying tourist.

You can also find me speaking regularly at conferences (Devoxx, JavaOne etc) on Java, open source and software development. I try not to shamelessly plug "The Well-Grounded Java Developer" (with Ben Evans) too much, but hey, it's our first book!