Spring 一二事 - 依赖注入

Posted 风间影月の技术小站

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 一二事 - 依赖注入相关的知识,希望对你有一定的参考价值。

 1     <!-- 依赖注入的装配过程 -->
 2     <bean id="person" class="com.lee.spring007.di.xml.setter.Person">
 3         <property name="pid" value="1001"></property>
 4 
 5         <property name="name" value="nathan"></property>
 6 
 7         <property name="stu" ref="student"></property>
 8 
 9         <property name="list">
10             <list>
11                 <value>a</value>
12                 <value>b</value>
13                 <value>c</value>
14                 <value>d</value>
15                 <value>e</value>
16             </list>
17         </property>
18 
19         <property name="map">
20             <map>
21                 <entry key="number" value="111"></entry>
22                 <entry key="bean" value-ref="student"></entry>
23             </map>
24         </property>
25 
26         <property name="properties">
27             <props>
28                 <prop key="one">111</prop>
29                 <prop key="two">222</prop>
30                 <prop key="three">333</prop>
31             </props>
32         </property>
33 
34         <property name="sets">
35             <set>
36                 <value>11</value>
37                 <ref bean="student" />
38             </set>
39         </property>
40 
41         <property name="obj">
42             <list>
43                 <value>a</value>
44                 <value>b</value>
45                 <value>c</value>
46                 <value>d</value>
47                 <value>e</value>
48             </list>
49         </property>
50 
51     </bean>
52     <bean id="student" class="com.lee.spring007.di.xml.setter.Student"></bean>

 

 1 package com.lee.spring007.di.xml.setter;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.Properties;
 6 import java.util.Set;
 7 
 8 public class Person {
 9     private int pid;
10     private String name;
11     private Student stu;
12     private List list;
13     private Set sets;
14     private Map map;
15     private Properties properties;
16     private Object[] obj;
17     public int getPid() {
18         return pid;
19     }
20     public void setPid(int pid) {
21         this.pid = pid;
22     }
23     public String getName() {
24         return name;
25     }
26     public void setName(String name) {
27         this.name = name;
28     }
29     public Student getStu() {
30         return stu;
31     }
32     public void setStu(Student stu) {
33         this.stu = stu;
34     }
35     public List getList() {
36         return list;
37     }
38     public void setList(List list) {
39         this.list = list;
40     }
41     public Map getMap() {
42         return map;
43     }
44     public void setMap(Map map) {
45         this.map = map;
46     }
47     public Properties getProperties() {
48         return properties;
49     }
50     public void setProperties(Properties properties) {
51         this.properties = properties;
52     }
53     public Object[] getObj() {
54         return obj;
55     }
56     public void setObj(Object[] obj) {
57         this.obj = obj;
58     }
59     public Set getSets() {
60         return sets;
61     }
62     public void setSets(Set sets) {
63         this.sets = sets;
64     }
65     
66 }
1 package com.lee.spring007.di.xml.setter;
2 
3 public class Student {
4 
5     public static void say() {
6         System.out.println("I am a student!");
7     }
8 }

 github地址:https://github.com/leechenxiang/maven-spring001-helloworld

以上是关于Spring 一二事 - 依赖注入的主要内容,如果未能解决你的问题,请参考以下文章

Spring 一二事 - 单例

Spring 一二事 - 别名

Spring 一二事 - IOC MVC 简易搭建

Spring 一二事 - xml 形式的 AOP

H5一二事

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段