프로그래밍/JAVA Spring

[Spring 스프링] Java기반 Configuration

hectick 2023. 5. 15. 15:26

 

 

Java 기반으로 빈을 등록할 수 있다.

    // Java-based Configuration을 하기 위한 클래스로 지정
    @Configuration
    public class AuthenticationPrincipalConfig {
    
     	// AuthService 빈을 등록
        @Bean
        public AuthService authService() {
            return new AuthService();
        }

        // AuthenticationPrincipalArgumentResolver를 빈 등록하고 authService에 대한 의존성을 주입
        @Bean
        public AuthenticationPrincipalArgumentResolver authenticationPrincipalArgumentResolver() {
            return new AuthenticationPrincipalArgumentResolver(authService());
        }
    }

 

Java기반 Configuration에는 @Configuration과 @Bean 어노테이션이 사용된다.

 

 

@Configuration 어노테이션이 달려 있는 클래스

 

해당 클래스의 목적이 빈을 정의하는 것임을 나타낸다.

 

@Bean 메서드는 같은 클래스 내의 다른 @Bean 메서드를 호출해서 빈 간의 의존성을 정의할 수도 있다. 이 방식은 @Configuration 클래스에서만 동작하므로, 다른 컴포넌트나 일반 클래스에서는 이와 같은 방식으로 빈 간의 의존성 선언이 불가하다.

	// AuthenticationPrincipalArgumentResolver를 빈 등록하고 authService에 대한 의존성을 주입
        @Bean
        public AuthenticationPrincipalArgumentResolver authenticationPrincipalArgumentResolver() {
            return new AuthenticationPrincipalArgumentResolver(authService());
        }

 

@Bean 어노테이션이 달려 있는 메서드

 

빈을 선언할 때 사용한다.

(xml기반 Configuration에서 <Bean> 태그와 동일한 역할을 한다.)

 

메서드의 반환값이 ApplicationContext에 빈으로 등록된다.

기본적으로 빈 이름은 메서드 이름과 동일하다.

    @Configuration
    public class AppConfig {

        @Bean
        public TransferServiceImpl transferService() {
            return new TransferServiceImpl();
        }
    }

 

 

+

@Bean을 @Configuration이 달려있는 클래스 내에 선언하면 "Full" 모드, @Configuration이 달려있지 않은 클래스(컴포넌트나 일반 클래스) 내에 선언하면 "Lite" 모드라 한다.

When @Bean methods are declared within classes that are not annotated with @Configuration, they are referred to as being processed in a “lite” mode. Bean methods declared in a @Component or even in a plain old class are considered to be “lite”, with a different primary purpose of the containing class and a @Bean method being a sort of bonus there. For example, service components may expose management views to the container through an additional @Bean method on each applicable component class. In such scenarios, @Bean methods are a general-purpose factory method mechanism.

Unlike full @Configuration, lite @Bean methods cannot declare inter-bean dependencies. Instead, they operate on their containing component’s internal state and, optionally, on arguments that they may declare. Such a @Bean method should therefore not invoke other @Bean methods. Each such method is literally only a factory method for a particular bean reference, without any special runtime semantics. The positive side-effect here is that no CGLIB subclassing has to be applied at runtime, so there are no limitations in terms of class design (that is, the containing class may be final and so forth).

In common scenarios, @Bean methods are to be declared within @Configuration classes, ensuring that “full” mode is always used and that cross-method references therefore get redirected to the container’s lifecycle management. This prevents the same @Bean method from accidentally being invoked through a regular Java call, which helps to reduce subtle bugs that can be hard to track down when operating in “lite” mode.