J and I and Me
2010-07-07
  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: ,

  15:30
Bookmark and Share
Comments:
Just a small note on this. One might get the impression that this has worked for Spring 3.0 but now (3.0.x) it does not anymore. Actually what you show only worked with the standalone JavaConfig project *before* it was integrated into the 3.0 branch.

Regards,
Ollie
 
Correct, thanks for clarifying Oliver!
 
Declaring a bean as protected to, say, a class cope, is problematic.

There are many cases in which all-of-a-type beans are retrieved.

For example, we use to start all JMS listener containers and expose them to JMX after the container start.

Declaring a bean as "not visible" can be problematic in such situations. Of cause, this is then a programmers mistake.

Decoupling of modules is done via interfaces, why is that not enough?

Ron Piterman
 
Ron,
thanks for the feedback. I think the visibility should be limited concerning names, not in general. I.e. outside a module you should not be able to resolve the name. Still, the problem you are pointing needs to be thought about and I added it to the JIRA.

Concerning the decoupling using Interfaces: They are code artefacts. Spring configurations are a different type of artefact and interfaces are not used there - so I would argue that you need other modularization features there.
 
So you want to actually introduce a new feature with unclear semantics.

Spring allows to define if a bean is anautowire candidate - thats obviously not what you mean -

but what exactly does it mean - visibility concerning names?

When you query a bean factory for "beans of type", it returns a map of bean by names.

Should it be in the map or not?

If yes, then where is the visibility gone?

If not, well, the problem with that I already described in my previous comment.

LG, Ron Piterman
 
I just commented on SPR-7170 as linked to above by Eberhard. If folks are interested in this feature, please do vote, but also comment and explain your use case - how would this feature help you? How does not having it hurt you? Thanks.
 
Kommentar veröffentlichen

<< Home
J for Java | I for Internet, iMac, iPod and iPad | Me for me

ARCHIVES
Juni 2005 / Juli 2005 / August 2005 / September 2005 / Oktober 2005 / November 2005 / Dezember 2005 / Januar 2006 / Februar 2006 / März 2006 / April 2006 / Mai 2006 / Juni 2006 / Juli 2006 / August 2006 / September 2006 / Oktober 2006 / November 2006 / Dezember 2006 / Januar 2007 / Februar 2007 / März 2007 / April 2007 / Mai 2007 / Juni 2007 / Juli 2007 / August 2007 / September 2007 / Oktober 2007 / November 2007 / Dezember 2007 / Januar 2008 / April 2008 / Mai 2008 / Juni 2008 / August 2008 / September 2008 / November 2008 / Januar 2009 / Februar 2009 / März 2009 / April 2009 / Mai 2009 / Juni 2009 / Juli 2009 / August 2009 / September 2009 / Oktober 2009 / November 2009 / Dezember 2009 / Januar 2010 / Februar 2010 / März 2010 / April 2010 / Mai 2010 / Juli 2010 / August 2010 / Oktober 2010 / Januar 2011 / Februar 2011 / März 2011 / April 2011 / Mai 2011 / Juni 2011 / August 2011 / September 2011 / November 2011 / Februar 2012 / April 2012 / Mai 2012 / April 2013 / Mai 2013 / Juni 2013 / Januar 2015 / Juli 2015 / Februar 2016 /

Links

Twitter
Google +
Slideshare
Prezi
XING
LinkedIn
Das Spring Buch


Feeds

Feedburner


Impressum
Betreiber und Kontakt:
Eberhard Wolff
Leobschützer Strasse 22
13125 Berlin
E-Mail-Adresse: eberhard.wolff@gmail.com

Verantwortlich für journalistisch-redaktionelle Inhalte:
Eberhard Wolff