gpt4 book ai didi

java - 如何允许基本自动配置类?当我有两个扩展它时,只有第一个被调用

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:14 26 4
gpt4 key购买 nike

我有一个基本的自动配置类,它使用泛型来创建您想要的 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 );
}
}

最佳答案

您可以通过继承createClient方法、删除父类上的@Bean注释并限定bean标题中的名称来解决您的问题:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestGenericBean.MyClientCreator.class, TestGenericBean.MyClientCreator2.class })
public class TestGenericBean {

// Need to remove the bean here so we don't get duplicate naming
// @Bean
public T createClient(AsyncRestTemplate asyncRestTemplate) throws Exception
{
...
}

...

@Configuration
public static class MyClientCreator extends ClientConfig<TestClient> {
public MyClientCreator()
{
super( TestClient.class );
}
@Bean("testClient")
@Override
public TestClient createClient(AsyncRestTemplate asyncRestTemplate) throws Exception {
return super.createClient(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 );
}
@Bean("testClient2")
@Override
public TestClient2 createClient(AsyncRestTemplate asyncRestTemplate) throws Exception {
return super.createClient(asyncRestTemplate);
}
}

...
}

但是,理想情况下,如果您可以控制 Bean,则应该使用很棒的 Object Factory (和类似的)非构造函数/DI 友好 bean 的方法。干杯!

关于java - 如何允许基本自动配置类?当我有两个扩展它时,只有第一个被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48387679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com