首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring2.5学习札记(一):三种实例化bean的方式

2012-09-23 
spring2.5学习笔记(一):三种实例化bean的方式beans.xml?xml version1.0 encodingUTF-8?beans xml

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>


包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();}}


热点排行