我有一个基本的自动配置类,它使用泛型来创建你想要的 bean。但是当我测试有两个都扩展该基本配置类的配置时,第二个永远不会创建它的 bean。
我相信这是因为两者的方法名称相同,所以 Spring 假定它已经创建。
有没有办法根据泛型类型动态设置名称?(或其他一些解决方案)
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( classes = { TestGenericBean.MyClientCreator.class, TestGenericBean.MyClientCreator2.class } )
public class TestGenericBean
{
@Autowired
private TestClient client;
@Autowired
private TestClient2 client2;
public static class ClientConfig<T>
{
private Class<T> classCreator;
public ClientConfig(Class<T> classCreator)
{
this.classCreator = classCreator;
}
/* ***** This base class's method is only called once for
* the first class (MyClientCreator) not for the
* second one (MyClientCreator2)
*/
@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;
}
}
/* This is the second configuration class. This config's bean never gets created */
@Configuration
public static class MyClientCreator2 extends ClientConfig<TestClient2>
{
public MyClientCreator2()
{
super( TestClient2.class );
}
}
public static class TestClient2
{
public AsyncRestTemplate asyncRestTemplate;
public TestClient2(AsyncRestTemplate asyncRestTemplate)
{
this.asyncRestTemplate = asyncRestTemplate;
}
}
@Test
public void testBean()
{
System.out.print( client.asyncRestTemplate );
}
}