SSH Learning nine Dependency Injection

Source: Internet
Author: User

Dependency Injection (DI) and control inversion (IoC), meaning the same

In the traditional way, if Java instance A requires another instance B, then new B () is required, and then a is used, that is, the caller a creates an instance of the callee B

Dependency Injection mode: The work of creating callee B is no longer done by a, but is done by the spring container (or by the factory-mode factory) and then injected into the caller, thus becoming a dependency injection, because A and B are dependency relationships.

There are two ways of dependency injection:

(1) Set Value injection

Spring uses the set method to inject values into the members of Class A.

(2) Construction injection

Injected through the constructor.

Examples are as follows:

Code logic:

The service injects dog and cat through the constructor, which is construction injection, while the attribute-age of dog and cat is injected through the set method, which is the set value injection, which implements the following configuration file:

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://" xmlns: Xsi= "Http://" xmlns:p= "" xsi: schemalocation= "Http:// Spring-beans-3.1.xsd "><!--<bean id=" dog "class=" Com.ehr.service.Service "></bean>dog dog =new Dog ( ); --<bean id = "Dog" class= "Com.learn.DAO.impl.Dog" > <property name= "dage" value= "a" ></property> </bean> <bean id= ' cat ' class= ' com.learn.DAO.impl.Cat ' > <property name= ' cage ' value= ' ></ property> </bean> <bean id = "Service" class= "Com.ehr.service.Service" > <constructor-arg ref= "Dog" ></constructor-arg> <constructor-arg ref= "Cat" ></constructor-arg> </bean></beans >


</pre>package Com.ehr.service;import Org.springframework.context.applicationcontext;import;import Com.learn.dao.icat;import Com.learn.dao.idog;import;import;public class Service {<span style= "White-space:pre" ></span>idog dog;<span style= "White-space:pre" ></span>icat Cat;<span Style= "White-space:pre" ></span><span style= "White-space:pre" ></span>public Service (IDog dog, ICat cat) {<span style= "white-space:pre" ></span> = Dog;<span style= "White-space:pre" ></ Span> = Cat;<span style= "white-space:pre" ></span>}<span style= "White-space:pre" ></ Span><span style= "White-space:pre" ></span>public static void Main (string[] args) {<span style= " White-space:pre "></span>applicationcontext context = new Classpathxmlapplicationcontext (<span style=") White-spAce:pre "></span>        " Applicationcontext.xml ") <span style=" White-space:pre "> </span>service S1 = (service) Context.getbean ("service") <span style= "White-space:pre" ></span> (); <span style= "White-space:pre" ></span> (); <span style= "White-space:pre" ></span>}}</p><p>dog</p><p><pre name= "code" class= "Java" >package Com.learn.dao.impl;public class Dog implements Com.learn.DAO.IDog {int dage;public void setdage (int dage) {this.dage = dag e;} public void bark () {System.out.println ("Dog.bark ()" + "Wangwang" + Dage);}}


Package Com.learn.dao;public interface Idog {public void bark ();


Package Com.learn.dao.impl;import Com.learn.dao.icat;public class Cat implements ICat {int cage;public void setcage (int c Age) {this.cage = cage;} @Overridepublic void Bark () {System.out.println ("Cat.bark ()"  + Cage);}}


Package Com.learn.dao;public interface ICat {public void bark ();

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SSH Learning nine Dependency Injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.