Spring-Bean的初始化(init方法和实现org.springframework.beans.factory.InitializingBean接口)
来源:互联网 发布:小姐威客网络兼职 编辑:程序博客网 时间:2024/04/28 23:25
init()方法
在BEAN中增加一个方法inti(),用来完成初始化工作(去掉构造函数)
然后修改配置文档config.xml,指定Bean中要初始化的方法,
最后编写测试程序
Bean
package com.gc.action;import java.util.Date;public class HelloWorld {private String msg=null;//该变量用来存储字符串private Date date=null;//该变量用来存储日期//public HelloWorld(String msg)//{// this.msg=msg;//}////public HelloWorld()//这个必须写,否则不能转到上面的那个//{// this.msg=msg;//}public void init(){this.msg="HelloWorld";this.date=new Date();}//设定变量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="init"> <!--将其变量msg通过依赖注入--> </bean> </beans>
测试程序
package com.gc.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import com.gc.action.HelloWorld;public class TestHelloWorld { public static void main(String[] args) { //通过ApplicationContext来获取Spring文件的配置 ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml"); //通过Bean的id来获取Bean HelloWorld helloworld=(HelloWorld)actx.getBean("HelloWorld"); //打印输出 System.out.println(helloworld.getMsg()+" "+helloworld.getDate()); }}
方法2:实现org.springframework.beans.factory.InitializingBean接口
1.首先让HelloWorld.java实现InitializingBena接口,增加一个方法afterPropertiesSet()用来完成初始化工作
2.然后修改配置文档config.xml
3.最后编写测试程序TestHelloWorld.java
package com.gc.action;import org.springframework.beans.factory.InitializingBean;import java.util.Date;public class HelloWorld implements InitializingBean{private String msg=null;//该变量用来存储字符串private Date date=null;//该变量用来存储日期@Overridepublic void afterPropertiesSet() {// TODO Auto-generated method stubthis.msg="HelloWorld";this.date=new Date();}//public HelloWorld(String msg)//{// this.msg=msg;//}////public HelloWorld()//这个必须写,否则不能转到上面的那个//{// this.msg=msg;//}//public void init(){////}//设定变量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" > <!--将其变量msg通过依赖注入--> </bean> </beans>
测试程序不需要改
输出:
HelloWorld Mon Mar 19 16:33:41 CST 2012
第一种方法好,因为第一种方法没有把代码耦合于spring
- Spring-Bean的初始化(init方法和实现org.springframework.beans.factory.InitializingBean接口)
- spring初始化Bean的方法---实现 InitializingBean接口
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring bean通过实现InitializingBean ,DisposableBean接口实现初始化方法和销毁前操作
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring容器初始化异常:org.springframework.beans.factory.BeanDefinitionStoreException
- Spring的Annotation使用注意(org.springframework.beans.factory.NoSuchBeanDefinitionException)
- org.springframework.beans.factory.BeanCreationException(spring注入问题)
- spring org.springframework.beans.factory.BeanCreationException
- Spring-org.springframework.beans.factory.BeanNotOfRequiredTypeException
- 【spring】 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
- spring + redis + org.springframework.beans.factory.UnsatisfiedDependencyException:
- spring异常org.springframework.beans.factory.NoSuchBeanDefinitionException
- 对jfreechar的使用健康图(1)
- UIView的一些基本方法理解:loadView、viewDidLoad、viewDidUnload、viewWillAppear、viewWillDisappear
- 关于博弈论基础知识的一些总结
- 抽象男的情商很低
- 循环链表的创建
- Spring-Bean的初始化(init方法和实现org.springframework.beans.factory.InitializingBean接口)
- Qt5 - 动态加载Glow等特效
- Android中遍历文件夹、比较文件类型测试
- 《大象--Thinking in UML 第二版》已于近日在当当首发,同时邀请各位加入新浪微博[大象-thinkinginUml群]:http://q.weibo.com/1483929
- HDU-1005 Number Sequence【矩阵二分幂】
- Android中的WatchDog
- HTTP POST GET 本质区别详解
- 基于C 的speex编程
- linux 2.6.32.2 mini2440平台移植--内核移植、yaffs2文件系统移植