框架学习 Spring之依赖注入DI
Posted luojack
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了框架学习 Spring之依赖注入DI相关的知识,希望对你有一定的参考价值。
依赖注入的方式有四种:
1、Setter注入(属性注入)
2、构造器注入
3、P命名空间注入
4、集合类型值注入
1、Setter注入(属性注入)
Employee 员工实体类
package com.spring.pojo; public class Employee private Integer id; private String name; private Department department; public Integer getId() return id; public void setId(Integer id) this.id = id; public String getName() return name; public void setName(String name) this.name = name; public Department getDepartment() return department; public void setDepartment(Department department) this.department = department; @Override public String toString() return "Employee [id=" + id + ", name=" + name + ", department=" + department + "]"; public Employee(Integer id, String name, Department department) super(); this.id = id; this.name = name; this.department = department; public Employee() super(); // TODO Auto-generated constructor stub
Department 部门实体类
package com.spring.pojo; public class Department private Integer id; private String name; public Integer getId() return id; public void setId(Integer id) this.id = id; public String getName() return name; public void setName(String name) this.name = name; public Department() super(); // TODO Auto-generated constructor stub public Department(Integer id, String name) super(); this.id = id; this.name = name; @Override public String toString() return "Department [id=" + id + ", name=" + name + "]";
主配置文件里面
2、构造器注入
3、P命名空间注入
添加命名空间
xmlns:p="http://www.springframework.org/schema/p"
使用P标签
4、集合类型注入
创建collection集合实体类
package com.spring.pojo; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class CollectionBean private List<String> list; private Set<String> set; private Map<String, Object> map; private Properties properties; private String[] array; public CollectionBean() super(); // TODO Auto-generated constructor stub public CollectionBean(List<String> list, Set<String> set, Map<String, Object> map, Properties properties, String[] array) super(); this.list = list; this.set = set; this.map = map; this.properties = properties; this.array = array; public List<String> getList() return list; public void setList(List<String> list) this.list = list; public Set<String> getSet() return set; public void setSet(Set<String> set) this.set = set; public Map<String, Object> getMap() return map; public void setMap(Map<String, Object> map) this.map = map; public Properties getProperties() return properties; public void setProperties(Properties properties) this.properties = properties; public String[] getArray() return array; public void setArray(String[] array) this.array = array;
主配置文件中,依赖注入
<bean id="collectionBean" class="com.spring.pojo.CollectionBean">
<!--有序可重复 -->
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<!--无序不可重复 -->
<property name="set" >
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="map">
<map>
<entry key="key1" value="法海1"></entry>
<entry key="key2" value="法海2"></entry>
<entry key="key3" value="法海3"></entry>
</map>
</property>
<property name="array">
<array>
<value>String1</value>
<value>String2</value>
<value>String3</value>
</array>
</property>
<!--properties是特殊的Map -->
<property name="properties">
<props>
<prop key="prokey1">values1</prop>
<prop key="prokey2">values2</prop>
<prop key="prokey3">values3</prop>
</props>
</property>
</bean>
以上是关于框架学习 Spring之依赖注入DI的主要内容,如果未能解决你的问题,请参考以下文章
spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入