1、普通方式注入

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5                               http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="person" class="com.xiaostudy.service.PersonImple">
 8         <property name="id" value="2018"></property>
 9         <property name="name" value="xiaostudy"></property>
10         <property name="age" value="23"></property>
11     </bean>
12 </beans>

PersonImple.java

 1 package com.xiaostudy.service;
 2 
 3 public class PersonImple implements Person {
 4 
 5     private Integer id;
 6     private String name;
 7     private Integer age;
 8 
 9     public Integer getId() {
10         return id;
11     }
12 
13     public void setId(Integer id) {
14         this.id = id;
15     }
16 
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public Integer getAge() {
26         return age;
27     }
28 
29     public void setAge(Integer age) {
30         this.age = age;
31     }
32 
33     @Override
34     public String toString() {
35         return "PersonImple [id=" + id + ", name=" + name + ", age=" + age + "]";
36     }
37 
38 }

2、通过构造方法的注入,name为参数名称

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5                               http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="person" class="com.xiaostudy.service.PersonImple">
 8         <constructor-arg name="id" value="2018"></constructor-arg>
 9         <constructor-arg name="name" value="xiaostudy"></constructor-arg>
10         <constructor-arg name="age" value="23"></constructor-arg>
11     </bean>
12 </beans>

PersonImple.java

 1 package com.xiaostudy.service;
 2 
 3 public class PersonImple implements Person {
 4 
 5     private Integer id;
 6     private String name;
 7     private Integer age;
 8 
 9     public PersonImple(Integer id, String name, Integer age) {
10         this.id = id;
11         this.name = name;
12         this.age = age;
13     }
14 
15     public Integer getId() {
16         return id;
17     }
18 
19     public void setId(Integer id) {
20         this.id = id;
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public void setName(String name) {
28         this.name = name;
29     }
30 
31     public Integer getAge() {
32         return age;
33     }
34 
35     public void setAge(Integer age) {
36         this.age = age;
37     }
38 
39     @Override
40     public String toString() {
41         return "PersonImple [id=" + id + ", name=" + name + ", age=" + age + "]";
42     }
43 
44 }

3、也是通过构造方法注入,index为参数索引的位置

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5                               http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="person" class="com.xiaostudy.service.PersonImple">
 8         <constructor-arg index="0" value="2018"></constructor-arg>
 9         <constructor-arg index="1" value="xiaostudy"></constructor-arg>
10         <constructor-arg index="2" value="23"></constructor-arg>
11     </bean>
12 </beans>

4、也是通过构造方法注入,type是参数的数据类型

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5                               http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="person" class="com.xiaostudy.service.PersonImple">
 8         <constructor-arg type="java.lang.Integer" value="2018" index="0"></constructor-arg>
 9         <constructor-arg type="java.lang.String" value="xiaostudy" index="1"></constructor-arg>
10         <constructor-arg type="java.lang.Integer" value="23" index="2"></constructor-arg>
11     </bean>
12 </beans>