정구리의 우주정복

[Spring] Autoconfiguration 만들어보기 본문

JAVA/STUDY

[Spring] Autoconfiguration 만들어보기

Jungry_ 2023. 7. 9. 21:22
반응형

Autoconfiguration 은 개발자가 명시적으로 설정을 작성하지 않아도 자동으로 구성되는 기능이다 ,필요한 Bean 을 자동으로 생성한다

예를들어 DataSource 같은게 있다. DataSource 사용 시 getConnect() 등등 하나하나 만들어주지 않아도 사용이 가능하다. 암튼 그런게 Autoconfiguration 임 

 

Autoconfiguration 원리 

 

 

/global/config/properties/PropertiesConfig

package com.example.jungry.global.config.properties;

import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationPropertiesScan(basePackages = "com.example.jungry.global.properties")
public class PropertiesConfig {
}

@Configuration 을 사용해서 Bean 으로 생성해준다

@ConfigurationPropertiesScan 을 사용해서 해당 경로의 @ConfigurationProperties 어노테이션이 붙은 클래스들을 스캔하도록 한다

 

해당 내용은 com.example.jungry.global.properties 안에 있는 @ConfigurationProperties 이 붙은 애들을 스캔해준다는 뜻이당

 

/global/properties/JungryProperties

package com.example.jungry.global.properties;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
// ConstructorBinding 쓰면 Setter 안쓰고 생성자로 주입 받겠다고 걍 라벨달아주는거임

@Getter
@ConstructorBinding
@RequiredArgsConstructor
@ConfigurationProperties(prefix = "jungry.test")
public class JungryProperties {
    private final String name;
    private final Integer age;
    private final String address;
}

@ConstructorBinding : 생성자 주입을 사용할 것임을 나타냄 , final 사용해서 선언되어야함 

@ConfigurationProperties(prefix = "") : 외부 설정 파일에서 바인딩할 프리픽스를 지정한다 지금은 jungry.test 라고 해줬음

 

application.yaml

jungry:
  test:
    name: 'jwseo'
    age: 25
    address: 'paju'

yaml 에 jungry.test 에 대한 내용을 적어준다

 

/global/config/jungry/JungryConfig.java

package com.example.jungry.global.config.jungry;

import com.example.jungry.global.properties.JungryProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
@Slf4j
public class JungryConfig {

    // 얘 가져오기
    private final JungryProperties jungryProperties;

    @Bean
    public String initializeJungry() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(jungryProperties.getName())
                .append(jungryProperties.getAddress())
                .append(jungryProperties.getAge());

        log.info("Test value: {}", stringBuilder);

        return stringBuilder.toString();
    }
}

이렇게 까지 해주면 AutoConfiguration 등록이 된당

spring 실행해주면

log에 찍혀있는거 볼 수 있다

와 ! 신기해 !

반응형

'JAVA > STUDY' 카테고리의 다른 글

[Spring] 라이브러리  (1) 2024.02.03
[Spring] Datasource PostgreSql 접속하기  (0) 2023.07.09
[IntelliJ] 단축키 (Mac)  (0) 2023.07.05
Mac mysql , Workbench download  (0) 2022.02.21
[JAVA] 객체지향 프로그래밍 (1)  (0) 2022.02.04
Comments