4/29/2012

How to use Spring Java Config

Filed under: — By Aviran Mordo @ 4:53 am

With Spring framework you can configure your beans with Java code instead of xml files, however mixing the two might cause unexpected results, if you don’t use Java config correctly.

One example we were facing when we used mixed configuration methods (xml and java) is that beans you reference in your Java config are not yet initialized thus you might get nulls in places you don’t expect to get them.

Here is an example how you should NOT use Java config:

Never user members annotated as resource and use them in your Bean definition. Spring will not guarantee that the bean will be injected on that time.


BAD example

class MyConfig {
@Resource
private MyResource myResource

@Bean
MyClientBridge myClientBridge(){
return new MyClientBridge(myResource);
}
}

In above code, myResource may be null.

So how can we use it properly?
You can still define beans similar in both xml files and Java Config however when you use them in Java Config you need to pass them as arguments to the bean definition. If the arguments are Spring beans then Spring will inject them with the relevant dependencies.

Here is the CORRECT usage:


Good Example

class MyConfig {

@Bean
MyClientBridge myClientBridge(MyResource myResource){
return new MyClientBridge(myResource);
}
}

Spring will now inject the myResource to the method and make sure it is initialized before it does that.

 

2 Responses to “How to use Spring Java Config”

  1. Chris Beams Says:

    The behavior you describe with @Resource-annotated fields in a @Configuration class should work the way you expect, i.e. should never be null by the time you reference the field within a @Bean method. If you can reproduce this situation in a standalone project, I’d appreciate it. You can submit that project by following the instructions at https://github.com/SpringSource/spring-framework-issues#readme

    Generally speaking, we advocate the use of @Autowired or @Inject instead of @Resource, particularly within @Configuration classes. The @Configuration Javadoc demonstrates this: http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

    Your workaround of using @Bean method parameter injection is valid, but limited. For example, if you have another @Bean dependent on myClientBridge(), the method will not be able to be called without providing a myResource object. So parameter injection should be used sparingly, and only for top-level or standalone beans that will not be depended upon by any other @Bean method.

  2. Johnathan Says:

    I posted a how to on the following site

    http://johnathanmarksmith.com/Spring/Java/JavaConfig/Programming/2013/04/25/how-to-use-spring-javaconfig-and-not-xml-files-for-configuation/

Leave a Reply

You must have Javascript enabled in order to submit comments.

All fields are optional (except comment).
Some comments may be held for moderation (depends on spam filter) and not show up immediately.
Links will automatically get rel="nofollow" attribute to deter spammers.

Powered by WordPress