Spring-管理Bean 使用BeanFactory管理Bean
2012-03-21 09:27
417 查看
Bean:
配置文件:
测试程序:
package com.gc.action; import java.util.Date; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.DisposableBean; public class HelloWorld //implements InitializingBean,DisposableBean { private String msg=null;//该变量用来存储字符串 private Date date=null;//该变量用来存储日期 public void afterPropertiesSet() { // TODO Auto-generated method stub this.msg="HelloWorld"; this.date=new Date(); System.out.println("2000"); } public void cleanup() { // TODO Auto-generated method stub this.msg=""; this.date=null; System.out.println("您销毁了msg"+this.msg+"和date"+this.date); } public HelloWorld(String msg) { this.msg=msg; } public HelloWorld()//这个必须写,否则不能转到上面的那个 { this.msg=msg; } //设定变量msg的set方法 public void setMsg(String msg) { this.msg=msg; } //获取变量msg的get方法 public String getMsg() { return this.msg; } public Date getDate() { return this.date; } public void setDate(Date date) { this.date = date; } }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!--定义一个Bean--> <bean id="HelloWorld" class="com.gc.action.HelloWorld" init-method="afterPropertiesSet" destroy-method="cleanup"> <!--将其变量msg通过依赖注入--> </bean> </beans>
测试程序:
package com.gc.test; import java.util.Date; import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapperImpl; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import com.gc.action.HelloWorld; public class TestHelloWorld { public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { //通过ClassPathResource获取配置文档 ClassPathResource res=new ClassPathResource("config.xml"); //通过XmlBeanFactory来解析配置翁当 XmlBeanFactory factory=new XmlBeanFactory(res); //根据id获取Bean HelloWorld helloWorld=(HelloWorld)factory.getBean("HelloWorld"); //拿出Bean在配置文档中设定的内容 System.out.println(helloWorld.getDate()+" "+helloWorld.getMsg()+"------"); } }
相关文章推荐
- java 如何在listener(监听器) 中使用Spring容器管理bean
- spring-02 spring的bean管理(使用注解方式创建对象 附代码)
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 使用Spring和SpringMVC管理bean时要注意的一个小细节
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean - fjdingsd
- Spring IOC使用(3) 基于注解管理bean
- 在ServletContextListener中使用Spring管理的bean
- Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)
- Spring中Bean的管理 – 使用BeanWrapper
- 为何在新线程中使用注解获取不到Spring管理的Bean
- 在完全由Spring管理的环境中使用Spring的Context获取Bean实例
- java调用spring管理的bean——spring的使用
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- Spring下,使用BeanNameAutoProxyCreator来管理Transaction例子
- Spring的bean管理(注解注入属性和注解文件混合使用)
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- 在ServletContextListener中使用Spring管理的bean