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 ...
随机推荐
- vue文档重读有感
vue 官方文档,每次读都有不一样的感受.项目已经做过一个了,遇到了不少问题,下面总结下这次看到的注意点: 一.指令方面 1. v-once 一次性绑定,只渲染元素和组件一次.随后的重新渲染,元素/ ...
- 织梦CMS使用JS实时动态调用评论数
网站中只要启用了会员系统,网站中的文章就会有评论,在网站首页中调用会员评论也能提升会员体验度,网页都是静态页面,如果每有一个评论都更新html的话就会有点浪费资源了,所以这里给大家分享一个使用JS调用 ...
- android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)
在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄很清楚. 现在把整个总结一下. 其实只要把下面这张图看明白就没问题了. 涉及到的方法一共有下面几个: view获取自身 ...
- Pycharm中如何安装python库
1首先打开pycharm工具,选择File中的Setting选项,如下图所示 2在打开的setting界面中我们点击python的解释器,你会看到很多导入的第三方库,如下图所示,点击最右边的加号 3在 ...
- AJAX路径问题
如果发AJAX请求,看发送请求消息的路径,如果看到报404的错,而这个 时候看下发送头部的路径,如果看到%20,这个时候有可能就是写路劲的时候,不小心按了一个空格
- AES加密算法(C++实现,附源码)
原创作品,转载请注明出自xelz's blog 博客地址:http://mingcn.cnblogs.com/ 本文地址:http://mingcn.cnblogs.com/archive/2010/ ...
- rtmplib rtmp协议过程分析
转自:http://chenzhenianqing.cn/articles/1009.html 写的很好,收藏如下,向作者致敬! 没事碰到了librtmp库,这个库是ffmpeg的依赖库,用来接收,发 ...
- 【转】 JUnit单元测试--IntelliJ IDEA
原文地址:https://blog.csdn.net/weixin_38104426/article/details/74388375 使用idea IDE 进行单元测试,首先需要安装JUnit 插件 ...
- 879C
贪心 题目看错了...还以为是从操作序列中选5个...然后半个小时没了... 我们把每位分别用0和1带入,看看返回值是什么,然后分类讨论.千万不用特判!!!之前忘了删了就fst... #include ...
- Struts2 文件上传 之 文件类型 allowedTypes
转自:https://www.cnblogs.com/zxwBj/p/8546889.html '.a' : 'application/octet-stream', '.ai' : ...