命名空间

自动装配

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(二)的更多相关文章

  1. spring 学习(二):spring bean 管理--配置文件和注解混合使用

    spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...

  2. spring实战二之Bean的自动装配(非注解方式)

    Bean的自动装配 自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bea ...

  3. Spring Bean注册解析(二)

           在上文Spring Bean注册解析(一)中,我们讲解了Spring在注册Bean之前进行了哪些前期工作,以及Spring是如何存储注册的Bean的,并且详细介绍了Spring是如何解析 ...

  4. Spring点滴二:Spring Bean

    Spring Bean: 被称作bean的对象是构成应用程序的支柱,是由Spring Ioc容器管理.bean是一个被实例化,配置.组装并由Spring Ioc容器管理对象. 官网API:A Spri ...

  5. Spring学习二:Spring Bean 定义

    Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean 是 ...

  6. Spring Bean详细讲解

    什么是Bean? Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...

  7. Spring Bean的生命周期(非常详细)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  8. Spring Bean

    一.Spring的几大模块:Data access & Integration.Transcation.Instrumentation.Core Spring Container.Testin ...

  9. spring bean实例化方式

    注意:xml配置中bean节点下scope属性默认值为singleton(单例),在需要多例的情况下需要配置成prototype spring提供三种实例化方式:默认构造.静态工厂.实例工厂 一.默认 ...

  10. 非spring组件servlet、filter、interceptor中注入spring bean

    问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个be ...

随机推荐

  1. svn服务器搭建与迁移

    2016-11-21更新: 今天被svn的钩子搞了半天,网上找解决方法都无效,下午被我试出来了,特此记录. 在svn的钩子中可以使用update来更新配置文件,比如ansible的,puppet的,具 ...

  2. YTU 2802: 判断字符串是否为回文

    2802: 判断字符串是否为回文 时间限制: 1 Sec  内存限制: 128 MB 提交: 348  解决: 246 题目描述 编写程序,判断输入的一个字符串是否为回文.若是则输出"Yes ...

  3. JQuery操作TABLE,及console.info问题。

    还用alert 吗?看看console.info吧,代码的测试平台:ie9, firefox12 ​1. [代码][JavaScript]代码<!DOCTYPE html><html ...

  4. javascript 树形菜单

    1. [代码][JavaScript]代码     var ME={ini:{i:true,d:{},d1:{},h:0,h1:0,h2:0},html:function(da,f){var s='& ...

  5. codeforces C. Magic Formulas 解题报告

    题目链接:http://codeforces.com/problemset/problem/424/C 题目意思:给出 n 个数:p1, p2, ..., pn,定义: q1 = p1 ^ (1 mo ...

  6. Xcode清楚缓存、清理多余证书路径

    Xcode清除缓存.清理多余证书 1.删除Xcode中多余的证书provisioning profile 手动删除: Xcode6 provisioning profile path: ~/Libra ...

  7. MyBatis相关资源

    MyBatis很多项目中有用到,但会用并不表示你真正理解它,更不代表你能很清楚的教会别人.如果想在会用它的基础上更深入的通过学习它而提升自己技术能力,可利用下面资源. 1.官网文档,基本概念讲的很清楚 ...

  8. Python之路,Day13 - 堡垒机

    项目实战:运维堡垒机开发 前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多人觉得,堡垒机就是跳板机,其实这个认识是不全面的,跳板功能只是堡垒 ...

  9. Goroutine被动调度之一(18)

    本文是<Go语言调度器源代码情景分析>系列的第18篇,也是第四章<Goroutine被动调度>的第1小节. 前一章我们详细分析了调度器的调度策略,即调度器如何选取下一个进入运行 ...

  10. 洛谷 - P1002 - 过河卒 - 简单dp

    https://www.luogu.org/problemnew/show/P1002 方程很好想,题目也很暴力.感谢题目提示数据会很大. #include<bits/stdc++.h> ...