简体   繁体   English

如何自动装配春季课的豆子

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM