Spring bean属性注入(Setter 方法)
2011-03-27 22:01
627 查看
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name;
private Integer id;
private Set<String> sets = new HashSet<String>();
private List<String> lists = new ArrayList<String>();
private Properties properties = new Properties();
private Map<String, String> maps = new HashMap<String, String>();
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 Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
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 PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void save(){
System.out.println("id"+ id+ ","+ name);
personDao.add();
}
}
<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">
<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<!-- 使用内部bean进行注入
<property name="personDao">
<bean class="cn.itcast.dao.impl.PersonDaoBean"/>
</property>
-->
<property name="personDao" ref="personDao"/>
<property name="name" value="itcast"/>
<property name="id" value="88"/>
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<list>
<value>第一个list元素</value>
<value>第二个list元素</value>
<value>第三个list元素</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key-1" value="value-1"/>
<entry key="key-2" value="value-2"/>
<entry key="key-3" value="value-3"/>
</map>
</property>
</bean>
</beans>
private PersonDao personDao;
private String name;
private Integer id;
private Set<String> sets = new HashSet<String>();
private List<String> lists = new ArrayList<String>();
private Properties properties = new Properties();
private Map<String, String> maps = new HashMap<String, String>();
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 Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
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 PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void save(){
System.out.println("id"+ id+ ","+ name);
personDao.add();
}
}
<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">
<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<!-- 使用内部bean进行注入
<property name="personDao">
<bean class="cn.itcast.dao.impl.PersonDaoBean"/>
</property>
-->
<property name="personDao" ref="personDao"/>
<property name="name" value="itcast"/>
<property name="id" value="88"/>
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<list>
<value>第一个list元素</value>
<value>第二个list元素</value>
<value>第三个list元素</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key-1" value="value-1"/>
<entry key="key-2" value="value-2"/>
<entry key="key-3" value="value-3"/>
</map>
</property>
</bean>
</beans>
相关文章推荐
- spring bean setter属性注入
- Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配
- Spring通过set方法注入Bean属性
- 利用SPring实现bean属性setter方式注入
- Spring给bean注入日期属性的方法
- Spring连接数据库的方式1:利用Spring实现bean属性setter方式注入
- Spring依赖注入方式_属性setter方法注入_构造器注入_注解注入
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- spring中使用@RequestBody,对应的bean中属性名大写注入为空的解决方法
- spring bean setter属性注入
- Spring根据XML配置文件注入属性 其实也是造bean,看看是使用constructor还是setter顺带完成属性赋值
- spring中使用@RequestBody,对应的bean中属性名大写注入为空的解决方法
- Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入
- spring in action 学习笔记(六)通过setter注入bean属性
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring 配置bean, 注解方式配置 xml方式要生产set方法方便属性注入,注解方式不需要,利用反射机制注入
- day03_12/13/2016_bean属性的设置之setter方法注入
- Spring(六)属性依赖注入(构造方法,setter方法,p命名空间,spel表达式,集合注入)
- spring中使用@RequestBody,对应的bean中属性名大写注入为空的解决方法
- spring开发_注入其他Bean的方法返回值_MethodInvokingFactoryBean