<bean> @Autowired
                               and @Bean
                                                               ...
Prologue




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohib...
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
4
Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
5
Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
Imagine


                 •       A big pile of car parts
                 •       Workers running around uncontrollably
...
The Model T was the first
           automobile mass produced on
           assembly lines with completely
           inte...
Comparison criteria


                 •       Verbosity
                 •       Type safety
                 •       (Op...
Part I
                                                                                  <bean>




Copyright 2007 SpringS...
Spring Sucks!?


        • Spring == XML
        • XML == Evil
        • Evil == Sucks

        • Therefore, Spring == Suc...
CarPlant                                                      CarAssemblyLine




                                        ...
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
Benefits of XML configuration


                 • Easy for tooling to generate graphs
                 • Central location...
Drawbacks of XML
                                                                                                 configur...
Part II
                          @Autowired and <bean>




Copyright 2007 SpringSource. Copying, publishing or distributi...
@Component


          • Candidate for auto-detection

      @Component
      public class HibernateCarPartsInventory
    ...
@Autowired



          • Constructor
          • Field
          • Property

                  @Autowired
               ...
CarPlant                                                      CarAssemblyLine




                                        ...
Benefits of @Autowired-based
                                                                            approach

       ...
Drawbacks of @Autowired-
                                                                             based approach

    ...
Part III
                                            @Bean and <bean>




Copyright 2007 SpringSource. Copying, publishing...
@Configuration



          • On type-level
          • Identifies a class as a configuration class
          • @Bean method...
@ExternalBean



          • Method-level
          • Identifies a method returning an external bean


    public abstract ...
CarPlant                                                      CarAssemblyLine




                                        ...
Benefits of @Bean-based
                                                                                     approach

   ...
Drawbacks of @Bean-based
                                                                               approach

        ...
Conclusion




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is proh...
Conclusion


                 • There’s something for everyone in Spring
                 • Type safe and separate configu...
Conclusion


                 • All three approaches build on Spring’s
                   proven and solid foundation
    ...
Upcoming SlideShare
Loading in...5
×

BeJUG - Di With Spring

1,653

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,653
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
65
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "BeJUG - Di With Spring"

  1. 1. <bean> @Autowired and @Bean Alef Arendsen Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  2. 2. Prologue Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  3. 3. Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  4. 4. 4 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  5. 5. 5 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  6. 6. Imagine • A big pile of car parts • Workers running around uncontrollably • The parts don’t connect at all • Creating cars is all done by hand • That’s what manufacturing was like before Henry Ford introduced the assembly line! 6 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  7. 7. The Model T was the first automobile mass produced on assembly lines with completely interchangeable parts... By 1914, the assembly process for the Model T had been so streamlined it took only 93 minutes to assemble a car. Source: Wikipedia 7 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  8. 8. Comparison criteria • Verbosity • Type safety • (Opportunity for) tool support • Solution for dealing with ambiguity 8 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  9. 9. Part I <bean> Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  10. 10. Spring Sucks!? • Spring == XML • XML == Evil • Evil == Sucks • Therefore, Spring == Sucks? Shamelessly stolen from Craig Walls 10 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  11. 11. CarPlant CarAssemblyLine Part CarPartInventory Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  12. 12. Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  13. 13. Benefits of XML configuration • Easy for tooling to generate graphs • Central location for all config data • Configuration separate from Java code only option for code you don’t control • Easy solution for ambiguity 13 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  14. 14. Drawbacks of XML configuration • Perceived XML hell (partially true) • Lack of type safety (at compile time) – Tooling helps us a bit here • Less refactoring friendly • Names needed to solve ambiguity 14 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  15. 15. Part II @Autowired and <bean> Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  16. 16. @Component • Candidate for auto-detection @Component public class HibernateCarPartsInventory implements CarPartsInventory { private SessionFactory sessionFactory; ... } <context:component-scan base-package=quot;com.carplantquot;/> 16 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  17. 17. @Autowired • Constructor • Field • Property @Autowired public HibernateCarPartsInventory( SessionFactory factory) { this.sessionFactory = factory; } 17 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  18. 18. CarPlant CarAssemblyLine Part CarPartInventory Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  19. 19. Benefits of @Autowired-based approach • ‘Config’ code in the Java code • More type safe experience • Elegant annotation-based solution for solving ambiguity (requires XML) • Less verbose 19 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  20. 20. Drawbacks of @Autowired- based approach • ‘Config’ code in the Java code • Extra (sometimes complex) measures needed for solving ambiguity 20 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  21. 21. Part III @Bean and <bean> Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  22. 22. @Configuration • On type-level • Identifies a class as a configuration class • @Bean methods represent beans @Configuration public class MyConfig { public @Bean Service service() { return new Service(); } } 22 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  23. 23. @ExternalBean • Method-level • Identifies a method returning an external bean public abstract @ExternalBean DataSource dataSource(); 23 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  24. 24. CarPlant CarAssemblyLine Part CarPartInventory Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  25. 25. Benefits of @Bean-based approach • ‘Config’ code completely separate from Java code • Entirely type safe approach • Easy solution for ambiguity problem • Allows for context inheritance • Allows for 100% of all Java constructs 25 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  26. 26. Drawbacks of @Bean-based approach • Harder to make it work in tooling • Requires a little bit more code 26 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  27. 27. Conclusion Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  28. 28. Conclusion • There’s something for everyone in Spring • Type safe and separate configuration – JavaConfig (@Bean) • Type safe and config in Java code – @Autowired / @Component • For external code and XML fans – <bean/> • For specification-minded people – EJB 3 • For Google fans – Guice ;-) 28 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  29. 29. Conclusion • All three approaches build on Spring’s proven and solid foundation – Just mix and match all approaches – A moving model, not a fixed static snapshot of the current state of union • Plus all the other benefits – Easy JMX exporting – Ease AOP configuration 29 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×