在Spring中,通常是通过配置文档的方式来定义Bean的。
=========================例子====================
<!--Bean的配置文档-->
<!--首先定义为XML的方式来储存Bean的配置-->
<?xml version="1.0" encoding="UTF-8"?>
<!--配置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----------------
<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============
<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