레알윙 2020. 4. 14. 09:04
반응형

백기선님이 자기가나오는 화면이 크다고 자책하시면서 작게 하셨다.

 

예전부터 느꼈던건데.. 수정해주시다니 감사할따름이다.

 


프로파일

이것을 보기 전 전에 정리한 내용을 한 번 돌아보자

2020/03/17 - [백기선 Spring/스프링 프레임워크 핵심 기술] - IoC 컨테이너와 빈 (3) - Enviornment - 프로파일

 

 

특정 프로파일에만 Bean을 등록하고 싶거나, 애플리케이션의 동작특정 프로파일일 때만 동작할 때 사용

 

@Configuration이란

어노테이션기반 환경구성을 도운다., 클래스가 하나이상의 @Bean 메소드를 제공하고 스프링 컨테이너가 Bean정의를 생성하고 런타임시 그 Bean들의 요청을 처리할 것을 선언하게 된다. 

 

spring.profiles.active(프로파일 활성화)

 

위치

 

각각 프로파일prod, test일 때만 사용하고 아닐때는 사용하지 않는다.

@Configuration
@Profile("prod")
public class BaseConfiguration {
	@Bean
	public String hello() {
		return "hello jinSeok Base";
	}
}

 

@Configuration
@Profile("test")
public class TestConfiguration {
	@Bean
	public String hello() {
		return "hello jinSeok Test";
	}
}

 

위 상태로 hello를 주입받아 사용하면 error가 발생이된다.

 

@Component
public class SampleRunner implements ApplicationRunner{

	@Autowired
	private String hello;
	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("=====================================");
		System.out.println(hello);
		System.out.println("=====================================");
	}
}

 

error

 

위의같은 오류가 발생되는 이유는 spring.profiles를 등록을 안했기 때문인데 아래와같이 application.properties에서 등록을 하면 된다. 결국 application.properties에서 사용한 spring.profile.active도 프로퍼티이다.

 

 

등록화면
결과

 

application-{profile}.properties (프로파일용 프로퍼티)

프로퍼티 파일의 우선순위가 기본 application.properties보다 높다.

 

java -jar spring-boot-practice-1.0-SNAPSHOT.jar --spring.profiles.active=prod명령어로 터미널에서 실행해보면 프로파일이 prod로 설정되어있으서 로그도 그렇게 찍힌다.

application.properties
application-prod-properties

@Component
@ConfigurationProperties("jinseok")
public class JinseokProperties {
	String name;
	int age;
	String fullName;
    
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getFullName() {
		return fullName;
	}
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
}

 

@Component
public class SampleRunner implements ApplicationRunner{
	@Autowired
	private String hello;
	
	@Autowired
	private JinseokProperties jinseokProperties;
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("=====================================");
		System.out.println(hello);
		System.out.println(jinseokProperties.getName());
		System.out.println(jinseokProperties.getFullName());
		System.out.println("=====================================");
	}
}

결과

 

spring.profiles.include(프로파일추가)

application-pro.properties include 추가
apllication-proddb.properties추가
추가한 화면

@Component
public class SampleRunner implements ApplicationRunner{
	@Autowired
	private String hello;
	
	@Autowired
	private JinseokProperties jinseokProperties;
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("=====================================");
		System.out.println(hello);
		System.out.println(jinseokProperties.getName());
		System.out.println(jinseokProperties.getFullName());
		System.out.println("=====================================");
	}
}

 

결과

 

반응형