This is not the document you are looking for? Use the search form below to find more!

Report home > Others

Introduction to Spring’s Dependency Injection

0.00 (0 votes)
Document Description
Introduction to Spring’s Dependency Injection
File Details
Submitter
  • Name: tomas
Embed Code:

Add New Comment




Related Documents

Wieden + Kennedy New York Respond To PSFK’s Future Of Retail Report

by: shumayl, 7 pages

Wieden + Kennedy New York Respond To PSFK’s Future Of Retail Report

Introduction to Econometrics Stock 2rd Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Econometrics Stock 2rd Edition Solutions Manual

Introduction to Financial Accounting Horngren 9th Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Financial Accounting Horngren 9th Edition Solutions Manual

Introduction to Management Accounting Horngren 14th Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Management Accounting Horngren 14th Edition Solutions Manual

Introduction to Managerial Accounting Brewer 5th Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Managerial Accounting Brewer 5th Edition Solutions Manual

Introduction to Management Science Taylor 10th Edition Solutions Manual

by: gordonbarbier, 48 pages

Introduction to Management Science Taylor 10th Edition Solutions Manual

Financial Accounting An Introduction to Concepts Methods and Uses Stickney 13th Edition Solutions Manual

by: gordonbarbier, 48 pages

Financial Accounting An Introduction to Concepts Methods and Uses Stickney 13th Edition Solutions Manual

Introduction to Management Accounting Horngren 14th Edition Solutions Manual

by: georgesheslers, 48 pages

Introduction to Management Accounting Horngren 14th Edition Solutions Manual

Introduction to Managerial Accounting Brewer 5th Edition Solutions Manual

by: georgesheslers, 48 pages

Introduction to Managerial Accounting Brewer 5th Edition Solutions Manual

Introduction to Probability Models Ross 9th Edition Solutions Manual

by: georgesheslers, 48 pages

Introduction to Probability Models Ross 9th Edition Solutions Manual

Content Preview
Overview of Dependency Injection in SpringRichard PaulKiwiplan NZ Ltd27 Mar 2009What is Dependency InjectionDependency 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. Benefits of Dependency InjectionEnsures 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.Constructor vs Setter Injection// Constructorpublic 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; }}Constructor vs Setter InjectionBoth constructor and setter injection provide:Loose couplingSeparation of configuration and usageEnhanced 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/Dependency Injection in SpringThere are multiple ways of setting up the dependency configuration in spring.XMLAnnotationsJavaConfig... It is possible to mix and match definition styles. XMLGive 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>XML - Multiple FilesThe XML configuration can be split amoung multiple files. With each file containing configuration for different areas.applicationContext.xmlcontrollers.xmldao.xml...Annotations - ExampleYou 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 { // ... }Annotations - ListingThere are many available annotation for wiring: @Autowired@Required@Qualifier("xxx")@PostConstruct/@PreDestroy@Resource - JSR-250 @Component@Service@Respository@Controllerhttp://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config

Download
Introduction to Spring’s Dependency Injection

 

 

Your download will begin in a moment.
If it doesn't, click here to try again.

Share Introduction to Spring’s Dependency Injection to:

Insert your wordpress URL:

example:

http://myblog.wordpress.com/
or
http://myblog.com/

Share Introduction to Spring’s Dependency Injection as:

From:

To:

Share Introduction to Spring’s Dependency Injection.

Enter two words as shown below. If you cannot read the words, click the refresh icon.

loading

Share Introduction to Spring’s Dependency Injection as:

Copy html code above and paste to your web page.

loading