it-source

Spring Context @Configuration에서 void 설정 방법 실행

criticalcode 2023. 6. 20. 21:41
반응형

Spring Context @Configuration에서 void 설정 방법 실행

Spring Context 내에서 몇 가지 설정 방법을 수행하고자 합니다.

나는 현재 다음 코드를 가지고 있지만 내가 말하는 것처럼 작동하지 않습니다.beans반환 유형이 없습니다.

@Configuration
@Component
public class MyServerContext {

    ...

    // Works
    @Bean
    public UserData userData() {
        UserData userData = new AWSUserDataFetcher(urlUtil()).fetchUserData();
        return userData;
    }   

    // Doesn't work
    @Bean
    public void setupKeyTrustStores() {
        // Setup TrustStore & KeyStore
        System.setProperty(SYS_TRUST_STORE, userData().get(TRUST_STORE_PATH));
        System.setProperty(SYS_TRUST_STORE_PASSWORD, userData().get(TRUST_STORE_PASSWORD));
        System.setProperty(SYS_KEY_STORE, userData().get(KEY_STORE_PATH));
        System.setProperty(SYS_KEY_STORE_PASSWORD, userData().get(KEY_STORE_PASSWORD));

        // Prevents handshake alert: unrecognized_name
        System.setProperty(ENABLE_SNI_EXTENSION, "false");
    }

    ...

}

어떻게 하면 자동으로 이 메서드를 실행할 수 있습니까?@Configuration없는 컨텍스트@Bean주석?

사용할 수 있습니다.@PostConstruct대신 주석 표시@Bean:

@Configuration
@Component
public class MyServerContext {

    @Autowired
    private UserData userData; // autowire the result of userData() bean method

    @Bean
    public UserData userData() {
        UserData userData = new AWSUserDataFetcher(urlUtil()).fetchUserData();
        return userData;
    }   

    @PostConstruct
    public void setupKeyTrustStores() {
        // Setup TrustStore & KeyStore
        System.setProperty(SYS_TRUST_STORE, userData.get(TRUST_STORE_PATH));
        System.setProperty(SYS_TRUST_STORE_PASSWORD, userData.get(TRUST_STORE_PASSWORD));
        System.setProperty(SYS_KEY_STORE, userData.get(KEY_STORE_PATH));
        System.setProperty(SYS_KEY_STORE_PASSWORD, userData.get(KEY_STORE_PASSWORD));

        // Prevents handshake alert: unrecognized_name
        System.setProperty(ENABLE_SNI_EXTENSION, "false");
    }

    ...

}

@bean 대신 @PostCruct 사용

@포스트 컨스트럭트

용접 기준 주입 및 초기화는 다음 순서로 이루어집니다.

  1. 먼저, 컨테이너는 빈의 인스턴스를 얻기 위해 빈 생성자(기본 생성자 또는 주석이 달린 @Inject)를 호출합니다.
  2. 그런 다음 용기는 콩의 모든 주입 필드 값을 초기화합니다.
  3. 다음으로, 컨테이너는 빈의 모든 이니셜라이저 메서드를 호출합니다(호출 순서는 휴대용이 아니므로 의존하지 마십시오).
  4. 마지막으로,@PostConstruct메서드가 있으면 호출됩니다.

그래서, 사용 목적은.@PostConstruct주입된 콩, 리소스 등을 초기화할 수 있는 기회를 제공합니다.

public class Person {

    // you may have injected beans, resources etc.

    public Person() {
        System.out.println("Constructor is called...");
    }

    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct is called...");
    } }

따라서, 사람 콩을 주입하면 다음과 같은 결과를 얻을 수 있습니다.

생성자 이름은...

@PostCruct를...라고 부릅니다.

한 가지 중요한 점은@PostConstruct즉, 생산자 방법을 통해 콩을 주입하고 초기화하려고 하면 호출되지 않습니다.생산자 방법을 사용하면 콩에 새 키워드를 프로그래밍 방식으로 만들고, 초기화하고, 주입할 수 있기 때문입니다.

리소스 링크:

  1. CDI 종속성 주입 @PostCruct 및 @PreDestroy 예제
  2. @PostCruct를 사용하는 이유는 무엇입니까?

언급URL : https://stackoverflow.com/questions/38957851/run-a-void-setup-method-in-spring-context-configuration

반응형