I'm a software developer for whom coding is a way of life as well as a hobby. Focused on slimming enterprise class systems, strongly interested in new technologies and continuously improving the software delivery process. My great passion are lightweight applications, in which the ratio of provided functionality to the code is as smallest as possible. Co-founder of the agile software house Codearte. Confitura conference organizer. Jakub is a DZone MVB and is not an employee of DZone and has posted 13 posts at DZone. You can read more from them at their website. View Full User Profile

Spring Java Config 101

01.24.2014
| 9008 views |
  • submit to reddit

After my last article there were some questions about how Java configuration work in details, and we can extend it to suit our needs. So I'll try to answer those questions in this post :)

Heart of Spring java configuration mechanism are @Configuration classes. That's the place where we can define all properties of our Spring context.

Assuming that we lean our application on annotations (which should be true if we want to use java config) we create beans using @Component annotation (with derivatives like @Repository,@Service and @Controller). Varying annotations apply for different layers:

@Component generic for any compoenents
@Repository persistence layer
@Service service layer
@Controller presentation layer

Component scanning

After annotating required classes we now want to add them into spring context. To achieve this we have to annotate our @Configuration class by @ComponentScan:

@Configuration
@ComponentScan("my.package.containing.beans")
public class SpringConfig {
}

What is worth considering in this case is a usage of string literals representing packages - it's easy to make a mistake which is hard to find, because even fancy IDE's like IntelliJ won't notice commited typo. Fortunately this annotation brings type-safe alternative which allows us use basePackageClasses parameter to specify for example marker interface lying in desired package.

By default @ComponentScan includes only mentioned earlier annotations but we can easily extend it to use any custom annotation, like for example @ConventionSucks :) It's just needed to addincludeFilters element:

@Configuration
@ComponentScan(basePackageClasses = BeansPackageMarker.class,
includeFilters = @ComponentScan.Filter(ConventionSucks.class))
public class SpringConfig {
}

@ComponentScan.Filter is very generic and it allows using various strategies by type (FilterType) parameter: 

ANNOTATION marked with a given annotation
ASSIGNABLE_TYPE assignable to a given type
ASPECTJ AspectJ type pattern passed by pattern attribute
REGEX uses Pattern class with passed pattern attribute
CUSTOM custom filter implementing TypeFilter


The same filters can be applied to excludeFilters attribute. We have one more attribute which handles filtering - useDefaultFilters turning on importing all objects annotated by @Component with derivatives)

Wiring beans

Now we know how to configure Spring to recognize our beans, but we still haven't covered topic about defining beans dependencies (beans wiring). We can distinguish two cases here:
  • beans that are created by us (we have full control)
  • external beans
User beans In the case when we're implementing classes we can use @Autowired annotation.
@Component
public class UserBeanB {
private final UserBeanA userBeanA;
@Autowired
public UserBeanB(UserBeanA userBeanA) {
this.userBeanA = userBeanA;
}
}

We can use @Autowired annotation on constructors, fields, methods and annotations. There are many discussions about the best way to inject dependencies, but we won't talk about that in this post.

Instead of Spring specific @Autowired annotation we can use @Inject introduced in JSR-330.

External beans It's especially important when we integrate some external frameworks or libraries (like for example SpringSecurity)
@Configuration
public class SpringConfig {
@Bean
public ExternalObjectA externalObjectA() {
return new ExternalObjectA();
}
@Bean
public ExternalObjectB externalObjectB1() {
return new ExternalObjectB(externalObjectA());
}
@Bean
public ExternalObjectB externalObjectB2() {
return new ExternalObjectB(externalObjectA());
}
}


Please notice that @Bean annotation on externalObjectA() method is very important even if you don't use this bean outside your configuration class. We you apply @Bean annotation Spring during context loading will discover it and will invoke this method only one (even if we use it many times in configuring our beans). Without this annotation the method will be treated as a normal java method. Also remember that method name will also be used as a bean name.

Joining different configurations

Quite often case while introducing Spring Java Config is when we want to extend working application (for example by introducing new module) or cleaning current configuration, by removing explicitly declared beans. Also in real life solutions we prefer having multiple configuration sources, not just whole big application in one giant class. In such situation what combining many configurations is becoming necessary. Spring brings two solutions: @Import annotation to import different configuration classes, and @ImportResource - to import configuration from XML file (you can use prefixes like classpath: or file:)

And that's all - nothing strange ;)
Published at DZone with permission of Jakub Kubrynski, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

raoul ghosh replied on Sun, 2014/01/26 - 9:13am

 Last row of first table should be @Controller ;)

Jakub Kubrynski replied on Wed, 2014/01/29 - 2:50am in response to: raoul ghosh

You'are really vigilant - thanks! Fixed :)

Thomas Whitmore replied on Wed, 2014/02/05 - 6:44pm

I prefer configuring the application in XML. Key configuration can be placed into one clear file, rather than hidden deep in the jumble of source-code.
This was the original principle that enabled code reusability, and made Spring great.

http://www.linkedin.com/in/thomaswhitmore 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.