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. Jersey RESTful WebService框架学习(七)文件上传

    引入jar包:jersey-media-multipart-2.22.jar 前端: <body> <input id="commonFile" type=&qu ...

  2. python计算机硬件基础以及变量常量常量池,解释器编译器比较,python的两种运行方式

    1.什么是编程语言 语言是一个事物与另外一个事物沟通的介质 编程语言是程序员与计算机沟通的介质 2.什么是编程 编程就是程序按照某种编程语言的语法规范将自己想要让计算机做的事情表达出来 表达的结果就是 ...

  3. Page页面生命周期——微信小程序

    onLoad:function (options) {     //页面初始化     console.log('index Load') }, onShow:function () {     // ...

  4. Warning the user/local/mysql/data directory is not owned by the mysql user

    sudo chown -RL root:mysql /usr/local/mysql sudo chown -RL mysql:mysql /usr/local/mysql/data sudo /us ...

  5. POJ3616--Milking Time(动态规划)

    Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...

  6. InvocationHandler中invoke方法中的第一个参数proxy的用途

    最近在研究Java的动态代理时对InvocationHandler中invoke方法中的第一个参数一直不理解它的用处,某度搜索也搜不出结果,最后终于在stackoverflow上找到了答案. 这是原文 ...

  7. 方案dp。。

    最近经常做到组合计数的题目,每当看到这种题目第一反应总是组合数学,然后要用到排列组合公式,以及容斥原理之类的..然后想啊想,最后还是不会做.. 但是比赛完之后一看,竟然是dp..例如前几天的口号匹配求 ...

  8. Android自适应屏幕的实现方法

    首先我们先了解下手机分辨率 分辨率是指屏幕上有横竖各有多少个像素目前手机分辨率大概情况如下: QVGA 分辨率:320×240 简    介:QVGA即"Quarter VGA". ...

  9. DELPHI微信支付代码

    DELPHI微信支付代码   不管是微信支付还是支付宝支付, 3个最棘手的问题是:1,如何生成签名2,支付请求如何提交3, 如何验证签名 下面就围绕这二个问题来讲. 我使用的是XE3. 先看微信支付: ...

  10. [转载]金融行业 DevOps 解决方案概述

    2009 年 6 月份,John Allspaw 及 Paul Hammond 在速度大会 (Velocity) 上分享了在 Flickr 中如何通过加强 Dev(开发团队)和 Ops(运维团队)之间 ...