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方法");
}
}