您的位置:首页 > 编程语言 > Java开发

Spring的核心容器Bean的基础知识(一)

2017-03-12 20:41 633 查看
Spring Bean无侵入架构模式

关于Spring Bean的两个关键的包:

spring-beans-4.3.7.RELEASE.jar

spring-context-4.3.7.RELEASE.jar

这两个包里最重要的是两个class,BeanFactory和ApplicationContext

(一)Bean的基础知识

id 和class

见配置文件(set注入方式):

<bean id="HelloWorld" class="com.gc.acion.HelloWorld">
<property name="msg">
<value>wangyj</value>
</property>
</bean>

singleton和non-singleton

bean是否是单例模式

单例模式,不管几个请求只有一个共享的实例,唯一的实例。所有对这个bean的请求都会返回这个唯一的实例。

非单例模式,每个请求都会创建新的实例,即类似于new

Spring bean的配置默认为是singleton。

Spring2.0模式下:

<bean id="HelloWorld" class="com.gc.acion.HelloWorld" scope="singleton">
<property name="msg">
<value>wangyj</value>
</property>
</bean>


Spring2.0以上:

<bean id="HelloWorld" class="com.gc.acion.HelloWorld" singleton="true">
<property name="msg">
<value>wangyj</value>
</property>
</bean>


Bean的属性:
在Spring中,Bean的属性值有两种注入方式:settet的依赖注入和构造函数的依赖注入。

在定义Bean 的时候,属性值即可以直接指定也可以参考配置文件中其他的bean。用<ref/>参考其他bean的意思。

setter注入:

<bean id="Hello" class="com.gc.acion.EnHello">
<property name="msg">
<value>laoda</value>
</property>
</bean>


构造函数注入:

<!-- 构造函数注入 ,constructor-arg表示通过构造函数注入的方式注入,index="0"表示构造函数的第一个参数,如果只有一个参数可省略-->
<bean id="DataBase" class="com.gc.acion.OracleDataBase">
<constructor-arg index="0">
<value>OracleDataBase</value>
</constructor-arg>
</bean>

属性值参考配置文件中的其他的bean:

<bean id="date" class="java.Util.Date"></bean>

<bean id="Hello" class="com.gc.acion.EnHello">
<property name="msg">
<value>laoda</value>
</property>
<property name="date">
<ref bean="date"/>
</property>
</bean>

其中<ref/>有三种参考关系:

<bean id="HelloWorld" class="com.gc.acion.HelloWorld" scope="singleton" init-method="init" destroy-method="cleanup" depends-on="date">
<!-- local属性模式 -->
<property name="date">
<ref local="date"/>
</property>
<!-- bean属性模式 -->
<property name="date">
<ref bean="date"/>
</property>
<!-- parent属性模式 -->
<property name="date">
<ref parent="date"/>
</property>
</bean>


使用依赖depends-on:

bean的depends-on属性可以用来在初始化这个bean之前,强制初始化指定的其他一个或多个的bean。

依上例,depends-on="date",就是在初始化HelloWorld这个bean之前,强制先初始化date这个bean。

Bean的生命周期:

Bean的定义——>Bean的初始化——>Bean的使用——>Bean的销毁

Bean的一定一般放在xml配置文件中定义,有id 和 class

Bean的初始化有两种实现方式:

一种

init-method="init" 。在Bean的类中定义一个init的方法,对属性进行初始化。

代码和配置如下:

package com.gc.acion;

public class HelloWorld {

private String msg = null;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

}

<bean id="HelloWorld" class="com.gc.acion.HelloWorld" scope="singleton">
<property name="msg">
<value>wangyj</value>
</property>
</bean>


等同于

package com.gc.acion;

public class HelloWorld {

private String msg = null;

public void init(){
this.msg = "wangyj";
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

}
<bean id="HelloWorld" class="com.gc.acion.HelloWorld" scope="singleton" init-method="init">
</bean>

另一种
实现InitializingBean.class接口,在afterPropertiesSet方法中初始化。

Bean的使用一般有3种方式,这里介绍两种常用的:

BeanFactory:

ClassPathResource resource = new ClassPathResource("config.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader  reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);


ApplicationContext:

//通过Spring.xml配置文件获取属性
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
HelloWorld helloWorld = (HelloWorld)context.getBean("HelloWorld");//通过bean得到HelloWorld


Bean的销毁:
destroy-method="cleanup"

在bean的类中定义cleanup方法:

package com.gc.acion;

public class HelloWorld {

private String msg = null;

public void init(){
this.msg = "wangyj";
}

public void cleanup(){
this.msg = "";
System.out.println("HelloWorld中的"+this.msg+"已销毁");
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

}


配置文件中配置:
<bean id="HelloWorld" class="com.gc.acion.HelloWorld" scope="singleton" init-method="init" destroy-method="cleanup">
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: