복붙노트

[SPRING] @Bean 메소드에`@ConfigurationProperties` 주석 사용하기

SPRING

@Bean 메소드에`@ConfigurationProperties` 주석 사용하기

누군가가 @Bean 메서드에서 직접 @ConfigurationProperties 주석을 사용하는 방법에 대한 MWE를 줄 수 있습니까?

클래스 정의에 사용되는 무수한 예제를 보았습니다. 그러나 @Bean 메소드의 예제는 아직 없습니다.

문서의 인용 :

그래서 가능성과 의도 된 용도가 있다고 생각합니다. 그러나 불행하게도 나는 그것을 파악할 수 없습니다.

해결법

  1. ==============================

    1.

    spring.datasource.url = [url]
    spring.datasource.username = [username]
    spring.datasource.password = [password]
    spring.datasource.driverClassName = oracle.jdbc.OracleDriver
    
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        return new DataSource();
    }
    

    여기 DataSource 클래스는 url, username, password, driverClassName 속성을 갖기 때문에 spring 부트는 생성 된 객체에이를 매핑합니다.

    DataSource 클래스의 예 :

        public class DataSource {
            private String url;
            private String driverClassName;
            private String username;
            private String password;
            //getters & setters, etc.
        }
    

    즉, 스테레오 타입 어노테이션 (@Component, @Service 등)을 사용하여 일부 bean을 초기화하는 경우와 동일한 효과가 있습니다. 예 :

    @Component
    @ConfigurationProperties(prefix="spring.datasource")
    public class DataSource {
                private String url;
                private String driverClassName;
                private String username;
                private String password;
                //getters & setters, etc.
            }
    
  2. ==============================

    2.24.8.1 타사 구성

    24.8.1 타사 구성

    @ConfigurationProperties를 사용하여 클래스에 주석을 추가하는 것은 물론 public @Bean 메소드에서도 사용할 수 있습니다. 이렇게하면 컨트롤 외부에있는 타사 구성 요소에 속성을 바인딩하려는 경우 특히 유용 할 수 있습니다.

    환경 등록 정보에서 bean을 구성하려면 다음 예와 같이 Bean 등록에 @ConfigurationProperties를 추가하십시오.

    @ConfigurationProperties(prefix = "another")
    @Bean
    public AnotherComponent anotherComponent() {
        ...
    }
    

    다른 접두어로 정의 된 특성은 이전 AcmeProperties 예제와 유사한 방식으로 해당 AnotherComponent Bean에 맵핑됩니다.

  3. ==============================

    3.아래와 같이 @ConfigurationProperties를 사용할 수 있습니다.

    아래와 같이 @ConfigurationProperties를 사용할 수 있습니다.

    엔티티 모델

    public class MY_ENTITY {
        private String prop1;
        private String prop2;
        // setter & getter & toString()
    }
    

    콩 방법

    @Configuration
    public class MyClass {
    
        @Bean
        @ConfigurationProperties(prefix = "my.entity")
        public MY_ENTITY getContract() {
            return new MY_ENTITY()
                    .setProp1("prop1111111")
                    .setProp2("prop2222222")
                    ;
        }
    
        @Bean(name = "contract2")
        @ConfigurationProperties(prefix = "my.entity2")
        public MY_ENTITY getContract2() {
            return new MY_ENTITY()
                    .setProp1("prop1111.2222")
                    .setProp2("prop2222.222")
                    ;
        }
    }
    

    application.properties

    my.entity.prop1=2120180023
    my.entity.prop2=CUSTOMER_NAME111
    
    my.entity2.prop1=9994494949
    my.entity2.prop2=CUSTOMER_NAME222
    

    SpringBootApplication

    @SpringBootApplication
    public class DemoApplication implements CommandLineRunner {
    
        @Autowired
        @Qualifier("contract2")
        private MY_ENTITY myEntity;
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println(myEntity);
        }
    }
    
  4. from https://stackoverflow.com/questions/43232021/using-configurationproperties-annotation-on-bean-method by cc-by-sa and MIT license