Bean后处理器

新建maven项目并添加spring依赖,目录结构如下

Axe

public interface Axe {
public String chop();
}

Person

public interface Person {
public void useAxe();
}

SteelAxe

public class SteelAxe
implements Axe
{
public SteelAxe()
{
System.out.println("Spring实例化依赖bean:SteelAxe实例...");
}
public String chop()
{
return "钢斧砍柴真快";
}
}

Chinese

public class Chinese implements Person,InitializingBean {
private Axe axe;
private String name; public Chinese() {
System.out.println("Spring 实例化主调bean:Chinese实例...");
} public void setAxe(Axe axe) {
this.axe = axe;
} public void setName(String name) {
System.out.println("Spring执行setName()方法注入依赖关系...");
this.name = name;
} public void useAxe(){
System.out.println(name+axe.chop());
} public void init(){
System.out.println("正在执行初始化方法init...");
} public void afterPropertiesSet() throws Exception {
System.out.println("正在执行初始化方法afterPropertiesSet...");
}
}

MyBeanPostProcessor

package org.mythsky.springdemo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable; public class MyBeanPostProcessor implements BeanPostProcessor { @Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Bean后处理器在初始化之前对"+beanName+"进行增强处理...");
return bean;
} @Nullable
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Bean后处理器在初始化之后对"+beanName+"进行增强处理...");
if(bean instanceof Chinese){
Chinese c=(Chinese)bean;
c.setName("Hello world!");
}
return bean;
}
}

测试BeanTest

package org.mythsky.springdemo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml");
Person p=(Person)ctx.getBean("chinese");
p.useAxe();
}
}

services.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="steelAxe" class="org.mythsky.springdemo.SteelAxe"></bean>
<bean id="chinese" class="org.mythsky.springdemo.Chinese" init-method="init" p:axe-ref="steelAxe" p:name="依赖注入的值"></bean>
<bean class="org.mythsky.springdemo.MyBeanPostProcessor"></bean> </beans>

测试结果

容器后处理器

MyBeanFactoryPostProcessor

package org.mythsky.springdemo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println("程序对Spring所做的BeanFactory的初始化没有改变...");
System.out.println("Spring容器是:"+configurableListableBeanFactory);
}
}

services.xml

<bean class="org.mythsky.springdemo.MyBeanFactoryPostProcessor"></bean>

运行上面的测试

PropertyPlaceholderConfigurer

services.xml

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>dbconn.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"></bean>

dbconn.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://10.200.151.28:3306/spring
jdbc.username=root
jdbc.password=pass

MysqlTest

package org.mythsky.springdemo;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MysqlTest {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml");
ComboPooledDataSource dataSource= (ComboPooledDataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getDriverClass());
System.out.println(dataSource.getJdbcUrl());
System.out.println(dataSource.getUser());
System.out.println(dataSource.getPassword());
}
}

添加c3p0依赖

<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.-dmr</version>
</dependency>

运行结果

PropertyOverrideConfigurer

services.xml

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"></bean>
<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="locations">
<list>
<value>db.properties</value>
</list>
</property>
</bean>
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"></bean>

db.properties

dataSource2.driverClass=com.mysql.jdbc.Driver
dataSource2.jdbcUrl=jdbc:mysql://10.200.151.28:3306/spring
dataSource2.user=root
dataSource2.password=pass

MysqlTest

ComboPooledDataSource dataSource= (ComboPooledDataSource) ctx.getBean("dataSource2");

运行结果同上。

以上两种配置可以简写

<context:property-placeholder location="dbconn.properties"></context:property-placeholder>
<context:property-override location="db.properties"></context:property-override>

spring 后处理器的更多相关文章

  1. Spring Bean后处理器以及容器后处理器【转】

    Bean后处理器:即当spring容器实例化Bean实例之后进行的增强处理. 容器后处理器:对容器本身进行处理,并总是在容器实例化其他任何Bean之前读取配置文件的元数据并可能修改这些数据. 一.Be ...

  2. spring 后置处理器BeanFactoryPostProcessor和BeanPostProcessor的用法和区别

    主要区别就是: BeanFactoryPostProcessor可以修改BEAN的配置信息而BeanPostProcessor不能,下面举个例子说明 BEAN类: package com.spring ...

  3. XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)

    承接上文 IObjectFactoryPostProcessor(工厂后处理器)) 前提是实现接口的对象注册给当前容器 直接继承的对象调用这个postProcessBeanFactory方法,参数为工 ...

  4. Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)

    承接上文 IObjectPostProcessor(对象后处理器) 前提是实现接口的对象注册给当前容器 C#: 通过继承AbstractObjectFactory对象的AddObjectPostPro ...

  5. Spring - BeanPostProcessor接口(后处理器)讲解

    概述: BeanPostProcessor接口是众多Spring提供给开发者的bean生命周期内自定义逻辑拓展接口中的一个,其他还有类似InitializingBean,DisposableBean, ...

  6. Spring框架——后处理器

    Bean的后处理 Spring容器实例化Bean实例之后进行的增强处理,关于这里的描述之前有点错误,现在来纠正一下:这个过程有点像AOP,不过我们知道AOP是对方法而言的,而Bean后处理器是针对Ja ...

  7. Spring的后处理器-BeanPostProcessor跟BeanFactoryPostProcessors

    最近在重读spring源码(为什么要重读?因为不得不承认,去年跟着<深入解析sping源码>一书过了一遍spring的源码,除了满脑袋都是各种BeanFactory跟BeanDefinit ...

  8. 8 -- 深入使用Spring -- 1...两种后处理器

    8.1 两种后处理器 Spring框架提供了很好的扩展性,出了可以与各种第三方框架良好整合外,其IoC容器也允许开发者进行扩展,这种扩展甚至无须实现BeanFactor或ApplicationCont ...

  9. 8 -- 深入使用Spring -- 1...3 容器后处理器

    8.1.3 容器后处理器(BeanFactoryPostProcessor) 容器后处理器负责处理容器本身. 容器后处理器必须实现BeanFacotryPostProcessor接口.实现该接口必须实 ...

随机推荐

  1. silverlight导出图片文件

    新建一个Silverlight应用程序,添加下面两个控件: image控件:image1: Button控件:Click="Button1_Click"; code-Behind代 ...

  2. Android自定义视图三:给自定义视图添加“流畅”的动画

    这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...

  3. phpwind部署问题

    1. 提示"PDO_Mysql 未安装" wamp安装后,首选确保在wamp/php/ext/目录下存在"php_pdo.dll"和"php_pdo_ ...

  4. 正则表达式Regular expressions

    根据某种匹配模式来寻找strings中的某些单词 举例:如果我们想要找到字符串The dog chased the cat中单词 the,我们可以使用下面的正则表达式: /the/gi 我们可以把这个 ...

  5. php获取跳转后的真实链接

    网站的跳转链接经常为本站链接加上一些参数来跳转,如何使用php获取跳转后的链接呢? php代码如下: <?php // echo get_redirect_url('http://www.osc ...

  6. POJ2229--Sumsets(动态规划)

    Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...

  7. hdu 4937 base进制只含3456的base数

    http://acm.hdu.edu.cn/showproblem.php?pid=4937 给定一个数n,若这个数在base进制下全由3,4,5,6组成的话,则称base为n的幸运进制,给定n,求有 ...

  8. codeforces 475D

    题意:给定n(n<=100000)个1e9以内的数的数组a,然后最多有3*1e5的询问,对于每个询问,给定一个x,问有多少个(l<=r&&gcd(a[l],a[l+1].. ...

  9. Delphi 的多线程使用已经很简单了

    先看一个非多线程的例子, 代码执行时不能进行其它操作(譬如拖动窗体): {自定义方法: 在窗体上绘制...} procedure MyMethod; var   i: Integer; begin   ...

  10. winrar.exe 命令行参数

    ========= 下面是 我写大论文时候的实例(批量压缩.备份文件)================== * 一共三个文件:(1) MyCopy.bat :   (2) UnCopy.txt :   ...