zoukankan      html  css  js  c++  java
  • spring ioc 泛型保留

    spring ioc 泛型保留

    在上一篇中,说到spring中并不能自动注入泛型,如何生成泛型类型已经讲的很清楚了,在本篇着重讲在spring中如何自动注入。

    环境

    这里所有代码基于如下环境,如有出入请参考当前环境。

    java version "1.8.0_201"
    Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
    Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
    
     implementation 'org.aspectj:aspectjweaver:1.9.2' 
     implementation "org.springframework:spring-context:5.1.9.RELEASE" 
     implementation 'org.javassist:javassist:3.25.0-GA' 
    

    方法一

    通过实现org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessororg.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor 自定义bean泛型的注入

    MainConfig.java

     @Configuration
    public class MainConfig
    {
    
    	 @Bean
    	public static MyBeanFactoryPostProcessor newMyBeanFactoryPostProcessor()
    	{
    		return new MyBeanFactoryPostProcessor();
    	}
    ....
    

    MyBeanFactoryPostProcessor.java

    public class MyBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor
    {
    
    	@Override
    	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
    	{
    		System.out.println("beanFactory=" + beanFactory);
    		 beanFactory.addBeanPostProcessor(new MyBeanPostProcessor(beanFactory));
    
    	}
    ...
    

    MyBeanPostProcessor.java

    public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor
    {
        public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException
    	{
    		// System.out.println("创建类:" + beanClass);
    		List<Field> fields = new ArrayList<>();
    		for (Field field : beanClass.getDeclaredFields())
    		{
    			if (field.getAnnotation(Autowired.class) != null && MyProvider.class.isAssignableFrom(field.getType()))
    			{
    				Type type = field.getGenericType();
    				if (type != null && type instanceof ParameterizedType)
    				{
    					fields.add(field);
    				}
    			}
    		}
    		if (!fields.isEmpty())
    		{
                //创建bean,请参考上一篇文章
                ....
            }
            return null;
        }
    ...
    

    输出结果如下:

    beanFactory=org.springframework.beans.factory.support.DefaultListableBeanFactory@51081592: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,main,mainConfig,baseProvider,myAspect,testService,testService2,newMyBeanFactoryPostProcessor,newBeanConfig,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy
    service=test.spring.service.TestService2@1817d444
    superclass=test.spring.provider.MyProvider<test.spring.model.User>
    userProviderFieldType=[]
    ResolvableType=test.spring.service.TestService2
    
  • 相关阅读:
    scanf()常犯错误
    C语言,链表反转
    c语言: 冒泡排序
    判断一个数是否为2的若干次幂
    计算一个整数中含1的个数
    ELF文件的加载过程(load_elf_binary函数详解)--Linux进程的管理与调度(十三)
    Linux进程启动过程分析do_execve(可执行程序的加载和运行)---Linux进程的管理与调度(十一)
    Linux内核线程kernel thread详解--Linux进程的管理与调度(十)
    Linux下进程的创建过程分析(_do_fork do_fork详解)--Linux进程的管理与调度(八)
    Linux下2号进程的kthreadd--Linux进程的管理与调度(七)
  • 原文地址:https://www.cnblogs.com/loveheihei/p/12485892.html
Copyright © 2011-2022 走看看