UnsatisfiedDependency
반응형
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
발생 시 해결 방법
[Error 로그]
Parameter 2 of constructor in us.puter.park.api.MenuController required a bean of type 'us.puter.park.common.security.auth.UrlFilterInvocationSecurityMetadataSource' that could not be found.
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'menuController' defined in file [D:\workspace\park\build\classes\java\main\us\puter\park\api\MenuController.class]:
Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'us.puter.park.common.security.auth.UrlFilterInvocationSecurityMetadataSource' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'us.puter.park.common.security.auth.UrlFilterInvocationSecurityMetadataSource' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
[현황]
1. 아래 코드에서 `UrlFilterInvocationSecurityMetadataSource`을 주입함
@RestController
@RequestMapping("/api/common/menu")
@RequiredArgsConstructor
public class MenuController {
private final MenuService menuService;
private final AuthService authService;
private final UrlFilterInvocationSecurityMetadataSource urlFilterInvocationSecurityMetadataSource;
...
}
2. `UrlFilterInvocationSecurityMetadataSource`은 `FilterInvocationSecurityMetadataSource` 상속 받으며, 어노테이션을 통한 주입을 하지 않음
public class UrlFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
private final LinkedHashMap<RequestMatcher, List<ConfigAttribute>> requestMap;
private final AuthService authService;
public UrlFilterInvocationSecurityMetadataSource(LinkedHashMap<RequestMatcher, List<ConfigAttribute>> requestMap,
AuthService authService) {
this.requestMap = requestMap;
this.authService = authService;
}
...
}
3. `SecurityConfig`에서 `@Bean`을 통하여 주입함
> 문제는 `FilterInvocationSecurityMetadataSource` 자료형으로 주입함
> 문제는 `FilterInvocationSecurityMetadataSource` 자료형으로 주입함
@Bean
public FilterInvocationSecurityMetadataSource urlFilterInvocationSecurityMetadataSource() throws Exception {
return new UrlFilterInvocationSecurityMetadataSource(urlResourcesMapFactoryBean().getObject(), authService);
}
private UrlResourcesMapFactoryBean urlResourcesMapFactoryBean() {
UrlResourcesMapFactoryBean urlResourcesMapFactoryBean = new UrlResourcesMapFactoryBean();
urlResourcesMapFactoryBean.setAuthService(authService);
return urlResourcesMapFactoryBean;
}
[해결]
`@Bean` 설정의 자료형을 `UrlFilterInvocationSecurityMetadataSource`로 변경
@Bean
public FilterInvocationSecurityMetadataSource urlFilterInvocationSecurityMetadataSource() throws Exception {
return new UrlFilterInvocationSecurityMetadataSource(urlResourcesMapFactoryBean().getObject(), authService);
}
▼▼▼▼▼▼▼▼▼▼
@Bean
public UrlFilterInvocationSecurityMetadataSource urlFilterInvocationSecurityMetadataSource() throws Exception {
return new UrlFilterInvocationSecurityMetadataSource(urlResourcesMapFactoryBean().getObject(), authService);
}
- 해당 오류는 `@Autowired`로 선언된 객체인 `UrlFilterInvocationSecurityMetadataSource` 타입의 빈으로 생성된 객체가 하나도 존재하지 않아 발생함
- `@Bean`, `@Component`, `@Service`과 같은 어노테이션을 선언해서 해당 객체가 빈으로 생성되도록 하거나 스프링에서 지원하는 빈생성 클래스인 `FactoryBean`을 사용해서 빈으로 생성하는 로직을 구현해야 함
- 즉, `@Autowired`된 빈을 찾고 있는데 해당 객체가 빈으로 생성되도록 선언된 구문이 없기 때문에 발생하는 오류
반응형
'development' 카테고리의 다른 글
PKIX path building failed (0) | 2023.05.01 |
---|---|
centos8에서 mariadb 설치 rpm 문제 (0) | 2023.05.01 |
WebSocket @Autowired (0) | 2022.09.14 |
lombok annotation (0) | 2022.09.12 |
spring boot transaction (0) | 2022.09.08 |
댓글
이 글 공유하기
다른 글
-
PKIX path building failed
PKIX path building failed
2023.05.01 -
centos8에서 mariadb 설치 rpm 문제
centos8에서 mariadb 설치 rpm 문제
2023.05.01 -
WebSocket @Autowired
WebSocket @Autowired
2022.09.14 -
lombok annotation
lombok annotation
2022.09.12