Spring Detailed (iii)------di Dependency Injection

Source: Internet
Author: User

Last blog We mainly explain the IOC control inversion, that is, the IOC let programmers do not focus on how to create objects, but focus on the operation after the creation of objects, object creation, initialization, destruction and so on to the spring container to do. when creating an object, it is possible to rely on other objects, that is, how the properties of a class are assigned values? This is another core point of our blog that explains Spring: di dependency Injection.

PS: This blog source download link: http://pan.baidu.com/s/1c2xVUDi Password: v1h3

1. What is di dependency injection?

Spring dynamically supplies to an object the other objects it needs. This is achieved through DI (Dependency injection, Dependency injection). For example, object A needs to manipulate the database, before we always have to write code in a to get a connection object, with spring we just need to tell spring,a need a connection, as for this connection how to construct, when to construct , a does not need to know. When the system is running, Spring creates a connection at the right time, and then, like an injection, it injects into a, which completes the control of the relationship between the various objects. A relies on connection to function properly, and this connection is injected into a by spring, and the name of the dependency injection comes in. So how is di implemented? An important feature after Java 1.3 is reflection (reflection), which allows the program to dynamically generate objects, execute methods of objects, and change the properties of objects when it is run, and spring is injected through reflection.

In a nutshell, what is dependency injection, which is assigning a value to a property (including the base data type and the reference data type)

2. Assigning values to attributes using the Set method

First step: Create the project and import the appropriate jar package

Step Two: Create entity class person

Package Com.ys.di;import Java.util.list;import java.util.map;import java.util.properties;import java.util.Set; public class Person {private Long pid;private String pname;private Student students;private List lists;private Set SETS;PR Ivate Map maps;private Properties properties;public Long getpid () {return PID;} public void Setpid (Long pid) {this.pid = pid;} Public String Getpname () {return pname;} public void Setpname (String pname) {this.pname = pname;} Public Student getstudents () {return students;} public void Setstudents (Student students) {this.students = students;} Public List getlists () {return lists;} public void setlists (List lists) {this.lists = lists;} Public Set Getsets () {return sets;} public void Setsets (Set sets) {this.sets = sets;} Public Map Getmaps () {return maps;} public void Setmaps (Map maps) {this.maps = maps;} Public Properties getProperties () {return Properties;} public void SetProperties (properties properties) {This.properties = properties;}}

We see that this entity class includes the reference type Student class, the base data class, and the collection data type.

Step three: Assign values in Applicationcontext.xml

    <!--property is a wrapper class that describes the basic type of a class's properties, strings that require values with value assignment reference types with ref-<bean id= "person" class= "com . Ys.di.Person "> <property name=" pid "value=" 1 "></property> <property name=" pname "value=" Vae ">& Lt;/property><property name= "Students" ><ref bean= "student"/></property><property name= " Lists "><list><value>1</value><ref bean=" student "/><value>vae</value>< /list></property> <property name= "sets" ><set><value>1</value><ref bean= " Student "/><value>vae</value></set></property><property name=" Maps "><map> <entry key= "M1" value= "1" ></entry><entry key= "m2" ><ref bean= "student"/></entry></ Map></property> <property name= "Properties" ><props><prop key= "P1" >p1</prop>< Prop key= "P2" >p2</prop></props></property> </bean>            <bean id= "Student" class= "Com.ys.di.Student" ></bean> 

Fourth Step: Test

Assigning a value to an object using the Set method @testpublic void Testset () {//1, starting Spring container//2, extracting data from the Spring container//3, calling method by object ApplicationContext context = N EW Classpathxmlapplicationcontext ("Applicationcontext.xml"); Person person = (person) context.getbean ("person"); System.out.println (Person.getpname ());//vae}

  

3. Assigning a value to a property using a constructor function

Step One: Add two construction methods to the entity class Per ' Son.java: with and without parameters

Default constructor public person () {}//with parameter constructor public person (Long pid,student students) {this.pid = Pid;this.students = students;}

Step Two: Assign values in Applicationcontext.xml

<!--assign values based on constructors-    <!--    index  represents the position of the parameter from 0 the type refers to the types of the parameters, which   can be distinguished by type when there are multiple constructors. If you can determine the constructor, you can assign the  base type value ref to the reference type--    <bean id= "Person_con" class = "Com.ys.di.Person" ><constructor-arg index= "0" type= "Java.lang.Long" value= "1" ></constructor-arg>    <constructor-arg index= "1" type= "com.ys.di.Student" ref= "Student_con" ></constructor-arg>    </bean>    <bean id= "Student_con" class= "Com.ys.di.Student" ></bean>

Step Three: Test

Use the constructor to assign a value to an object @testpublic void Testconstrutor () {ApplicationContext context = new Classpathxmlapplicationcontext (" Applicationcontext.xml "); Person person = (person) context.getbean ("Person_con"); System.out.println (Person.getpid ());//1}

  

Summarize:

1. Call the default constructor if there is no <constructor-arg> element in the bean in the spring configuration file

2. If there is a <constructor-arg> element in the bean in the spring configuration file, the element determines the unique constructor

Spring Detailed (iii)------di 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.