[英]How to autowired bean of spring class
I can have create bean with explicit bean factory method. 我可以使用显式bean工厂方法创建bean。
package org.package;
import org.springframework.security.web.PortResolver;
import org.springframework.security.web.PortResolverImpl;
@Configuration
public Configuration {
@Bean
public PortResolver portResolver(){
return new PortResolverImpl();
}
}
My goal is avoid factory method. 我的目标是避免工厂方法。
If you don't want a factory method, let Spring instantiate an instance of your class for you by annotating your class with @Component
and make your @Configuration
class @ComponentScan
its package. 如果您不需要工厂方法,请让Spring通过使用
@Component
注释您的类来实例化您的类的实例,并使@Configuration
类@ComponentScan
其包。
When Spring scans that package, it will find your class, use its default constructor (or constructor annotated with @Inject
or @Autowired
) and use it to make an instance of your class. 当Spring扫描该包时,它将找到您的类,使用其默认构造函数(或使用
@Inject
或@Autowired
注释的构造函数)并使用它来创建类的实例。 Spring will then add that instance to its context. 然后Spring会将该实例添加到其上下文中。
Because PortResolveImpl
is not under your control, you will need to use a @Bean
factory method. 由于
PortResolveImpl
不在您的控制之下,因此您需要使用@Bean
工厂方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.