Spring-bean(二)
命名空间
自动装配
bean之间的关系:继承;依赖
使用外部属性文件
SpEL
bean的生命周期
bean的后置处理器
(一)util命名空间
当用list,set等集合时,不能将集合作为独立的bean定义,导致其他bean无法引用,不同的bean之间不能共享集合。所以,引入util标签。
<!-- 配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间 -->
<util:list id="cars">
<ref bean="car" ></ref>
<ref bean="car2" ></ref>
</util:list>
<bean id="person" class="com.text.Person">
<property name="car" ref="cars"></property>
</bean>
<bean id="person2" class="com.text.Person">
<property name="car" ref="cars"></property>
</bean>
(二)p命名空间
<bean id="person" class="com.text.Person" p:name="tom" p:car-ref="cars"></bean>
bean自动装配(此时person这个bean会自动将car装配,与上面p命名空间实例代码等价)
<bean id="car" class="com.text.Car" p:name="baoma" p:speed="80" p:price="800000"></bean>
<!-- 可以使用autowire属性指定自动装配的方式,byName根据bean的名字和当前bean的setter风格的属性名进行自动装配,若匹配上,则自动匹配
byType根据bean的类型和当前bean的属性的类型进行自动装配,若ioc容器中有一个以上的类型匹配的bean,抛异常
-->
<bean id="person" class="com.text.Person" p:name="Tom" autowire="byName"></bean>
抽象bean以及bean的继承(autowire和abstract不会被继承)
<!-- 抽象bean,bean的abstract属性为true的bean,这样的bean不能被实例化,成为模板bean
若某一个bean的class属性没有指定,则该bean必须是一个抽象bean-->
<bean id="car" p:name="baoma" p:speed="80" p:price="800000" abstract="true"></bean>
<!-- bean配置的继承,使用bean的parent属性指定继承哪个bean的配置 -->
<bean id="car2" class="com.text.Car" p:name="baoma" p:speed="80" parent="car"></bean>
依赖
<!-- 要求再配置Person时,必须有一个关联的car。换句话说Person这个bean依赖于Car这个bean -->
<bean id="person" class="com.text.Person" p:name="Tom" depends-on="cars"></bean>
scope
<!-- 使用bean的scope属性来配置bean的作用域
默认Singleton: 单例,容器初始时创建bean实例,在整个容器的生命周期内只创建一个bean
prototype: 原型的,容器初始化时不创建bean的实例,而在每次请求时都创建一个新的bean实例,并返回 -->
<bean id="car" class="com.text.Car" scope="singleton"></bean>
使用外部属性文件
在配置文件里配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息,如文件路径,数据源配置信息。而这些部署细节实际上需要和Bean配置相分离
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///bookclub"></property>
</bean>
当以后需要改配置时,需要在很多xml文件中寻找bean,十分麻烦,此时引入属性文件
user=root
password=root
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///bookclub
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean>
SpEL(为bean的动态赋值提供了方便)
可以实现:
1. 通过bean的id对bean进行引用
2. 调用方法以及引用对象中的属性
3. 计算表达式的值
4. 正则表达式的匹配
<bean id="car" class="com.text.Car">
<property name="name" value="audi"></property>
<property name="price" value="12345"></property>
<!-- 使用SpEL引用类的静态属性 -->
<property name="speed" value="#{T(java.lang.Math).PI*100}"></property>
</bean> <bean id="person" class="com.text.Person">
<!-- 使用SpEL来应用其他bean的属性 -->
<property name="name" value="#{car.name}"></property>
<!-- 使用SpEL来应用其他bean -->
<property name="car" value="#{car}"></property>
<!-- 在SpEL中使用运算符 -->
<property name="info" value="#{car.price > 30000 ? '金领' : '白领'}"></property>
</bean>
bean的生命周期
1. 通过构造器或工厂方法创建bean实例
2. 为bean的属性设置值和对其他bean的引用
3. 调用bean的初始化方法,前后可调用BeanPostProcessor
4. bean可以使用了
5. 当容器关闭,调用bean的销毁方法
<bean id="car" class="com.text.Car" init-method="init" destroy-method="destroy">
在car.java中加入初始化方法和销毁方法
public void init() {
System.out.println("init/.");
}
public void destroy() {
System.out.println("destroy/.");
}
Main函数
public class Main {
public static void main(String[] args) throws SQLException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Car car = (Car)ctx.getBean("car");
System.out.println(car);
ctx.close();
}
}
bean的后置处理器
允许在调用初始化方法前后对bean进行额外的处理。对ioc容器的所有bean实例逐一处理。可修改返回的bean,甚至返回一个新的bean
典型应用:检查bean属性的正确性或根据特定的标准更改bean的属性
在set方法以及构造方法中加入输出
MyBeanPostProcessor.java
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization:" + bean + "," + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization:" + bean + "," + beanName);
return bean;
}
}
<!-- 配置bean的后置处理器 -->
<bean class="com.text.MyBeanPostProcessor"></bean>
输出顺序
car's contructor
setName:audi
setprice:12345
setspeed:314
postProcessBeforeInitialization:com.text.Car@cdbdf5,car
init/.
postProcessAfterInitialization:com.text.Car@cdbdf5,car
com.text.Car@cdbdf5
destroy/
Spring-bean(二)的更多相关文章
- spring 学习(二):spring bean 管理--配置文件和注解混合使用
spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...
- spring实战二之Bean的自动装配(非注解方式)
Bean的自动装配 自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bea ...
- Spring Bean注册解析(二)
在上文Spring Bean注册解析(一)中,我们讲解了Spring在注册Bean之前进行了哪些前期工作,以及Spring是如何存储注册的Bean的,并且详细介绍了Spring是如何解析 ...
- Spring点滴二:Spring Bean
Spring Bean: 被称作bean的对象是构成应用程序的支柱,是由Spring Ioc容器管理.bean是一个被实例化,配置.组装并由Spring Ioc容器管理对象. 官网API:A Spri ...
- Spring学习二:Spring Bean 定义
Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean 是 ...
- Spring Bean详细讲解
什么是Bean? Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...
- Spring Bean的生命周期(非常详细)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- Spring Bean
一.Spring的几大模块:Data access & Integration.Transcation.Instrumentation.Core Spring Container.Testin ...
- spring bean实例化方式
注意:xml配置中bean节点下scope属性默认值为singleton(单例),在需要多例的情况下需要配置成prototype spring提供三种实例化方式:默认构造.静态工厂.实例工厂 一.默认 ...
- 非spring组件servlet、filter、interceptor中注入spring bean
问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个be ...
随机推荐
- Codeforces Round #Pi (Div. 2) C. Geometric Progression
C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input stan ...
- kentico检查当前授权用户,是否为admin角色
https://docs.kentico.com/k11/custom-development/user-internals-and-api/checking-permissions-using-th ...
- Collections工具类、Map集合、HashMap、Hashtable(十八)
1.Map集合概述和特点 * A:Map接口概述 * 去重复, * 查看API可以知道, * 将键映射到值的对象, * 一个映射不能包含重复的键, * 每个键最多只能映射到一个值.* B:Map接口和 ...
- html5--6-8 CSS选择器5
html5--6-8 CSS选择器5 实例 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...
- U3D Navigation
让我们来一起粗步认识一下NavMesh的简单使用 首先我们建立一个新场景,在新场景我们创建 一个地形或者创建一个Plane, 然后在其上面用Cube或者其它的建立一些障碍物 再创建自己需要为其设置自动 ...
- bzoj1207 [HNOI2004]打鼹鼠——LIS
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1207 这题和求LIS有点像,打这一只鼹鼠一定可以从打上一只鼹鼠转移过来: 所以不用考虑机器人 ...
- Makefile的引入及规则
ARM裸机1期加强版视频课程配套WiKi第9课第5节_Makefile的引入及规则. 文字不能完全替代视频,所以如果你看了这些文章不太懂,建议购买视频进一步学习. 视频购买地址:100ask.taob ...
- Bootstrap-CSS:响应式实用工具
ylbtech-Bootstrap-CSS:响应式实用工具 1.返回顶部 1. Bootstrap 响应式实用工具 Bootstrap 提供了一些辅助类,以便更快地实现对移动设备友好的开发.这些可以 ...
- 02_通过scrollview实现内容滚动
在ScrollView里面放一个Button和TextView程序直接就挂了. ScrollView它只限制了有几个孩子,没限制有几个孙子.给Button和TextView套上一个爹LinearLay ...
- Table View Programming Guide for iOS---(二)----Table View Styles and Accessory Views
Table View Styles and Accessory Views 表格视图的风格以及辅助视图 Table views come in distinctive styles that are ...