【Spring Boot】 독자적인 프로퍼티 파일을 추가해, env.getProperty()로 값을 취득하고 싶다.
                                            
                                                
                                                
                                                
                                                
                                                
                                                 5485 단어  자바spring-boot
                    
데모 앱 구성
 
src/main/resources/test.properties
추가한 속성 파일.
이 파일로부터, 값을 취득한다.
 
프로퍼티 파일내는 ↑의 상태.
 @PropertySource로 Environment에 속성 파일 추가
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:test.properties")
public class PropertyTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(PropertyTestApplication.class, args);
    }
}
 @PropertySource ()의 인수에, 프로퍼티 파일의 패스를 지정해 주면(자), Environment에 추가할 수 있다.
 @PropertySource 은 @Configuration 과 함께 선언해야 하지만,
 @SpringBootApplication 내에서 @Configuration 이 선언되어 있기 때문에 특별히 선언할 필요는 없다.
 @PropertySource의 레퍼런스는 이쪽
 env.getProperty()로 취득한다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class PropertyGetComponent {
    @Autowired
    Environment env;
    public void printProperty() {
        String value = env.getProperty("test.property.key");
        System.out.println("取得した値は[ " + value + " ] です。");
    }
}
printProperty()의 실행 결과는↓
取得した値は[ test value ] です。
env.getProperty ()로 추가 한 속성 파일에서 값을 검색 할 수있었습니다.
 참고한 기사
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:test.properties")
public class PropertyTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(PropertyTestApplication.class, args);
    }
}
@PropertySource ()의 인수에, 프로퍼티 파일의 패스를 지정해 주면(자), Environment에 추가할 수 있다.
@PropertySource 은 @Configuration 과 함께 선언해야 하지만,
@SpringBootApplication 내에서 @Configuration 이 선언되어 있기 때문에 특별히 선언할 필요는 없다.
@PropertySource의 레퍼런스는 이쪽
env.getProperty()로 취득한다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class PropertyGetComponent {
    @Autowired
    Environment env;
    public void printProperty() {
        String value = env.getProperty("test.property.key");
        System.out.println("取得した値は[ " + value + " ] です。");
    }
}
printProperty()의 실행 결과는↓
取得した値は[ test value ] です。
env.getProperty ()로 추가 한 속성 파일에서 값을 검색 할 수있었습니다.
 참고한 기사
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class PropertyGetComponent {
    @Autowired
    Environment env;
    public void printProperty() {
        String value = env.getProperty("test.property.key");
        System.out.println("取得した値は[ " + value + " ] です。");
    }
}
取得した値は[ test value ] です。
Reference
이 문제에 관하여(【Spring Boot】 독자적인 프로퍼티 파일을 추가해, env.getProperty()로 값을 취득하고 싶다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/HiromasaNojima/items/90c656397260697dac3a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)