欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring ioc 泛型保留

程序员文章站 2022-05-04 14:27:51
spring ioc 泛型保留 在上一篇中,说到spring中并不能自动注入泛型,如何生成泛型类型已经讲的很清楚了,在本篇着重讲在spring中如何自动注入。 环境 这里所有代码基于如下环境,如有出入请参考当前环境。 方法一 通过实现 和`org.springframework.beans.fact ......

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