`

Spring BeanPostProcessor用法

 
阅读更多

BeanPostProcessor该接口作用是:如果我们需要在Spring容器完成Bean的实例化,配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcessor接口的实现。

 

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import com.mchange.v2.c3p0.ComboPooledDataSource;


public class DBPwdEncrypt implements BeanPostProcessor{
	
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {

		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		try {
			//Spring在加载数据源时,先对配置文件的数据库密码解密,然后传给数据源类
			if (bean instanceof DBDataSource){
				String encPass = SystemPropertyUtil.getValue("ecnPassword");
				
				//DES解密
				DesEncrypt des = new DesEncrypt();
				String realPass = des.Decrypt(encPass.getBytes());
				bean.setPassword(realPass);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return bean;
	}
}

 

分享到:
评论

相关推荐

    spring BeanPostProcessor 生命周期

    spring使用模板模式,在bean的创建过程中安插了许多锚点,用户寻找对应的锚点,通过重写方法介入到bean的创建过程当中。本节通过重写这些锚点,学习如何使用BeanPostProcessor、获取各类BeanAware并且理清bean的生命...

    spring杂谈 作者zhang KaiTao

    1.8 Spring3.1 对Bean Validation规范的新支持(方法级别验证) 1.9 Spring对事务管理的支持的发展历程(基础篇) 1.10 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    开源框架 Spring Gossip

    结合 JSTL <spring:bind> 标签 数据绑定的几个方法 <spring:message> 标签 <spring:transform> 标签 其它 View 层 除了 JSP View 层技术之外,您还可以使用其它的 View 层技术,或建立...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack ...19.2. 使用Spring JMS ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring 3 Reference中文

    4.3.2.2 使用静态工厂方法来实例化.. 36 4.3.2.3 使用实例工厂方法来实例化.. 37 4.4 依赖 38 4.4.1 依赖注入. 38 4.4.1.1 基于构造方法的依赖注入 39 4.4.1.2 基于setter 方法的依赖注入...

    spring源码16: @Autowired实现原理

    在阅读本篇之前,如果对后置处理器还不够熟悉的先参考下spring BeanPostProcessor 生命周期 @Autowired使用 构造函数注入 public Class Outer { private Inner inner; @Autowired public Outer(Inner inner) { ...

    spring3.1中文参考文档

    spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................

    spring:这是Spring框架研究

    spring This is spring framework study ...八、使用spring FactoryBean技术模拟mybatis MapperScan注解 https://github.com/AvengerEug/spring/tree/develop/implement-mapperscan 九、mybatis源码学习 ...

    spring基础

    Spring 2.5 的一大增强就是引入了很多注释类,现在您已经可以使用注释配置完成大部分 XML 配置的功能。 @Autowired,按照类型注入 @Resource,默认按照名称注入,如果名称没有指定,则按照类型注入 @Resource 的...

    fredomli#java-standard#Spring容器扩展1

    使用BeanPostProcessor自定义beanBeanPostProcessor接口定义了回调方法,您可以实现这些回调方法来提供您自己的(或覆盖容器的默认

Global site tag (gtag.js) - Google Analytics