Spring启动自动装配在Configuration类中不起作用

时间:2015-11-11 00:24:01

标签: spring spring-boot

我有一个Spring Boot应用程序,它使用Redis服务器进行缓存。我使用Spring RedisTemplate连接到Redis服务器。我在@Confiuration类中配置Redis参数。但是,redis url和port存储在DB中,相应的DAO作为另一个类(也包含其他全局信息)的成员包含在内。我正在尝试在Configuration类中自动装配父类,但是我收到错误。这是我的代码:

@Configuration
public class MyConfiguration {
    @Autowired
    protected GlobalPropertiesLoader globalPropertiesLoader;

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        GlobalPropertiesDAO globalPropertiesDAO = globalPropertiesLoader.getGlobalProperties();

        factory.setHostName(globalPropertiesDAO.getRedisUrl());
        factory.setPort(globalPropertiesDAO.getRedisPort());
        factory.setUsePool(true);
        return factory;
    }

    @Bean
    RedisTemplate< String, Object > redisTemplate() {
        final RedisTemplate< String, Object > template =  new RedisTemplate< String, Object >();
        template.setConnectionFactory( jedisConnectionFactory() );
        template.setKeySerializer( new StringRedisSerializer() );
        template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        return template;
    }
}

GlobalPropertiesLoader是包含DAO(GlobalPropertiesDAO)作为对象的类。当我尝试运行我的应用程序时,出现以下错误:

Exception in thread "main" 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mypkg.CommonsConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.utils.GlobalPropertiesLoader com.mypkg.CommonsConfiguration.globalPropertiesLoader; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.utils.GlobalPropertiesLoader] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 50 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.utils.GlobalPropertiesLoader com.mypkg.CommonsConfiguration.globalPropertiesLoader; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.utils.GlobalPropertiesLoader] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 61 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.utils.GlobalPropertiesLoader] 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)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)

这是什么问题?是否无法在@Configuration课程中进行自动装配?因为我能够在其他类中自动装配相同的类。

::::::::::::::::::::::编辑::::::::::::::::::::::

我尝试@Import如下:

@Configuration
@Import({GlobalPropertiesLoader.class})
public class CommonsConfiguration {
    @Autowired
    GlobalPropertiesLoader globalPropertiesLoader;
    ....
}

然而,当我这样做时,我收到以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.utils.GlobalPropertiesLoader': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.persistence.repositories.GlobalPropertiesRepository com.utils.GlobalPropertiesLoader.globalPropertiesRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.persistence.repositories.GlobalPropertiesRepository] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 63 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.persistence.repositories.GlobalPropertiesRepository com.utils.GlobalPropertiesLoader.globalPropertiesRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.persistence.repositories.GlobalPropertiesRepository] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 74 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.persistence.repositories.GlobalPropertiesRepository] 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)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)

这是GlobalPropertiesLoader类:

@Component
@Scope("singleton")
public class GlobalPropertiesLoader {
    @Autowired
    public GlobalPropertiesRepository globalPropertiesRepository;
    private GlobalPropertiesDAO globalProperties;
/*
     * Load GlobalProperties once upon loading the context.
     */
    @PostConstruct
    public void init(){
        globalProperties = globalPropertiesRepository.findOne(1L);
        ....
    }
}

GlobalPropertiesRepository java:

@Repository
public interface GlobalPropertiesRepository extends CrudRepository<GlobalPropertiesDAO, Long>{      
}

感谢。

4 个答案:

答案 0 :(得分:12)

@Import 是将配置类添加到另一个配置类中。

http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch04s03.html

@ComponentScan 是扫描代码中声明的组件,如@ Service,@ Component,@ Repository等。

http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06s02.html

我认为您需要在配置类中添加@ComponentScan,以便它可以使用您的组件类扫描包。

@Configuration
@ComponentScan(value = "org.foo.path.baseFolder")
public class MyConfiguration {
    @Autowired
    protected GlobalPropertiesLoader globalPropertiesLoader;

答案 1 :(得分:0)

@Configuration是类,它们将首先尝试启动。当GlobalPropertiesLoader启动时,MyConfiguration没有它的实例。试试这个。

@Configuration
@Import({GlobalPropertiesLoader.class})
public class MyConfiguration {
}

答案 2 :(得分:0)

此错误的原因之一是您没有此类的实现。创建一个名为CustomUserDetailsS​​ervice的类,实现UserDetailsS​​ervice并使用@Component注释它。 有关更多信息,请参阅spring文档。或遵循@reos引用的解决方案

答案 3 :(得分:0)

之前我遇到了同样的问题,我的初始类“ Application.class”位于“ abc.klm.prq”之类的软件包中,而其他组件的软件包名称均以“ abc.klm.xyz”开头。因此,组件类不在“ Application.class”的下面的层次结构中。当我将组件类移动到“ abc.klm.prq。*”程序包时,它将起作用。