Spring Java Config 101

After my last article there were some questions about how Java configuration work in details, and we can extend it to suite our needs. So I'll try to answer those question 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:

@Componentgeneric for any compoenents
@Repositorypersistence layer
@Serviceservice layer
@Controllerpresentation 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:
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 add includeFilters element:
@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:

ANNOTATIONmarked with a given annotation
ASSIGNABLE_TYPEassignable to a given type
ASPECTJAspectJ type pattern passed by pattern attribute
REGEXuses Pattern class with passed pattern attribute
CUSTOMcustom 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.
public class UserBeanB {

 private final UserBeanA userBeanA;

 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)

public class SpringConfig {

 public ExternalObjectA externalObjectA() {
  return new ExternalObjectA();

 public ExternalObjectB externalObjectB1() {
  return new ExternalObjectB(externalObjectA());

 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 ;)


Popular posts from this blog

Smart package structure to improve testability

Understanding Spring Web Initialization

Injecting Spring beans into non-managed objects