-
카테고리
-
세부 분야
백엔드
-
해결 여부
미해결
@Bean, @Componet 질문
22.11.21 12:27 작성 조회수 188
0
@Component
FormAuthenticationDetailsSource
의 경우 .java
소스파일에 @Component
를 명시했습니다.
@Component
public class FormAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> {
@Override
public WebAuthenticationDetails buildDetails(HttpServletRequest request) {
return new FormWebAuthenticationDetails(request);
}
}
따라서 securityConfig설정클래스의 멤버변수에 @Autowired
로 의존관계설정이 가능하였고 이 것을 아래 코드에서 사용됐습니다.
.formLogin()
.loginPage("/login") //로그인 페이지의 GET 요청 엔드포인트
.loginProcessingUrl("/login_proc") //로그인 시도의 POST요청 엔드포인트
.defaultSuccessUrl("/") //로그인 성공후 루트페이지로 이동
.authenticationDetailsSource(authenticationDetailsSource)
@Bean
SecurityConfig에 메소드를 정의하고 @Bean
을 설정하는 경우
참고로, 5) 인증 및 인가 예외 처리 - AjaxLoginUrlAuthenticationEntryPoint
강의의 08:49에서는 @Bean
없이 메소드를 정의했습니다.
.and()
.exceptionHandling()
.accessDeniedHandler(ajaxAccessDeniedHandler())
...
public AccessDeniedHandler ajaxAccessDeniedHandler(){
return new AjaxAccessDeniedHandler();
}
new 생성자() 호출하는 경우
.and()
.exceptionHandling()
.authenticationEntryPoint(new AjaxLoginAuthenticationEntryPoint())
위 세 가지 케이스로 패턴을 정리할 수 있었는데 공통점은 IoC컨테이너에 빈객체를 생성한다는 점입니다. 차이점은 어떤 것이 있는지 알고싶습니다.
답변을 작성해보세요.
1
정수원
지식공유자2022.11.21
@Component 나 @Bean 을 설정하는 것은 빈을 정의하는 방식의 차이가 있을 뿐 결과는 동일합니다.
그리고 빈을 정의하지 않고 new 를 사용해서 일반 객체로 생성하는 것은 스프링의 빈으로 생성해야 할 특별한 이유가 없다면 (가령 DI 기능을 사용하지 않는다거나 빈의 라이프 사이클에 관여할 필요가 없을 경우 등..) 문제될 것은 없습니다.
오히려 일반객체로 생성하는 것이 더 가볍고 의존관계가 적을 수 있습니다.
제가 강의에 빈을 생성하거나 일반 객체로 생성하는 부분은 참조만 해 주시고 상황에 따라 반드시 빈으로 생성해야 하는 경우와 그렇지 않은 경우를 판단하셔서 구현하시면 됩니다.
답변 1