打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Bean的生命周期之初始化
在Spring中,通常是通过配置文档的方式来定义Bean的。
=========================例子====================
<!--Bean的配置文档-->
<!--首先定义为XML的方式来储存Bean的配置-->
<?xml version="1.0" encoding="UTF-8"?>
<!--声明使用的是http://www.springframework.org/dtd/spring-beans.dtd-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<!--配置Bean的开始,根节点Beans中包含一个或多个Bean元素-->
<beans>
<!--定义一个Bean,id是这个Bean的唯一标识,class指出这个Bean的来源-->
 <bean id="HelloWord" class="com.gc.action.HelloWord" depends-on="date">
<!--配置Bean的开始-->
  <property name="msg">
  <value>123</value>
  </property>
  <!-- <property name="date">
   <ref bean="date"/>
  </property> -->
<!--定义上面Bean的结束-->
 </bean>
<!--配置Bean的结束-->
 <bean id="date" class="java.util.Date"></bean>
</beans>
=======================================
在一个大的应用中,会有很多的Bean需要在配置文档中定义,这样配置文档就会很大,变得不好维护,这时可以把相关的Bean放到一个配置文档中,出现多个配置文档。
==============Bean的初始化==============
在Spring中,Bean的初始化有两种方式:
1:在配置文档中通过指定init-method属性来完成.
2:实现org.springframework.bean.factory.InitializingBean接口。
如果一个Bean实现org.springframework.bean.factory.InitializingBean接口则它的所有必需的属性被BeanFactory设置后,会自动执行它的afterPropertiesSet()方法
---------------------------------测试一(推荐使用)---------------------------------
HelloWord 类
package com.gc.action;
import java.util.Date;
public class HelloWord {
 private String msg;
 private Date date;
 private void init() {
  this.msg = "你好!";
  this.date = new Date();
 }
 // 无参构造方法
 public HelloWord() {
  super();
 }
 // 带参构造
 public HelloWord(String msg) {
  super();
  this.msg = msg;
 }
 // ---getXxx 和setXxx
 public String getMsg() {
  return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg;
 }
 public Date getDate() {
  return date;
 }
 public void setDate(Date date) {
  this.date = date;
 }
}
------------------------config.xml----------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="HelloWord" class="com.gc.action.HelloWord" init-method="init" >
 </bean>
</beans>
---------------------TestHelloWord 类-------------------------
package com.gc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.gc.action.HelloWord;
public class TestHelloWord {
 /**
  * @param args
  */
 public static void main(String[] args) {
  ApplicationContext ac =new FileSystemXmlApplicationContext("classpath:/config.xml");
  HelloWord hw =(HelloWord) ac.getBean("HelloWord");
  System.out.println(hw.getMsg()+"==="+hw.getDate());
 }
}
--------------------结果------------------------
你好!===Thu Nov 24 15:14:22 CST 2016
=======================测试二===================
package com.gc.action;
import java.util.Date;
import org.springframework.beans.factory.InitializingBean;
public class HelloWord implements InitializingBean{
 private String msg;
 private Date date;
 public void afterPropertiesSet() {
   this.msg ="zxx";
   this.date = new Date();
 }
 // 无参构造方法
 public HelloWord() {
  super();
 }
 // 带参构造
 public HelloWord(String msg) {
  super();
  this.msg = msg;
 }
 // ---getXxx 和setXxx
 public String getMsg() {
  return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg;
 }
 public Date getDate() {
  return date;
 }
 public void setDate(Date date) {
  this.date = date;
 }
}
=================config.xml============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="HelloWord" class="com.gc.action.HelloWord" >
 </bean>
</beans>
============TestHelloWord 类======================
package com.gc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.gc.action.HelloWord;
public class TestHelloWord {
 /**
  * @param args
  */
 public static void main(String[] args) {
  ApplicationContext ac =new FileSystemXmlApplicationContext("classpath:/config.xml");
  HelloWord hw =(HelloWord) ac.getBean("HelloWord");
  System.out.println(hw.getMsg()+"==="+hw.getDate());
 }
}
=============结果=================
zxx===Thu Nov 24 15:23:25 CST 2016
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
快速上手Spring--7. ref的用法
Spring中Quartz配置 - - JavaEye技术网站
spring and hibernate,spring and tapestry整合篇
struts2和spring的两种整合方式
Spring 中的定时器TimerTask 与 quartz的使用
Spring多数据源的配置和使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服