Spring Boot Autowired在配置类中不起作用
我有一个Spring Boot应用程序,它使用Redis服务器进行缓存.我使用Spring RedisTemplate连接到Redis服务器.我在@Confiuration
类中配置Redis参数.但是,redis URL和端口存储在DB中,并且相应的DAO作为成员包含在另一个类中(该类还包含其他全局信息的负载).我正在尝试自动连接Configuration类中的父类,但出现错误.这是我的代码:
I have a Spring Boot application which uses a Redis server for caching. I use Spring RedisTemplate for connecting to the Redis server. I configure the Redis parameters in a @Confiuration
class. However, the redis url and port are stored in DB, and the corresponding DAO is contained as a member in another class (which also contains loads of other global information). I'm trying to autowire the parent class in the Configuration class, but I get an error. This is the code I have:
@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)作为对象的类.当我尝试运行我的应用程序时,出现以下错误:
GlobalPropertiesLoader is the class which contains the DAO (GlobalPropertiesDAO) as an object. When I try to run my application, I get the following error:
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
类中无法自动接线吗?因为我能够自动将其他班级的同一个班级连接起来.
What is the problem here? Is it not possible to Autowire in @Configuration
class? Because I'm able to autowire the same class in other classes.
:::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::: EDIT ::::::::::::::::::::
我尝试了@Import
,如下所示:
@Configuration
@Import({GlobalPropertiesLoader.class})
public class CommonsConfiguration {
@Autowired
GlobalPropertiesLoader globalPropertiesLoader;
....
}
但是,当我这样做时,出现以下错误:
However, when I do this, I get the following error:
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类:
This is the GlobalPropertiesLoader class:
@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:
And the GlobalPropertiesRepository java:
@Repository
public interface GlobalPropertiesRepository extends CrudRepository<GlobalPropertiesDAO, Long>{
}
谢谢.
@Import 是将配置类添加到另一个配置类中.
@Import is to add a configuration class into another configuration class.
http://docs. spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch04s03.html
@ComponentScan 用于扫描代码中声明的组件,例如@ Service,@ Component,@ Repository等.
@ComponentScan is to scan for components declared in your code, like @Service, @Component, @Repository, among others.
http://docs. spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06s02.html
我认为您需要在配置类中添加@ComponentScan,以便它可以使用您的组件类扫描软件包.
I think you need to add in your configuration class the @ComponentScan so it can scan the package with your component classes.
@Configuration
@ComponentScan(value = "org.foo.path.baseFolder")
public class MyConfiguration {
@Autowired
protected GlobalPropertiesLoader globalPropertiesLoader;