Spring Dependency Injection

Source: Internet
Author: User

The English name of dependency injection is dependency injection. Similar to the IOC, it is just a two-point description of the same concept. For a chestnut, an object a needs to use object B to accomplish a function, and it can be said that a dependent b,spring will automatically inject the B object into the A object when creating a. So, the role of dependency injection is to dynamically inject the dependent objects into the bean component when creating objects using the Spring framework.


When it comes to dependency injection, there are 3 implementations, namely setter method injection, construction method injection and interface injection.


Property Setter Injection: The IOC container uses the setter method to inject the dependent object. After you instantiate BEA by calling the parameterless constructor or the parameterless static factory method, invoke the Bean setter method to implement a setter-based dependency injection.

Construct method Injection: Use the constructor method to inject the dependent instance.

Interface injection: The spring container does not support interface injection.


Today's game is setter injection. The DAO layer is invoked through the servi layer to simulate dependency injection.

public class Bookdao {public void Save () {System.out.println ("add Book");}}

public class Bookservice {private Bookdao bookdao;//objects that are dependent on public void Setbookdao (Bookdao Bookdao)//for setter injection { This.bookdao = Bookdao;} public void Addbook () {Bookdao.save ();}}

The location of the configuration Xml,xml can be placed in the directory where the Bean resides I

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"       Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"       xsi:schemalocation= "http://www.springframework.org/ Schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd "><bean id=" Bookdaoid "class= "Com.canyugan.di.BookDao"/><bean id= "Bookserviceid" class= "Com.canyugan.di.BookService" ><!--<  Property> the dependency injection process, the underlying will execute the Setter method * Name Object properties, Setbookdao, Bookdao, bookdao* ref other object instance reference--<!--has a --><property name= "Bookdao" ref= "Bookdaoid" ></property></bean></beans>

Test our Setter Injection


@Testpublic  void Demo1 () {//Load configuration file ApplicationContext applicationcontext=new classpathxmlapplicationcontext (" Com/canyugan/di/beans.xml ");//Get Object Bookservice Bookservice=applicationcontext.getbean (" Bookserviceid ") from Spring's factory , Bookservice.class); Bookservice.addbook ();}


Can see that a successful addition of a book.


Enjoy the spring setter injection.

Spring 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: info-contact@alibabacloud.com 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.