Enterprise Systems Modeling EGN 5620 Enterprise Systems Configuration Spring, 2012.
Spring @configuration - So Long Spring XMLs
-
Upload
avi-etzioni -
Category
Software
-
view
267 -
download
0
Transcript of Spring @configuration - So Long Spring XMLs
October 2002
Rod Johnson releases Spring, a new framework for easy
implementation of IoC
Configuration is done in XML files
Allows complete separation between code and wiring
Not everyone liked it
XMLs are not readable and very verbose
They lack things like:
Type safety
auto-completion
Smart navigation
Debugging
Xdoclet – a tool that (amongst other things) generated
XMLs from java doc comments
November 2007
Spring 2.5 is released
Introduces annotation-based configuration:
Full-featured annotation-driven DI
Auto-detection of application components (in the classpath) and auto-configuring them as Spring managed objects.
Simplified
1. Annotate your beans with suitable annotation
2. Annotate the injection points with @Autowired or @Resource
3. Let the magic happen
But annotations had their
problems too
No central point of wiring
Harder (though possible) to replace entire
configuration
With XMLs you could just load another main XML context
file
Obscures the injected class
December 2009
Spring 3.0 is released
Introduces @Configuration classes:
Like XMLs – one point for declaring configuration
But better– Have all the capabilities of code
You can now:
Debug the context startup
Perform conditionals (Did someone say feature-flags??)
And use IDE’s capabilities (even without the Ultimate
version)
It’s important to understand
Spring doesn’t care about the method of the beans
declaration:
XML bean == Annotation bean == @Configuration bean
You can mix the whole 3 ways of declaring beans
THIS MAKES IT VERY EASY TO MIGRATE FROM XML TO
@Configuration!!!!
Just migrate one XML at a time
Importing XMLs from
@Configuration
In XML we use <import resource=“…”/>
In @Configuration classes we use @ImportResource
Just copy the classpath from the original <import> tag
Importing @Configuration from XML
Declare <context:annotation-config/> tag in any of your
XMLs
Declare a bean with the class type of the
@Configuration class
Don’t abuse
Code is much easier to abuse than XMLs
Keep the configuration – configuration
Avoid logic inside @Configuration (aside from feature
flags)
Conventions
Always put under a package “context”
Use suffix *ApplicationContext.java (to ease the search)
Don’t overload the @Configuration with code – split
them as if they were XMLs