beans.xml
包cn.itcast.service.impl中的两个类,
普通bean类:PersonServiceBean
PersonServiceBean所继承的接口:IPersonService
工厂类:PersonServiceBeanFactory
<?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 id="personservice" class="cn.itcast.service.impl.PersonServiceBean"
lazy-init="default" init-method="init">
</bean>
<!--实例化bean方法二 -->
<bean id="personservice2" class="cn.itcast.service.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBean">
</bean>
<!--实例化bean方法三 -->
<bean id="personservicefactory" class="cn.itcast.service.impl.PersonServiceBeanFactory" >
</bean>
<bean id="personservice3" factory-bean="personservicefactory" factory-method="createPersonServiceBean2">
</bean>
</beans>
包cn.itcast.service.impl中的两个类,
普通bean类:PersonServiceBean
package cn.itcast.service.impl;
import cn.itcast.service.IPersonService;
public class PersonServiceBean implements IPersonService {
/* (non-Javadoc)
* @see cn.itcast.service.impl.IPersonService#save()
*/
//此方法在配置文件bean标签中用init-method属性调用
public void init(){
System.out.println("初始化");
}
public PersonServiceBean(){
System.out.println("我被实例化了");
}
public void save(){
System.out.println("这是PersonServiceBean类中的save方法");
}
public void destroy(){
System.out.println("关闭打开的资源");
}
}
PersonServiceBean所继承的接口:IPersonService
package cn.itcast.service;
public interface IPersonService {
public abstract void save();
}
工厂类:PersonServiceBeanFactory
package cn.itcast.service.impl;
public class PersonServiceBeanFactory {
public static PersonServiceBean createPersonServiceBean(){
return new PersonServiceBean();
}
public PersonServiceBean createPersonServiceBean2(){
return new PersonServiceBean();
}
}