spring2.5学习笔记(一):三种实例化bean的方式
beans.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方法一 --> <bean id="personservice" lazy-init="default" init-method="init"></bean><!--实例化bean方法二 --><bean id="personservice2" factory-method="createPersonServiceBean"></bean><!--实例化bean方法三 --><bean id="personservicefactory" ></bean><bean id="personservice3" factory-bean="personservicefactory" factory-method="createPersonServiceBean2"></bean></beans>
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("关闭打开的资源");}}
package cn.itcast.service;public interface IPersonService {public abstract void save();}
package cn.itcast.service.impl;public class PersonServiceBeanFactory {public static PersonServiceBean createPersonServiceBean(){return new PersonServiceBean();}public PersonServiceBean createPersonServiceBean2(){return new PersonServiceBean();}}