zoukankan      html  css  js  c++  java
  • BeanFactoryPostProcessor进行bean的注入

    BeanFactoryPostProcessor 自定义spring进行注入

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.support.GenericBeanDefinition;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyPostProcess implements BeanFactoryPostProcessor {
    	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    		GenericBeanDefinition beanDefinition = (GenericBeanDefinition) beanFactory.getBeanDefinition("userServiceImpl");
    		//如果需要被注入的bean是接口的实现类,需要将spring默认的jdk代理改为cglib代理
    		//因为jdk是代理的接口,cglib是根据具体的类来进行代理的
    		beanDefinition.setBeanClass(UserServiceImpl2.class);
    		
    	}
    }
    
     <aop:aspectj-autoproxy proxy-target-class="true"/>
    

    如果使用默认的jdk代理,会抛出以下异常

    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.simon.UserServiceImpl2' available
    
  • 相关阅读:
    BZOJ 2724: [Violet 6]蒲公英
    codeforces Lightsabers (hard)
    BZOJ 3884: 上帝与集合的正确用法
    BZOJ 4809: 皇后
    python的变量类型(Day6)
    Python集合方法整理(Day9)
    基本运算符与流程控制(Day5)
    基本数据类型(Day4)
    第一个Python程序(Day3)
    操作系统(Day2.5)
  • 原文地址:https://www.cnblogs.com/SimonHu1993/p/14530119.html
Copyright © 2011-2022 走看看