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 ...
随机推荐
- td 中连续数字或连续英文内容不自动换行
原因: 把连续的英文当做成了一个单词. 解决: 加上 : word-break: break-all (允许单词内换行)
- idhttp post 上传或下载时显示进度条(对接idhttp1.OnWork事件)
通过 idhttp 带进度条上传演示一下,下载和上传原理差不多,说明一下下面例子中的的idhttp 是动态创建的 第一步:添加一个StatusBar或者gauge 进度条,这2个都可以.我用的是 st ...
- YTU 2203: 最小节点(线性表)
2203: 最小节点(线性表) 时间限制: 1 Sec 内存限制: 128 MB 提交: 243 解决: 204 题目描述 (线性表)设有一个由正整数组成的无序(向后)单链表,编写完成下列功能的算 ...
- emacs环境配置
Cscope: 首先官网上下载cscope的源码包,解压进入,按照INSTALL的说明: ./configure make make install 但是在make时报如下错误:fatal error ...
- SPOJ:K-Query Online(归并树)
Given a sequence of n numbers a1, a2, ..., an and a number of k-queries. A k-query is a triple (i, j ...
- 礼物gift(DP)
这道题的DP非常的有意思…… 一开始我们总是会以为这是一个背包问题,直接dp[0] = 0,dp[j] += dp[j-c[i]]进行转移.之后统计一下从dp[m-minn]~dp[m]的答案之和为结 ...
- Watir: Win32ole对于excel某些指令无法操作的时候有如下解决方案
Similar Threads 1. WIN32OLE - failed to create WIN32OLE 2. WIN32OLE#[] and WIN32OLE#[]= method in Ru ...
- 廖雪峰python3练习题一
数据类型和变量 题目: 答案: print(123) print(456.789) print('\'Hello,world\'') print('\'Hello,\\\'Adam\\\'\'') p ...
- 【转】[钉钉通知系列]Jenkins发布后自动通知
转载请注明出处:https://www.cnblogs.com/jianxuanbing/p/7211006.html 阅读目录 一.前言 二.使用钉钉推送的优势 三.配置 一.前言 最近使用Jenk ...
- sublime 插件:Emmet
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...