Introduction to Spring's Dependency Injection

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

Favorites, Groups & Events

Introduction to Spring's Dependency Injection - Presentation Transcript

  1. Overview of Dependency Injection in Spring Richard Paul Kiwiplan NZ Ltd 27 Mar 2009
  2. What is Dependency Injection Dependency Injection is a form of Inversion of Control. Also known as the Hollywood principle, \"Don't call us, we'll call you!\" MyService is given instances of ItemDAO and LabelDAO. MyService isn't responsible for looking up DAOs.
  3. Benefits of Dependency Injection Ensures configuration and usages of services are separate. Can switch implementations by simply changing the configuration. Enhances testability as mock dependencies can be injected Dependencies are easily identified No need to read the source code to see what dependencies your code talks to.
  4. Constructor vs Setter Injection // Constructor public class MyService implement Service { private ItemDAO itemDAO; private LabelDAO labelDAO; public MyService(ItemDAO itemDAO, LabelDAO labelDAO) { this.itemDAO = itemDAO; this.labelDAO = labelDAO; } } // Setter public class MyService implement Service { private ItemDAO itemDAO; private LabelDAO labelDAO; public setItemDAO(ItemDAO itemDAO) { this.itemDAO = itemDAO; } public setLabelDAO(LabelDAO labelDAO) { this.labelDAO = labelDAO; } }
  5. Constructor vs Setter Injection Both constructor and setter injection provide: Loose coupling Separation of configuration and usage Enhanced testability Constructor injection has a slight advantage (IMO) as it is upfront about what collaborators it requires. More details at: http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/
  6. Dependency Injection in Spring There are multiple ways of setting up the dependency configuration in spring. XML Annotations JavaConfig ... It is possible to mix and match definition styles.
  7. XML Give myService access to labelDAO . <bean id=\"labelDAO\" class=\"com.example.dao.MyLabelDAO\"/> <!-- Constructor Injection --> <bean id=\"myService1\" class=\"com.example.service.MyServiceImpl\"> <constructor-arg ref=\"labelDAO\"/> </bean> <!-- Setter Injection --> <bean id=\"myService2\" class=\"com.example.service.MyServiceImpl\"> <property name=\"labelDAO\" ref=\"labelDAO\"/> </bean>
  8. XML - Multiple Files The XML configuration can be split amoung multiple files. With each file containing configuration for different areas. applicationContext.xml controllers.xml dao.xml ...
  9. Annotations - Example You can by-pass some of the XML configuration by using Spring's annotation support. @Service public class MyServiceImpl implement MyService { private ItemDAO itemDAO; private LabelDAO labelDAO; @Autowired public MyService(ItemDAO itemDAO, LabelDAO labelDAO) { this.itemDAO = itemDAO; this.labelDAO = labelDAO; } } @Repository public class MyItemDAO implements ItemDAO { // ... }
  10. Annotations - Listing There are many available annotation for wiring: @Autowired @Required @Qualifier(\"xxx\") @PostConstruct/@PreDestroy @Resource - JSR-250 @Component @Service @Respository @Controller http://static.springframework.org/spring/docs/2.5.x/reference/beans. html#beans-annotation-config
  11. Java Config Dependency Injection with configuration done using Java. Uses annotations and replaces the XML config with Java code. @Configuration public class AppConfig { @Bean public Foo foo() { return new Foo(bar()); } @Bean public Bar bar() { return new Bar(); } }
  12. Alternatives The main alternative to Spring's Dependency Injection is Guice. More info available at: http://www.infoq.com/news/2007/03/guice-javaconfig Questions?

+ Richard PaulRichard Paul, 7 months ago

custom

2058 views, 0 favs, 3 embeds more stats

Introductory slides for Spring's Dependency Injecti more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 2058
    • 1956 on SlideShare
    • 102 from embeds
  • Comments 1
  • Favorites 0
  • Downloads 69
Most viewed embeds
  • 93 views on http://www.rapaul.com
  • 7 views on http://maven
  • 2 views on http://devblog.kiwiplan.co.nz

more

All embeds
  • 93 views on http://www.rapaul.com
  • 7 views on http://maven
  • 2 views on http://devblog.kiwiplan.co.nz

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories