當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
框架学习 Spring之依赖注入DI
生活随笔
收集整理的這篇文章主要介紹了
框架学习 Spring之依赖注入DI
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
依賴注入的方式有四種:
1、Setter注入(屬性注入)
2、構(gòu)造器注入
3、P命名空間注入
4、集合類型值注入
?
?
?
1、Setter注入(屬性注入)
Employee 員工實(shí)體類
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;}@Overridepublic 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 }} View Code?
?
Department 部門實(shí)體類
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;}@Overridepublic String toString() {return "Department [id=" + id + ", name=" + name + "]";}} View Code?
主配置文件里面
?
?
?
2、構(gòu)造器注入
?
3、P命名空間注入
添加命名空間
xmlns:p="http://www.springframework.org/schema/p"
使用P標(biāo)簽
?
?
4、集合類型注入
創(chuàng)建collection集合實(shí)體類
?
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;}} View Code?
?
主配置文件中,依賴注入
<bean id="collectionBean" class="com.spring.pojo.CollectionBean"><!--有序可重復(fù) --><property name="list"><list><value>list1</value><value>list2</value><value>list3</value></list></property><!--無(wú)序不可重復(fù) --><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> View Code?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/luojack/p/11141004.html
總結(jié)
以上是生活随笔為你收集整理的框架学习 Spring之依赖注入DI的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++STL总结笔记(二)——仿函数(函
- 下一篇: 【JS】实时监控页面,input框数值自