1

我想提供一个基本配置类,它将处理创建具有泛型类型的 bean,该泛型类型在您扩展类时定义,如下所示。但它从不调用@Bean方法。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Constructor;

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( classes = { TestGenericBean.MyClientCreator.class } )
public class TestGenericBean
{
    /* This throws an error saying no bean provided */
    @Autowired
    private TestClient client;

    public static class ClientConfig<T>
    {
        private Class<T> classCreator;

        public ClientConfig(Class<T> classCreator)
        {
            this.classCreator = classCreator;
        }

        /* This is never called */
        @Bean
        public T createClient(RestTemplate restTemplate) throws Exception
        {
            Constructor<T> constructor = classCreator.getConstructor(
                RestTemplate.class
            );

            return constructor.newInstance( restTemplate );
        }

        /* This is never called */
        @Bean
        public RestTemplate restTemplate()
        {
            return new RestTemplate();
        }
    }

    @Configuration
    public static class MyClientCreator extends ClientConfig<TestClient>
    {
        public MyClientCreator()
        {
            super( TestClient.class );
        }
    }

    public static class TestClient
    {
        public RestTemplate restTemplate;

        public TestClient(RestTemplate restTemplate)
        {
            this.restTemplate = restTemplate;
        }
    }

    @Test
    public void testBean()
    {
        System.out.print( client.restTemplate );
    }
}
4

2 回答 2

1

不知道为什么,但它需要一个 bean 类型MappingJackson2HttpMessageConverter。有了它,它就可以工作了。

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( classes = { TestGenericBean.MyClientCreator.class } )
public class TestGenericBean
{
    @Autowired
    private TestClient client;

    public static class ClientConfig<T>
    {
        private Class<T> classCreator;

        public ClientConfig(Class<T> classCreator)
        {
            this.classCreator = classCreator;
        }

        /**** THIS IS REQUIRED - WHY? ****/
        @Bean
        public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter()
        {
            return new MappingJackson2HttpMessageConverter();
        }

        @Bean
        public T createClient(AsyncRestTemplate asyncRestTemplate) throws Exception
        {
            Constructor<T> constructor = classCreator.getConstructor(
                AsyncRestTemplate.class
            );

            return constructor.newInstance( asyncRestTemplate );
        }

        @Bean
        public AsyncRestTemplate asyncRestTemplate()
        {
            return new AsyncRestTemplate();
        }
    }

    @Configuration
    public static class MyClientCreator extends ClientConfig<TestClient>
    {
        public MyClientCreator()
        {
            super( TestClient.class );
        }
    }

    public static class TestClient
    {
        public AsyncRestTemplate asyncRestTemplate;

        public TestClient(AsyncRestTemplate asyncRestTemplate)
        {
            this.asyncRestTemplate = asyncRestTemplate;
        }
    }

    @Test
    public void testBean()
    {
        System.out.print( client.asyncRestTemplate );
    }
}
于 2018-01-18T00:52:00.907 回答
0

我最近没有经常使用 Spring / Spring boot,但我对注释预处理器有很多经验。Spring Boot 使用它们的方式不允许将任何通用方法用作 @autowire 的 bean 提供程序。这是因为它无法安全地解析 java 类 <T> 并因此将其与字段进行映射。我建议您尝试使用诸如 Getter< T > 或 List< T > 之类的包装类,但不能保证任何通用实现都能正常工作。

于 2018-01-18T00:36:56.860 回答