阅读:3478次
评论:0条
更新时间:2011-06-01
1、新建一个java工程名叫spring2.5Test1,在类路径下加入spring.jar和commons-logging.jar,在src下新建beans.xml配置文件
2、具体测试讲解见代码中的注释:
/** * */ package com.iwtxokhtd.bean; /** * @author Administrator * */ public class ExampleBean { /** * 执行bean的初始化可以让bean实现 * org.springframework.beans.factory.InitializingBean接口 * 这里为了与spring容器解耦才自己手工写初始化代码 * 这样在bean配置文件中配置bean时指定init-method属性即可 * <=>afterPropertiesSet()方法 */ //执行初始化工作 public void init(){ System.out.println("Bean例子手工初始化"); } /** * 在这中间执行其它的操作 */ public void middle(){ System.out.println("执行其它的操作"); } /** * 执行bean的销毁工作可以让bean实现 * org.springframework.beans.factory.DisposableBean接口 * 这里为了与spring容器解耦才自己手工写销毁工作代码 * 这样在bean配置文件中配置bean时指定destroy-method属性即可 * <=>destroy()方法 */ //执行销毁或清除工作 public void cleanup(){ System.out.println("Bean例子手工清除工作"); } }
package com.iwtxokhtd.bean; public class NewsFeed { private String news; public String getNews() { return this.toString() + ": '" + news + "'"; } public void setNews(String news) { this.news = news; } }
package com.iwtxokhtd.bean; import org.springframework.beans.factory.ObjectFactory; public class NewsFeedManager { @SuppressWarnings("unused") private ObjectFactory factory; public void setFactory(ObjectFactory factory) { this.factory = factory; } public void printNews() { NewsFeed news = (NewsFeed) factory.getObject(); System.out.println(news.getNews()); } }
beans.xml:
<?xml version="1.0" encoding="gbk"?> <!--若所有bean的初始化和销毁方法都是init和cleanup 且都想这样默认执行,可以在此处的beans元素这配置如下: default-init-method="init" default-destroy-method="cleanup" --> <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 id="example" class="com.iwtxokhtd.bean.ExampleBean" init-method="init" destroy-method="cleanup"/> <!--org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean执行bean的查找--> <bean id="newsFeedManager" class="com.iwtxokhtd.bean.NewsFeedManager"> <property name="factory"> <bean class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean"> <!--查看源代码知此处默认的就是targetBeanName--> <property name="targetBeanName"> <idref local="newsFeed" /> </property> </bean> </property> </bean> <bean id="newsFeed" class="com.iwtxokhtd.bean.NewsFeed" scope="prototype"> <property name="news" value="... that's fit to print!" /> </bean> </beans>
测试代码:
/** * 定制bean特性测试 */ package com.iwtxokhtd.test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.iwtxokhtd.bean.ExampleBean; import com.iwtxokhtd.bean.NewsFeedManager; /** * @author Administrator * */ public class BeanCharacterTest { public static void main(String []args){ /** * 由于测试bean的destroy-method属性设置,因此此处用AbstractApplicationContext * 因为该类有registerShutdownHook()方法,可以让容器关闭而执行销毁方法 * 而ApplicationContext无registerShutdownHook()方法 */ AbstractApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); ExampleBean eb=(ExampleBean)ac.getBean("example"); eb.middle(); ac.registerShutdownHook(); //ac.close(); NewsFeedManager manager = (NewsFeedManager) ac.getBean("newsFeedManager"); manager.printNews(); manager.printNews(); } }
结果:
Bean例子手工初始化
执行其它的操作
com.iwtxokhtd.bean.NewsFeed@1171b26: '... that's fit to print!'
com.iwtxokhtd.bean.NewsFeed@1f78040: '... that's fit to print!'
Bean例子手工清除工作
评论 共 0 条 请登录后发表评论