《一》setter方实现依赖注入:
1)PersonDao接口类:
public interface PersonDao {
public void add();
}
2)PersonDaoBean实现类:
public class PersonDaoBean implements PersonDao {
@Override
public void add() {
System.out.println("执行personDao中的add()方法!");
}
}
3)PersonService 接口类:
public interface PersonService {
public void save();
public Set<String> getSet();
public List<String> getList();
public Properties getProperties();
public Map<String, String> getMap();
}
4)PersonServiceBean 实现类:
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private Integer id;
private String name;
private Set<String> set = new HashSet<String>();
private List<String> list = new ArrayList<String>();
private Properties properties = new Properties();
private Map<String, String> map = new HashMap<String, String>();
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
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> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
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 init() {
System.out.println("init()实例化!");
}
public PersonServiceBean() {
System.out.println("初始化构造函数!");
}
@Override
public void save() {
System.out.println("name= " + name + ", id= " + id);
personDao.add();
}
}
5)XML文件的配置:
<pre name="code" class="html"><?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的方法:(推荐使用)
优点:该Bean可以为多个bean服务
当执行personService即PersonServiceBean时,
会调用ref对应的personDao_即PersonDaoBean,
然后利用反射技术将PersonDaoBean中的方法赋给property对应的属性personDao,<span style="white-space:pre"> </span> 这样personServiceBean中的personDao就相当于
<span style="white-space:pre"> </span> PersonDaoBean的对象.
-->
<bean id="personDao_" class="springDao.PersonDaoBean" />
<bean id="personService" class="springDao.PersonServiceBean" >
<property name="personDao" ref="personDao_" />
<!-- 在此也可以给其它属性注入值 -->
<property name="name" value="ItCast"></property>
<property name="id" value="33"></property>
<!-- 此处的name对应PersonServiceBean中Set集合对象的名字 -->
<property name="set">
<set>
<value>First_set</value>
<value>Second_set</value>
<value>Third_set</value>
</set>
</property>
<property name="list">
<list>
<value>First_list</value>
<value>Second_list</value>
<value>Third_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="map">
<map>
<entry key="key1" value="value1"></entry>
<entry key="key2" value="value2"></entry>
<entry key="key3" value="value3"></entry>
</map>
</property>
</bean>
<!-- 第二种注入Bean的方法:(不推荐使用)
缺点:该内部Bean只能为一个bean服务,具有局限性
<bean id="personService" class="springDao.PersonServiceBean" >
<property name="personDao" >
<bean class="springDao.PersonDaoBean" ></bean>
</property>
</bean>
-->
</bean>
6)测试类springTest:
public class springTest {
@Test
public void instanceSpring() {
AbstractApplicationContext ctx = new
ClassPathXmlApplicationContext("springXml/beans.xml");
PersonService personService = (PersonService)
ctx.getBean("personService");
personService.save();
System.out.println("========输出注入的set集合的值=====");
for (String value : personService.getSet()) {
System.out.println(value);
}
System.out.println("======输出注入的list集合的值======");
for (String value : personService.getList()) {
System.out.println(value);
}
System.out.println("=输出注入的properties集合的键值对=");
for (Object key : personService.getProperties().keySet()) {
System.out.println(key + "=" +
personService.getProperties().getProperty((String) key));
}
System.out.println("=====输出注入的map集合的键值对====");
for (String key : personService.getMap().keySet()) {
System.out.println(key + "=" +
personService.getMap().get(key));
}
}
}
《二》构造函数方法依赖注入:
接口类PersonDao和PersonService与《一》中相同。
1)PersonServiceBean类:
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name;
public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
}
public void save() {
personDao.add();
System.out.println("name= " + name);
}
}
2)XML文件的配置:
<?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="springDaoBean.PersonDaoBean" />
<bean id="personService"
class="springDaoBean.PersonServiceBean">
<constructor-arg index="0" ref="personDao_" />
<constructor-arg index="1" value="Hello World" />
</bean>
</beans>
3)测试文件代码:
public class springTest {
@Test
public void instanceSpring() {
AbstractApplicationContext ctx = new
ClassPathXmlApplicationContext("springXml/beans.xml");
// 使用类构造器实例化Bean
PersonService personService = (PersonService)
ctx.getBean("personService");
personService.save();
}
}
<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></strong>