spring 后处理器
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 后处理器的更多相关文章
- Spring Bean后处理器以及容器后处理器【转】
Bean后处理器:即当spring容器实例化Bean实例之后进行的增强处理. 容器后处理器:对容器本身进行处理,并总是在容器实例化其他任何Bean之前读取配置文件的元数据并可能修改这些数据. 一.Be ...
- spring 后置处理器BeanFactoryPostProcessor和BeanPostProcessor的用法和区别
主要区别就是: BeanFactoryPostProcessor可以修改BEAN的配置信息而BeanPostProcessor不能,下面举个例子说明 BEAN类: package com.spring ...
- XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)
承接上文 IObjectFactoryPostProcessor(工厂后处理器)) 前提是实现接口的对象注册给当前容器 直接继承的对象调用这个postProcessBeanFactory方法,参数为工 ...
- Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)
承接上文 IObjectPostProcessor(对象后处理器) 前提是实现接口的对象注册给当前容器 C#: 通过继承AbstractObjectFactory对象的AddObjectPostPro ...
- Spring - BeanPostProcessor接口(后处理器)讲解
概述: BeanPostProcessor接口是众多Spring提供给开发者的bean生命周期内自定义逻辑拓展接口中的一个,其他还有类似InitializingBean,DisposableBean, ...
- Spring框架——后处理器
Bean的后处理 Spring容器实例化Bean实例之后进行的增强处理,关于这里的描述之前有点错误,现在来纠正一下:这个过程有点像AOP,不过我们知道AOP是对方法而言的,而Bean后处理器是针对Ja ...
- Spring的后处理器-BeanPostProcessor跟BeanFactoryPostProcessors
最近在重读spring源码(为什么要重读?因为不得不承认,去年跟着<深入解析sping源码>一书过了一遍spring的源码,除了满脑袋都是各种BeanFactory跟BeanDefinit ...
- 8 -- 深入使用Spring -- 1...两种后处理器
8.1 两种后处理器 Spring框架提供了很好的扩展性,出了可以与各种第三方框架良好整合外,其IoC容器也允许开发者进行扩展,这种扩展甚至无须实现BeanFactor或ApplicationCont ...
- 8 -- 深入使用Spring -- 1...3 容器后处理器
8.1.3 容器后处理器(BeanFactoryPostProcessor) 容器后处理器负责处理容器本身. 容器后处理器必须实现BeanFacotryPostProcessor接口.实现该接口必须实 ...
随机推荐
- ASP.NET 压缩输出的HTML字符
重写Render using System; using System.Collections.Generic; using System.Text; using System.Web.UI; usi ...
- Redis的appendfsync参数详解
redis.conf中的appendfysnc是对redis性能有重要影响的参数之一.可取三种值:always.everysec和no. 设置为always时,会极大消弱Redis的性能,因为这种模式 ...
- 返回结点值为e的二叉树指针
题意为,如果二叉树某结点的值为e(假定是整型二叉树),返回这个结点的指针.初看这道题,联想到二叉树可以很简单的遍历,直接返回这个指针不就行了吗?如下图所示,假如要返回值为3的结点指针: 设计好了一个函 ...
- 初识Android的ReactiveX
初识Android的ReactiveX 开发一个复杂一点的Android应用都会用到网络请求,交互和动画.这些都意味着 要写很多的回调嵌套.这样的代码也被称为callback hell(回调地狱).这 ...
- CentOS和Ubuntu哪个好?
CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代 ...
- day34(注解)
注解 注解和注释的区别: 注释:是给程序员看的. 注解:是给虚拟机识别的. 注解的作用: 1.代替配置文件 2.标识一个方法,代替一个特殊功能. JDK当中的提供的注解: 标识重写方法 @Overri ...
- POJ1064--Cable master(Binary Search)
Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...
- Scala_类
类 简单类 最简单的类的定义形式是: class Test1 { //这里定义类的字段和方法} 可以使用new关键字来生成对象 var test = new Test1() 给类增加字段和方法 Un ...
- hive 动态分区与混合分区
hive的分区概念,相信大家都非常了解了.通过将数据放在hdfs不同的文件目录下,查表时,只扫描对应分区下的数据,避免了全表扫描. 提升了查询效率. 关于hive分区,我们还会用到多级分区.动态分区. ...
- [mysql] mysql 查询语句收集
// 1. 筛选出当天的中奖名单 $where = " date_format(f_ctime,'%Y-%m-%d') = current_date()"; ...