Spring Java Config 101
Join the DZone community and get the full member experience.
Join For FreeAfter 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 |
@Component | 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
- beans that are created by us (we have full control)
- external beans
User beans
@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
@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
And that's all - nothing strange ;)
Published at DZone with permission of Jakub Kubrynski, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Clear Details on Java Collection ‘Clear()’ API
-
A Data-Driven Approach to Application Modernization
-
Top 10 Engineering KPIs Technical Leaders Should Know
-
What Is JHipster?
Comments