[SPRING] 스프링 부트 Autowired가 Configuration 클래스에서 작동하지 않습니다.
SPRING스프링 부트 Autowired가 Configuration 클래스에서 작동하지 않습니다.
캐싱을 위해 Redis 서버를 사용하는 Spring Boot 애플리케이션이 있습니다. Redis 서버에 연결하기 위해 Spring RedisTemplate을 사용합니다. @Confiuration 클래스에서 Redis 매개 변수를 구성합니다. 그러나 redis url 및 port는 DB에 저장되고 해당 DAO는 다른 클래스의 멤버로 포함됩니다 (다른 전역 정보로드도 포함). Configuration 클래스에서 부모 클래스를 autowire하려고하지만 오류가 발생합니다. 이것은 내가 가지고있는 코드이다.
@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 클래스에서 Autowire 할 수 있습니까? 다른 클래스에서 같은 클래스를 자동으로 호출 할 수 있기 때문입니다.
::::::::::::::::::::: 편집하다 ::::::::::::::::::::
나는 아래에 @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>{
}
감사.
해결법
-
==============================
1.@Import는 다른 구성 클래스에 구성 클래스를 추가하는 것입니다.
@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;
-
==============================
2.@Configurations는 먼저 시작하려고하는 클래스입니다. MyConfiguration이 시작되면 GlobalPropertiesLoader에 인스턴스가 없습니다. 이 시도.
@Configurations는 먼저 시작하려고하는 클래스입니다. MyConfiguration이 시작되면 GlobalPropertiesLoader에 인스턴스가 없습니다. 이 시도.
@Configuration @Import({GlobalPropertiesLoader.class}) public class MyConfiguration { }
-
==============================
3.이 오류의 원인 중 하나는이 클래스를 구현하지 않았기 때문입니다. UserDetailsService를 구현하는 CustomUserDetailsService라는 클래스를 만들고 @Component로 주석을 만듭니다. 자세한 것은 스프링 문서를 참조하십시오. @reos가 인용 한 솔루션을 따르십시오.
이 오류의 원인 중 하나는이 클래스를 구현하지 않았기 때문입니다. UserDetailsService를 구현하는 CustomUserDetailsService라는 클래스를 만들고 @Component로 주석을 만듭니다. 자세한 것은 스프링 문서를 참조하십시오. @reos가 인용 한 솔루션을 따르십시오.
from https://stackoverflow.com/questions/33642199/spring-boot-autowired-not-working-in-configuration-class by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] SessionRegistry로 사용자 로그인하기 (0) | 2019.04.10 |
---|---|
[SPRING] 봄 부팅로드 orm.xml (0) | 2019.04.10 |
[SPRING] 모든 프로 바이더의 후에 Spring Security java.lang.StackOverflowError 예외 (0) | 2019.04.10 |
[SPRING] 사용자 정의 메소드 보안 표현식을 작성하는 가장 좋은 방법 (0) | 2019.04.10 |
[SPRING] 나머지 컨트롤러 vs spring-data-rest RepositoryRestResource (0) | 2019.04.10 |