SlideShare una empresa de Scribd logo
1 de 29
Descargar para leer sin conexión
<bean> @Autowired
                               and @Bean
                                                                              Alef Arendsen




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
Prologue




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
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 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.
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.
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.
Part I
                                                                                  <bean>




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
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.
CarPlant                                                      CarAssemblyLine




                                                                                                                          Part
                                      CarPartInventory




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
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 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.
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.
Part II
                          @Autowired and <bean>




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
@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.
@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.
CarPlant                                                      CarAssemblyLine




                                                                                                                          Part
                                      CarPartInventory




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
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.
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.
Part III
                                            @Bean and <bean>




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
@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.
@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.
CarPlant                                                      CarAssemblyLine




                                                                                                                          Part
                                      CarPartInventory




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
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.
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.
Conclusion




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
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.
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.

Más contenido relacionado

Más de Stephan Janssen

Más de Stephan Janssen (15)

The new Voxxed websites with JHipster, Angular and GitLab
The new Voxxed websites  with JHipster, Angular and GitLabThe new Voxxed websites  with JHipster, Angular and GitLab
The new Voxxed websites with JHipster, Angular and GitLab
 
Java, what's next?
Java, what's next?Java, what's next?
Java, what's next?
 
Programming 4 kids
Programming 4 kidsProgramming 4 kids
Programming 4 kids
 
Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5Devoxx2011 timesheet day3-4-5
Devoxx2011 timesheet day3-4-5
 
Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2Devoxx2011 timesheet day1-2
Devoxx2011 timesheet day1-2
 
EJB 3.1 by Bert Ertman
EJB 3.1 by Bert ErtmanEJB 3.1 by Bert Ertman
EJB 3.1 by Bert Ertman
 
BeJUG JAX-RS Event
BeJUG JAX-RS EventBeJUG JAX-RS Event
BeJUG JAX-RS Event
 
Whats New In Java Ee 6
Whats New In Java Ee 6Whats New In Java Ee 6
Whats New In Java Ee 6
 
Glass Fishv3 March2010
Glass Fishv3 March2010Glass Fishv3 March2010
Glass Fishv3 March2010
 
Advanced Scrum
Advanced ScrumAdvanced Scrum
Advanced Scrum
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
 
Intro To OSGi
Intro To OSGiIntro To OSGi
Intro To OSGi
 
Kick Start Jpa
Kick Start JpaKick Start Jpa
Kick Start Jpa
 
BeJUG - Spring 3 talk
BeJUG - Spring 3 talkBeJUG - Spring 3 talk
BeJUG - Spring 3 talk
 
BeJug.Org Java Generics
BeJug.Org   Java GenericsBeJug.Org   Java Generics
BeJug.Org Java Generics
 

Último

(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Último (20)

(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 

BeJUG - Di With Spring

  • 1. <bean> @Autowired and @Bean Alef Arendsen Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 2. Prologue Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 3. Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 4. 4 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 5. 5 Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 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. 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. 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. Part I <bean> Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 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. CarPlant CarAssemblyLine Part CarPartInventory Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 12. Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 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. 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. Part II @Autowired and <bean> Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 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. @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. CarPlant CarAssemblyLine Part CarPartInventory Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 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. 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. Part III @Bean and <bean> Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 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. @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. CarPlant CarAssemblyLine Part CarPartInventory Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 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. 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. Conclusion Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 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. 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.