Spring 装配基本属性的原理
String 类型
<property name="name" value="wtljiayou"></property>
Integer类型
<property name="id" value="88"></property>
Spring装配各种集合类型的属性
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
<!-- String 类型-->
<property name="name" value="wtljiayou"></property>
<!--Integer类型-->
<property name="id" value="88"></property>
<!-- 注入的集合 -->
application.xml
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
<value>第四个</value>
</set>
</property>
<!-- 注入list集合 -->
<property name="list">
<list>
<value>第一个</value>
<value>第三个</value>
<value>第二个</value>
<value>第一个</value>
</list>
</property>
<!-- 注入properties -->
<property name="properties">
<props>
<prop key="key1">可以1</prop>
<prop key="key2">可以2</prop>
<prop key="key3">可以3</prop>
</props>
</property>
<!-- 注入Map -->
<property name="maps">
<map>
<entry key="key-1" value="value-1"></entry>
<entry key="key-2" value="value-2"></entry>
<entry key="key-3" value="value-3"></entry>
</map>
</property>
</beans>
//这里本身只需要set方法就行,但是为了测试时的调用,就写了get方法
PersonServiceBean.java
package com.service.impl;
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name;
//注入集合
private Set<String> sets =new HashSet<String>();
//注入List集合
private List<String> list = new ArrayList<String>();
//注入properties
private Properties properties =new Properties();
//注入Map<String,String>
private Map<String ,String> maps=new HashMap<String, String>();
public void setName(String name) {
this.name = name;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void save(){
personDao.add();
System.out.println(name);
}
}
PersonService.java
package com.service;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public interface PersonService {
public abstract void save();
public abstract Set<String> getSets();
public abstract List<String> getList();
public abstract Properties getProperties();
public abstract Map<String, String> getMaps();
}
PersonDao.java
package com.dao;
public interface PersonDao {
public abstract void add();
}
PersonDaoBean.java
package com.dao.impl;
import com.dao.PersonDao;
public class PersonDaoBean implements PersonDao {
public void add(){
System.out.println("z执行PersonDaoBean中的方法");
}
}
Spring 装配基本属性和集合
最新推荐文章于 2023-04-18 14:58:49 发布