gpt4 book ai didi

Java Spring : getting the generic type

转载 作者:行者123 更新时间:2023-11-30 01:42:05 26 4
gpt4 key购买 nike

我有一个实现 FactoryBean 的参数化类接口(interface):

public class DogFactory<T extends Dog> implements FactoryBean<T> {

// ...

}

我想要的是使用这个工厂来生成不同类的对象(所有这些类都扩展 Dog )。所以我想我可以做如下的事情:

public class ShepherdService {

private DogFactory<Shepherd> shepherdFactory;

public ShepherdService(
@Autowired
DogFactory<Shepherd> shepherdFactory
) {
this.shepherdFactory = shepherdFactory;
}

// ...

}

唉,我收到以下错误:Couldn't autowire. No beans of DogService<Shepherd> type found. :-(

如何注入(inject)并用作DogFactory<Shepherd>DogFactory<Corgi> (不仅仅是 DogFactory )?

还有一个问题。我还需要通过Shepherd.class (或 Corgi.class )到这个 bean,这样它就可以在运行时“知道”它应该生成什么类的对象。有办法做到这一点吗?

或者我应该忘记 FactoryBean并将工厂实例化为常规类?当然,我可以这样做:

    DogFactory<Shepherd> shepherdFactory = new DogFactory<Shepherd>(Shepherd.class);

它可以完美工作,但我想使用 FactoryBean因为我在这个项目的其他工厂中使用它,所以我想坚持 FactoryBean .

提前致谢!

更新 1

也许我应该更准确地澄清它。我需要的是一个可以生产不同类对象的工厂。所有这些类都应该是某个类的扩展(例如, ShepherdCorgi - 所有这些类都扩展 Dog )。作为最终结果,我实际上需要类似的东西:

public class DogService<T extend Dog> {

private DogFactory<T> dogFactory;

@Autowired
public DogService(DogFactory<T> dogFactory) {
this.dogFactory = dogFactory;
}

public T spawnDog(Color color) {
T dog = DogFactory.getObject(color);
return dog;
}

}

但我似乎无法用 FactoryBean 创建一个“通用”工厂接口(interface),所以我不能做这样的事情:

public class DogFactory<T extends Dog> implements FactoryBean<T> {
// ...
}

相反,我必须执行以下操作:

public class ShepherdFactory implements FactoryBean<Shepherd> {
// ...
}
public class CorgiFactory implements FactoryBean<Corgi> {
// ...
}

这是真的吗?

谢谢!

最佳答案

What I want is to instantiate this factory in another class

如果您的意思是“将工厂注入(inject)到类中,而不是它创建的 bean”,您可以通过在其名称前加上“&”符号来注入(inject)工厂:

public class ShepherdService {

private DogFactory<Shepherd> shepherdFactory;

@Qualifier("&dogFactory")
public ShepherdService(DogFactory<Shepherd> shepherdService) {
this.shepherdService = shepherdService;
}
}

来自 Spring 文档 (section 3.8.3):

When you need to ask a container for an actual FactoryBean instance itself, not the bean it produces, you preface the bean id with the ampersand symbol & (without quotes) when calling the getBean method of the ApplicationContext. So for a given FactoryBean with an id of myBean, invoking getBean("myBean") on the container returns the product of the FactoryBean, and invoking getBean("&myBean") returns the FactoryBean instance itself.

此外,我的猜测是 FactoryBean 没有被选择为 Spring bean,因为您的代码片段不包含 XML config 部分、Java config 部分或@Component 注释。我会在 @Configuration 带注释的类中显式声明工厂 bean,或者使用 @Component 注释工厂类。

[编辑]

But it seems that I can't make a "universal" factory with the FactoryBean interface, so I can't do something like that:

您仍然可以有一个 Configuration 类,在其中声明所有工厂 bean。

@Configuration
public class DogFactoryConfig {

@Bean
public DogFactory<Shepherd> shepherdFactory() {
return new DogFactory<Shepherd>();
}

@Bean
public DogFactory<Corgi> corgiFactory() {
return new DogFactory<Corgi>();
}
}

并从 DogFactory 中删除 @Component 注释(如果存在)。

关于Java Spring : getting the generic type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59599033/

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