我有一个 spring bean 类,其构造函数具有多个参数和 @Inject 注释。
有没有办法使用 spring Java 配置类为该类创建 bean,而无需实际编写创建对象的代码?比如在字段上使用@Bean?
@Bean(MyClassName.class) private MyInterfaceName myBean;
或者也许通过使配置类抽象和 bean 方法抽象,例如:
@Bean(MyClassName.class) abstract MyInterfaceName myBean();
如果您知道自己只有该类的 1 个实现并且想要使用 Autowiring 和构造函数注入(inject),那么每次编写仅创建一个新对象的整个方法是非常烦人的(而且毫无意义)。
最佳答案
您可以使用@Component
注释。根据 Spring 文档:
@Component indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
关于java - 使用Java配置和构造函数注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271331/