Limiting the Visibility of Spring Beans
Lately I have been working on concepts for the configuration of large Spring applications. The basic concept to build large system is well established in computer science: Break the system down in smaller blocks and build each of the blocks. Have the blocks talk to each other only in defined ways to decouple. This means that the blocks can easily be changed internally as long as the external properties stay the same. Transferred to Spring this means that the Spring configuration of the system must be broken down into parts. This is very well possible and actually already a best practice.
However, there is one problem: The Spring configuration has only a global visibility. I.e. every Spring bean can be used anywhere in the configuration. There are some ways to tackle this problem - that is beyond the scope of this posting.
There used to be a very elegant solution to this in the Spring Java Configuration introduced in Spring 3.0:
@Configuration
public class JavaConfiguration {
@Bean(destroyMethod = "close")
protected DataSource dataSource() {...}
@Bean
public EntityManagerFactory entityManagerFactory() {...}
@Bean
protected JpaVendorAdapter jpaVendorAdapter() {...}
}
This configuration creates Spring beans called dataSource, entityManagerFactory and jpaVendoreAdapter by calling the respective methods. Now the trick is: If you use the
protected keyword the beans are not visible outside this class i.e. in particular they are not visible in an XML configuration that uses this class or in other Spring configuration classes. At least that is how it used to be - this feature is gone now. If you agree with me that this is an important feature - please vote for
http://jira.springframework.org/browse/SPR-7170 to bring it back!
Labels: Spring in the Large, Spring JavaConfig
J for Java |
I for Internet, iMac, iPod and iPad |
Me for me