티스토리 뷰

Programming/Java

Spring Boot : Config(properties) 파일

파란크리스마스 2023. 6. 5. 17:09
728x90

출처

Maven pom.xml 설정

Maven 배포시 프로파일 선택 옵션 : mvn clean package -P prod

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/resources/bluexmas-${profile}</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    
    <!-- 생략 -->  
  </build>
  
  <profiles>
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <profile>dev</profile>
      </properties>
    </profile>
    <profile>
      <id>local</id>
      <properties>
        <profile>local</profile>
      </properties>
    </profile>
    <profile>
      <id>prod</id>
        <properties>
          <profile>prod</profile>
      </properties>
    </profile>
  </profiles>

src\main\resources\bluexmas-dev\config\config.properties

username=test
password=test!!

@Configuration 클래스 (PropertyConfig.java)

@Bean(name="config") : Bean 객체 config 이름으로 스프링 컨테이너에 로딩

package com.bluexmas.config;

import java.io.IOException;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration
public class PropertyConfig {

	@Bean(name="config")
	public static PropertiesFactoryBean config() throws IOException {
		PropertiesFactoryBean pspc = new PropertiesFactoryBean();
		Resource[] resources = new PathMatchingResourcePatternResolver()
				.getResources("classpath:/config/config.properties");
		pspc.setLocations(resources);
		return pspc;
	}
}

Config 값 참조

@Resource(name="config") : config 이름의 Properties 객체 참조
@Value("#{config['username']}") : config 이름의 Properties 객체에서 username 값 참조

package com.bluexmas.controller;

import java.util.Properties;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SampleController {
	
	@Resource(name="config")
	private Properties config;
	
	@Value("#{config['username']}")
	private String username;
	
	@ResponseBody
	@RequestMapping("/sample")
	public String sample() {
		System.out.println("username.1 = " + config.get("username"));
		System.out.println("username.2 = " + username);
		
		String data = "@ResponseBody 어노테이션을 통해 반환";
		return data;
	}
	
}
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함