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注入方式):
singleton和non-singleton
bean是否是单例模式
单例模式,不管几个请求只有一个共享的实例,唯一的实例。所有对这个bean的请求都会返回这个唯一的实例。
非单例模式,每个请求都会创建新的实例,即类似于new
Spring bean的配置默认为是singleton。
Spring2.0模式下:
Spring2.0以上:
Bean的属性:
在Spring中,Bean的属性值有两种注入方式:settet的依赖注入和构造函数的依赖注入。
在定义Bean 的时候,属性值即可以直接指定也可以参考配置文件中其他的bean。用<ref/>参考其他bean的意思。
setter注入:
构造函数注入:
<!-- 构造函数注入 ,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/>有三种参考关系:
使用依赖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 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:
ApplicationContext:
Bean的销毁:
destroy-method="cleanup"
在bean的类中定义cleanup方法:
配置文件中配置:
关于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>
相关文章推荐
- Spring的核心容器Bean的基础知识(三)
- Spring的核心容器Bean的基础知识(二)
- Spring基础学习教程(Spring之核心容器bean)-06
- 动态代理,Spring两大核心知识BeanFactory、AOP框架!
- Spring学习笔记之bean的基础知识
- Spring之IOC、核心容器和Bean概念详解
- spring的基础知识----Spring的Bean有两种基本行为
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.2 相关Java基础知识 类装载器 反射机制
- Spring基础知识及bean的配置
- Spring应用、原理以及粗读源码系列(一)--框架总述、以Bean为核心的机制(IoC容器初始化以及依赖注入)
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.2 相关Java基础知识 类装载器 反射机制
- Spring之核心容器bean
- Spring基础——在 IOC 容器中 Bean 之间的关系
- (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期
- spring基础知识1:bean属性注入
- Spring(基础)学习文档四(spring容器管理bean)
- Spring之核心容器bean
- Spring(三)——核心容器Bean
- Spring基础知识及bean的配置
- Spring基础特性总结一--核心组件Bean的使用