您的位置:首页 > 编程语言 > Java开发

spring2.5 依赖注入--setter

2011-08-03 22:34 246 查看
ref注入

<?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"> <bean id="personDao" class="net.itdos.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="net.itdos.service.impl.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
</bean>

</beans>


内部bean注入

<?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"> <bean id="personService" class="net.itdos.service.impl.PersonServiceBean">
<property name="personDao">
<bean class="net.itdos.dao.impl.PersonDaoBean"></bean>
</property>
<property name="name" value="xxx"></property>
</bean>

</beans>


集合注入

<?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"> <bean id="personService" class="net.itdos.service.impl.PersonServiceBean">
<property name="personDao">
<bean class="net.itdos.dao.impl.PersonDaoBean"></bean>
</property>
<property name="">
<list>
<value></value>
</list>
</property>
</bean>

</beans>


package net.itdos.service.impl;

import net.itdos.dao.PersonDao;
import net.itdos.service.PersonService;

public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name;
public void setName(String name) {
this.name = name;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void save(){
System.out.println(name);
personDao.save();
}
}


package net.itdos.dao.impl;

import net.itdos.dao.PersonDao;

public class PersonDaoBean implements PersonDao {
public void save(){
System.out.println("save方法");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: