Monday, June 15, 2009

Spring Dependency Injection Questions

This past week, we had our “Beta” run of Intertech’s new Complete Spring Core and Complete Spring Web classes. The latest Spring (2.5.6 with mentions of Spring 3) and Spring MVC/Web Flow are taught in these classes.

I had a great group of students to help give this new class its inaugural flight. While a little on the quiet side, they had wonderful questions and some great insight at the conclusion of our class for how to improve it. I thought I would share two questions that came out this week’s class.

Question #1 (thanks Sharon) - What happens when you use for dependency injecting a property but the property is a List in the actual class? For example, what if the bean definition looked something like this…

And the class looked like this…

public class Player
{
private List teams;
public void setTeams(List teams) {
this.teams = teams;
}
}

The implementation class of the collection bean property does not have to match or property definition in the Spring configuration. Both can be used interchangeably with any type of java.util.Collection. The element wires a collection to the bean and ensures that there are no duplicates. But, as in this case, the underlying collection is still a list. So in this example, the Player bean will have a List of team Strings, but the duplicate “Yankees” will be removed.

Question #2 (thanks Kurt) - Another great question came when we looked at the @Autowire annotation that was added in Spring 2.5. This annotation can be used on any method in the class to conduct dependency injection. The question was what if the @Autowire is used on multiple methods to dependency inject the same property? For example…

public class Player {
private String name;
private Team team;
@Autowired
public void initializePlayer(Team team){
this.team =
team;
}
@Autowired
public void initializePlayerAgain(Team team){
this.team = team;
}
}

In fact, both methods will get called and a Team will get passed to both methods. While this works, we could not think of a good reason for its use.If you have interest in exploring Spring, please contact our office (651-994-8558) and ask for Dan McCabe. I think our Spring training is the most complete (pun absolutely intended!).

Friday, June 5, 2009

Java SE 7 and Java EE 6

I just returned from JavaOne. Details on Java 7 and Java EE 6 were big topics during the conference.

Java 7 is expected to be released Feb 2010. Based on last year's conference notes and milestones released this year already, many thought (including myself) that Java 7 would be released sooner. The last feature-complete milestone is scheduled for Oct 2009. I attended a few sessions that discussed what will be in Java 7. Everyone was careful to say that the final decisions hadn't been made yet, indicating there is still work being done on Java 7. Some of the things likely to be in Java 7...
1. modules (allowing you to customize the features of Java you need for your app and finally killing the classpath)
2. null check operator/conditional - "?:"
3. Strings in switches
4. multiple exception catches in the catch block (using "")
5. diamond operator to allow the generics to be more easily used.
//For example...
HashMap> map = new HashMap>();
//becomes
HashMap> map = new HashMap<>();

Things not likely to make Java 7 but at one time rumored to have a chance...
1. closures
2. SQL expression checking

Java EE 6 will actually come out sooner. Java EE 6 is due on in final form in Sept 2009. In this release, the big new features are:
1. JAX-RS (support for RESTful web services)
2. JSF 2.0
3. Asynch servlets
4. Bean validation (adding validation to JavaBeans that can be used to validate property data anytime they are used).
5. Web.xml is gone (at least it can be gone) with the use of annotations and/or web.xml fragments.
6. Web beans - essentially session beans in the WAR file.

A few other notes from the conference...

Eclipse Galileo is the next train release of Eclipse (following the previous year releases of Calisto, Europa, and Ganymede) and it will come out on June 24th. This release now includes 33-48 Eclipse projects (Ganymede included 24 projects). On Jun 26, there will be a live Webinar to learn about new features. From the talk, I wouldn't say there is any real big thing in this release that most of us would use on a regular basis.

Spring has a new project called Spring Roo (yes for kangaroo). Essentially, this is Rails or Grails but all in Java and Spring. Meta programming is here to stay folks. I hope to have more on that in a later blog posting.