Spring Feature--di

Source: Internet
Author: User

DI: Dependency Injection (Dependency injection), popular is a kind of XML configuration file, to the Sping container object initialization parameters. Also known as control reversal: inversion of the control (IoC)

There are two main forms of dependency injection:

|-: Dependency Injection based on construction method

|-: Dependency Injection based on setter method

the dependency injection based on the construction method can be divided into the following types:

• Complex data types:

• Simple data type:

|-based on attribute type

|-based on index (Index)

|-based on parameter names (name)

complex data type instances

Package com.fuwh.spring;

/* * Pojo Classes */public class Clazz {private String name;private int grade;Public String GetName () { return name;}public void SetName (String name) { THIS.name = name;}public int Getgrade () { return grade;}public void Setgrade (int grade) { This.grade = grade;}}package com.fuwh.spring;/* * Pojo Classes */public class Lesson {private String name;private int score;Public String GetName () { return name;}public void SetName (String name) { THIS.name = name;}public int Getscore () { return score;}public void SetScore (int score) { This.score = score;}}package Com.fuwh.spring;public class Student {Private Clazz Clazz;Private Lesson Lesson;Public Student (Clazz clazz, Lesson Lesson) { This.clazz = Clazz; This.lesson = lesson;}@OverridePublic String toString () { Return "Student [clazz=" + Clazz.getgrade () +clazz.getname () + ", lesson=" + lesson.getname () + "," +lesson.getscore () + "credits" ";}}

<?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.O Rg/schema/beans/spring-beans.xsd "> <!--configuration file--<bean id= "Student" class= "com.fuwh.spring.Student" lazy-init= "Default" > <constructor-arg ref= "Clazz"/> <constructor-arg ref= "Lesson"/></bean><bean id= "Clazz" class= "Com.fuwh.spring.Clazz" > <property name= "name" value= "Xin"/> <property name= "grade" Value= "/>"</bean><bean id= "Lesson" class= "Com.fuwh.spring.Lesson" > <property name= "name" value= "Java"/> <property name= "Score" value= "4"/></bean></beans>

Package com.fuwh.spring;

Import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Spring01 {public static void Main (string[] args) { /* * Test class */ ApplicationContext ac=new classpathxmlapplicationcontext ("Bean.xml"); System.out.println (Ac.getbean ("Student", Student.class));}}

based on attribute type Example:

Package com.fuwh.spring;/* * Pojo Classes */public class Clazz {private String name;private int grade;Public Clazz (String name, int grade) { Super (); THIS.name = name; This.grade = grade;}@OverridePublic String toString () { Return "Student [name=" + name + ", grade=" + Grade + "]";}}<?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 "> <!--Spring configuration file--<bean id= "Clazz" class= "Com.fuwh.spring.Clazz" lazy-init= "Default" > <constructor-arg type= "String" value= "Information and computational science"/> <constructor-arg type= "int" value= "/>"</bean></beans>

Package Com.fuwh.spring;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Spring01 {public static void Main (string[] args) { /* * Test class */// ApplicationContext ac=new classpathxmlapplicationcontext ("Bean.xml");// ApplicationContext ac=new Classpathxmlapplicationcontext (New string[]{"Beanstudent.xml", "BeanClazz.xml"});// ApplicationContext ac=new classpathxmlapplicationcontext ("Beanstudent.xml");// System.out.println (Ac.getbean ("student")); ApplicationContext ac=new classpathxmlapplicationcontext ("Beanclazz.xml"); System.out.println (Ac.getbean ("Clazz", Clazz.class));}}

An index-based (index) instance :

※ Note that the index starts with "0"
<?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.O Rg/schema/beans/spring-beans.xsd "> <!--Spring configuration file--<bean id= "Clazz" class= "Com.fuwh.spring.Clazz" lazy-init= "Default" > <constructor-arg index= "0" value= "Information and computational science"/> <constructor-arg index= "1" value= "/>"</bean></beans>

based on the argument name (name) instance :

<?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 "> <!--Spring configuration file--<bean id= "Clazz" class= "Com.fuwh.spring.Clazz" lazy-init= "Default" > <constructor-arg name= "name" value= "Information and computational science"/> <constructor-arg name= "Grade" index= "1" value= "/>"</bean></beans>

Dependency Injection based on setter method

Package com.fuwh.spring;

/* * Pojo Classes */public class Lesson {private String name;private int score;public void SetName (String name) { SYSTEM.OUT.PRINTLN ("Name parameter is injected"); THIS.name = name;}public void SetScore (int score) { SYSTEM.OUT.PRINTLN ("Score parameter is injected"); This.score = score;}@OverridePublic String toString () { Return "Lesson [name=" + name + ", score=" + score + "credits]";}}

<?xml version= "1.0" encoding= "UTF-8"?>

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <!--S Pring configuration File--<bean id= "Lesson" class= "Com.fuwh.spring.Lesson" lazy-init= "Default" P:name= "PHP" P:score= "2"/></beans>

Package com.fuwh.spring;

Import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Spring01 {public static void Main (string[] args) { /* * Test class */ ApplicationContext ac=new classpathxmlapplicationcontext ("Beanlesson.xml"); System.out.println (Ac.getbean ("Lesson", Lesson.class));}}

When injected, it is possible to use both of these methods, but in one of the following cases, only the setter can be used to inject

Class A constructs an instance of Class B, which requires an instance of Class A in the construction method of Class B.

At this time will be reported beancurrentlyincreationexception exception.

Spring Feature--di

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.