不能将autowire bean注入我的班级

时间:2016-02-08 02:44:43

标签: java spring spring-boot

我定义了一个Spring bean,但是当我为自动装配另一个RestClient rest bean添加最后4行时,我收到错误(如果我删除最后3行,我的spring容器加载正常):

@Component
public class TimeseriesServiceImpl {

    private static Logger log = Logger.getLogger(TimeseriesServiceImpl.class);

    @PostConstruct
    public void init() {
        System.out.println("MyService init method called");
    }

    @PreDestroy
    public void destory(){
        System.out.println("MyService destroy method called");
    }

    @Autowired
    @Qualifier("restClient")  
    public RestClient rest ;

错误:

2016-02-07 18:46:41.609  WARN 11084 --- [main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'timeseriesServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.ge.predix.solsvc.restclient.impl.RestClient com.tcs.timeseries.service.TimeseriesServiceImpl.rest; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ge.predix.solsvc.restclient.impl.RestClient] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=restClient)}
2016-02-07 18:46:41.612  INFO 11084 --- [main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2016-02-07 18:46:41.623 ERROR 11084 --- [main] o.s.boot.SpringApplication               : Application startup failed

我正在使用maven spring boot应用程序

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.tcs.timeseries.*","com.ge.predix.solsvc.restclient.config.*", "com.ge.predix.solsvc.restclient.impl.*"})
@PropertySource("classpath:application-default.properties")
@ContextConfiguration(locations = { 
        "classpath*:META-INF/spring/application-context.xml"         
        })
public class TimeseriesclientApplication {

    private static final Logger log = LoggerFactory.getLogger(TimeseriesclientApplication.class);

    public static void main(String[] args) {
        //SpringApplication.run(TimeseriesclientApplication.class, args);

            SpringApplication springApplication = new SpringApplication(TimeseriesclientApplication.class);
            ApplicationContext ctx = springApplication.run(args);

            log.info("Let's inspect the beans provided by Spring Boot:");
            String[] beanNames = ctx.getBeanDefinitionNames();


            Arrays.sort(beanNames);
            for (String beanName : beanNames)
            {
                log.info(beanName);
            }

    }

RestClientImpl bean的详细信息:

@Component(value = "restClient")
public class RestClientImpl
        implements RestClient, ApplicationContextAware
{

    private static Logger                  log                  = LoggerFactory.getLogger(RestClientImpl.class);

    /**
     * 
     */
    @Autowired
    protected IOauthRestConfig             restConfig;

    private javax.net.ssl.SSLSocketFactory sslSocketFactory;

    private SSLContext                     sslContext;

    private ApplicationContext             applicationContext;

    static private final ObjectMapper      mapper               = new ObjectMapper()
                                                                        .configure(
                                                                                DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
                                                                                false).setSerializationInclusion(
                                                                                JsonSerialize.Inclusion.NON_NULL);
    /**
     * 
     */
    static final String                    DEFAULT_CONTENT_TYPE = "application/json";                             //$NON-NLS-1$


    private PoolingHttpClientConnectionManager poolManager;

    /**
     * 
     */
    public RestClientImpl()
    {
        super();
    }

    @PostConstruct
    private void init()
    {
        setupSecureContext(this.restConfig.getOauthCertLocation(), this.restConfig.getOauthCertPassword());
        poolManager = new PoolingHttpClientConnectionManager();
        if(poolManager !=null ) {
            poolManager.setMaxTotal(this.restConfig.getOauthPoolMaxSize());
            poolManager.setDefaultMaxPerRoute(this.restConfig.getOauthPoolMaxSize());
        }



}

2 个答案:

答案 0 :(得分:0)

*注释中指定basePackages属性时,请勿包含@ComponentScan

所以用下面的代码替换它 -

@ComponentScan(basePackages={"com.tcs.timeseries","com.ge.predix.solsvc.restclient.config", "com.ge.predix.solsvc.restclient.impl"})

这将正确扫描包和子包。

答案 1 :(得分:0)

问题在于配置文件声明,在属性文件中我需要提及为本地。在config文件夹中创建application.properties文件并将此行放入spring.profiles.active = local之后,它能够毫无问题地注入RestClient bean