Giter VIP home page Giter VIP logo

spring-learning's Introduction

Ch 05. 구성속성 사용하기

5.1 자동-구성(auto Configuration) 세부 조정하기

spring 구성의 2가지

  • 빈 연결 (bean wiring) : ex) dataSouce, transaction manager, kafka listener ...
  • 속성 주입 (property injection) ex) db host, id, pw , ssl keystore , user custom properties (no hardcoding)

spring 에서는 대부분의 bean을 직접 지정 필요

  • spring boot 에서는 대부분 자동화 (starter), 수동 설정하여 override 가능

property 주입 방법

참고 ) https://velog.io/@ashappyasikonw/SPRING-BOOT-%EC%99%B8%EB%B6%80-%EC%84%A4%EC%A0%95-%EB%B0%8F-%EC%9A%B0%EC%84%A0%EC%88%9C%EC%9C%84-Externalized-Configuration-and-priorities

  • cli argument
  • ServletContext init parameter
  • JNDI attribute
  • java system properties (System.getProperties())
  • OS env param (EXPORT SPRING_ACTIVE_PROFILES=prod)

server.port=0 동작할까?

랜덤한 미사용 포트로 동작한다

micro 서비스 환경 혹은 test 시에 유용하다

로깅 구성 (without logback)

logging:
  path: /val/logs/
  file: TacoCloud.log
  level:
    root: warn
    org:
      springframework:
        security: debug

다른 속성의 값 가져오기

...
greeting:
  welcome: ${spring.application.name}

5.2 우리의 구성 속성 생성하기

taco:
  props:
    size: 10
    prize: 1000
    name: 존맛탱타코
@Component
@ConfigurationProperties(prefix="taco.props")
@Getter
public class TacoProps {
	private int size;

	@Min(value=5, message="must be between 5 and 5000")
	@Max(value=5000, message="must be between 5 and 5000")
	private long prize;
	private String name;
}

...

@Service
public class TacoServiceImpl {

    @AutoWired
    private TacoProps tacoProps;

	....

}

구성 메타데이터 선언

  • 없어도 동작에 이상 없지만, 구성속성 정보를 제공해주므로 개발시 편리하다
  • spring-boot-configuration-processor 의존성 추가
ex)src/main/resources/META-INF/additional-spring-configuration-metadata.json
{
	properties: [
		{
			name: "taco.orders.page-size",
			type: "int",
			description: "user custom properties meta into sample"
		}
	]
}

5.3 프로파일로 구성하기

logging:
  level:
    root: warn

---
spring:
  profiles: prod
  datesource:
    url: jdbc:mysql://10.10.20.30/prod-db

---
spring:
  profiles: dev
  datasource:
    url: jdbc:mysql://10.100.200.100/dev-db

java -jar app.jar --spring.profiles,active=prod

or

export SPRING_PROFILES_ACTIVE=prod

프로파일로 빈 구분하기

@Bean
@Profile("dev")
public DevDataSourceConfiguration { 
...
}

@Bean
@Profile("prod")
public ProdDataSourceConfiguration { 
...
}

@Bean
@Profile("!prod")
public NotProdDataSourceConfiguration { 
...
}

spring-learning's People

Contributors

swsw1005 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.