Successfully reported this slideshow.
Your SlideShare is downloading. ×

Spring @configuration - So Long Spring XMLs

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 24 Ad

More Related Content

Similar to Spring @configuration - So Long Spring XMLs (20)

Advertisement

Recently uploaded (20)

Spring @configuration - So Long Spring XMLs

  1. 1. Make your life easier with Spring @Configuration @By Avi Etzioni
  2. 2. Some history…
  3. 3. 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
  4. 4. 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
  5. 5. September 2004  Java 5 is released  Introduces Metadata  Which is better known as annotations
  6. 6. 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
  7. 7. 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
  8. 8. 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)
  9. 9. How does it work?
  10. 10. Create a @Configuration class Instead of <beans> use @Configuration:
  11. 11. Add methods for generating beans What about the dependencies? Instead of <bean> use @Bean:
  12. 12. Dependencies – Option 1: Class Members restTemplate needs to be injected Name of bean to inject
  13. 13. Dependencies – Option 2: Method Variables
  14. 14. What about properties??
  15. 15. Use @Value for injecting propeties
  16. 16. Feature Flags
  17. 17. 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
  18. 18. 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
  19. 19. Importing Other @Configuration classes  We use @Import to import another @Configuration class:
  20. 20. 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
  21. 21. Import directly from the servlet web.xml Taken from ImageProvider Xml Style @Configuration Style
  22. 22. Don’t abuse  Code is much easier to abuse than XMLs  Keep the configuration – configuration  Avoid logic inside @Configuration (aside from feature flags)
  23. 23. 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
  24. 24. ImageProvider: Look mommy, no XMLs

×