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 ...
随机推荐
- bzoj3134: [Baltic2013]numbers
稍微用脑子想一想,要是一个回文数,要么s[i]==s[i+1]要么s[i]==s[i+2]就可以实锤了 所以多开两维表示最近两位选的是什么数就完了 注意前导0 #include<cstdio&g ...
- POJ 3750 小孩报数问题 (线性表思想 约瑟夫问题 数组模拟运算的 没用循环链表,控制好下标的指向就很容易了)
小孩报数问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10423 Accepted: 4824 Descripti ...
- ZOJ3209 Treasure Map —— Danc Links 精确覆盖
题目链接:https://vjudge.net/problem/ZOJ-3209 Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 ...
- WinDbg 在64位系统下转储32位进程
在64位系统下,首先要判断进程是32位,还是64位 在Win8之前,进程名后带星号(*)则是32位进程.但Win8.1后,则不显示星号.需要选出“平台”列,来确认32位,还是64位. 在64位系统下的 ...
- codeforces 445B. DZY Loves Chemistry 解题报告
题目链接:http://codeforces.com/problemset/problem/445/B 题目意思:给出 n 种chemicals,当中有 m 对可以发生反应.我们用danger来评估这 ...
- magento导入数据的方法
导入演示数据 分两种情况处理. 如果你是用composer方式安装的 非常简单,二行命令搞定:在项目根目录下执行.我们的是在/var/www/magento2/下面. 安装演示数据 php bin/m ...
- Watir: Watir-WebDriver对富文本编辑器的定位于Watir是不一致的。
Watir对富文本编辑,一般可以采用b.frame().document.body.innerText = "Value you want to insert"但是Watir-We ...
- 我自己比较习惯的Watir自动化测试代码管理方式
- 任务44:Identity MVC: EF + Identity实现
使用VSCode开发 Razer的智能感知不好.所以这里切换为VS2017进行开发: 新建一个Data的文件夹来存放我们的DBContext.在Data文件夹下新建: ApplicationDbCon ...
- HDU 2064 汉诺塔III (递推)
题意:.. 析:dp[i] 表示把 i 个盘子搬到第 3 个柱子上最少步数,那么产生先把 i-1 个盘子搬到 第3个上,再把第 i 个搬到 第 2 个上,然后再把 i-1 个盘子, 从第3个柱子搬到第 ...