Spring-IOC(DI)的三种注入方式

spring为方便不同的需求,为我们提供了3中不同的注入方式分别是set、get方法注入,构造注入还有p命名空间注入,老规矩,直接上代码

首先创建实体类Student

 1 public class Student {
 2     private String name;
 3     private int age;
 4 
 5     public String getName() {
 6         return name;
 7     }
 8 
 9     public void setName(String name) {
10         this.name = name;
11     }
12 
13     public int getAge() {
14         return age;
15     }
16 
17     public void setAge(int age) {
18         this.age = age;
19     }
20 
21     //重写toString方法,方便测试
22     @Override
23     public String toString() {
24         return "Student{" +
25                 "name='" + name + '\'' +
26                 ", age=" + age +
27                 '}';
28     }
29 }

 第一种set注入,以下是核心配置文件

 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        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 6     <!--set、get注入-->
 7     <bean id="student" class="com.lhf.entity.Student">
 8         <property name="name" value="张三"/>
 9         <property name="age" value="16"/>
10     </bean>
11 </beans>

测试类:

 1 public class App 
 2 {
 3     public static void main( String[] args )
 4     {
 5         //spring读取xml文件
 6         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 7         //从spring容器中获取service层的对象
 8         Student bean = (Student) context.getBean("student");
 9         //输出
10         System.out.println(bean);
11     }
12 }

第二种,构造注入

创建实体类,并写出成员的构造方法,这里建议将该类的无参构造也写上

 1 public class Student {
 2     private String name;
 3     private int age;
 4 
 5     public Student() {
 6     }
 7 
 8     public Student(String name, int age) {
 9         this.name = name;
10         this.age = age;
11     }
12     
13     //重写toString方法,方便测试
14     @Override
15     public String toString() {
16         return "Student{" +
17                 "name='" + name + '\'' +
18                 ", age=" + age +
19                 '}';
20     }
21 }

配置文件:(在进行构造注入的时候,我们需要按照参数的顺序逐个进行注入,否则回报错。在不同的场合我们需要进行的参数赋值也是不同的,所以构造注入也是有局限性的,至于构造注入那一定也是有他的好处的,比如在对象实例化的时候我们就可以将参数的赋值,效率高)

   <bean id="student" class="com.lhf.entity.Student">
        <!--可以通过参数类型进行注入-->
        <constructor-arg type="java.lang.String" value="张三"/>
        <!--也可以通过参数在构造方法中的位置进行注入-->
        <constructor-arg index="1" value="16"/>
    </bean>

  

第三种,p命名空间:(这种注入说白了就是set注入的简化,也就是说,我们还需要重写set方法,这里我就不写了)

<bean id="student" class="com.lhf.entity.Student" p:name="张三" p:age="16"/>

以上三种方法测试类相同,这里就投个懒只写一次,至于结果自行运行

以下是数组和集合的注入方式

实体类:

 1 public class Student {
 2     private Map<String,Integer> map;//map集合注入
 3     private Set<String> set;//set集合的注入
 4     private List<String>lists;//list集合注入
 5     private Properties properties;//properties注入
 6 
 7     public void setMap(Map<String, Integer> map) {
 8         this.map = map;
 9     }
10 
11     public void setSet(Set<String> set) {
12         this.set = set;
13     }
14 
15     public void setLists(List<String> lists) {
16         this.lists = lists;
17     }
18 
19     public void setProperties(Properties properties) {
20         this.properties = properties;
21     }
22     //重写toString方法,方便测试
23 
24     @Override
25     public String toString() {
26         return "Student{" +
27                 "map=" + map +
28                 ", set=" + set +
29                 ", lists=" + lists +
30                 ", properties=" + properties +
31                 '}';
32     }
33 }

配置文件

 1 <bean id="student" class="com.lhf.entity.Student">
 2         <property name="lists">
 3             <list>
 4                 <value>张三</value>
 5                 <value>李四</value>
 6             </list>
 7         </property>
 8         <property name="map">
 9             <map>
10                 <entry key="张三" value="16"></entry>
11                 <entry key="李四" value="18"></entry>
12             </map>
13         </property>
14         <property name="set">
15             <set>
16                 <value>张三</value>
17                 <value>李四</value>
18             </set>
19         </property>
20         <property name="properties">
21             <props>
22                 <prop key="张三">22</prop>
23                 <prop key="李四">66</prop>
24             </props>
25         </property>
26     </bean>

猜你喜欢

转载自www.cnblogs.com/Tiandaochouqin1/p/10389071.html