Spring Boot@PropertySource 에서 지정 한 프로필 을 불 러 오고@ImportResource 에서 Spring 프로필 가 져 오기
@PropertySource properties 프로필 불 러 오기
@PropertySource yml 프로필 불 러 오기
@ImportResource 스프링 프로필 가 져 오기
@PropertySource properties 프로필 불 러 오기
1.을 통 해 @ConfigurationProperties,@Value 주 값 은'@Value'와'@ConfigurationProperties'를 사용 하면 전역 설정 파일 인'application.properties'나'application.yml'에서 값 을 추출 한 다음 필요 한 속성 에 값 을 부여 할 수 있 음 을 알 고 있 습 니 다.
2.응용 이 비교적 클 때 모든 내용 이 하나의 프로필 에 있 으 면 비대 해 보이 고 이해 와 유지 가 쉽 지 않 습 니 다.이 때 한 파일 을 여러 개 로 나 눌 수 있 습 니 다.@PropertySource 주 해 를 사용 하여 지정 한 프로필 을 불 러 올 수 있 습 니 다.
3.다음은@PropertySource 주 해 를 사용 하여 로 딩 경로 의 user.properties 설정 파일 을 설명 하고 User.java POJO 대상 의 속성 에 값 을 부여 합 니 다.
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author wangmaoxiong
* Created by Administrator on 2018/7/11 0011.
* ···
* @ConfigurationProperties SpringBoot ;
* prefix = "user" key user ,
* "user" key, POJO ,
*
* @Component Spring , , @ConfigurationProperties
* @PropertySource (value = { " classpath : user.properties " })
*/
@PropertySource(value = {"classpath:user.properties"})
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private Integer id;
private String lastName;
private Integer age;
private Date birthday;
private List colorList;
private Map cityMap;
private Dog dog;
// getter、setter 、toString
}
/**
* @author wangmaoxiong
* Created by Administrator on 2018/7/11 0011.
* ----------- Java Bean。
* @ConfigurationProperties , getter、setter
*/
public class Dog {
private Integer id;
private String name;
private Integer age;
// getter、setter 、toString
}
4.클래스 경로 에서 user.properties 설정 파일 을 다음 과 같이 제공 합 니 다(.yml 파일 도 마찬가지 입 니 다).
user.id=111
user.lastName=
user.age=120
user.birthday=2018/07/11
user.colorList=red,yellow,green,blacnk
user.cityMap.mapK1=
user.cityMap.mapK2=
user.dog.id=7523
user.dog.name=
user.dog.age=3
5.테스트 실행:
import com.wmx.yuanyuan.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class YuanyuanApplicationTests {
/**
* POJO Component , Spring , Spring
* @Autowired @Resource,DI
*/
@Resource
private User user;
@Test
public void contextLoads() {
System.out.println("------------------------------********************--------------------------------------------");
System.out.println("·······················" + user);
}
}
@PropertySource yml 프로필 불 러 오기
1.위의 properties 를 바탕 으로 다음 과 같이 수정 합 니 다.
@PropertySource(value = {"classpath:user.yml"}, factory = PropertySourceFactory.class)
2.그 다음 에 yml 프로필 을 처리 하 는 속성 자원 프로젝트 를 제공 합 니 다.다음 과 같 습 니 다.
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import java.io.IOException;
import java.util.List;
/**
* @PropertySource yml .
*
* @author wangmaoxiong
* @version 1.0
* @date 2020/5/25 20:45
*/
public class PropertySourceFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource> createPropertySource(String name, EncodedResource resource) throws IOException {
if (resource == null) {
return super.createPropertySource(name, resource);
}
List> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());
return sources.get(0);
}
}
@ImportResource 스프링 프로필 가 져 오기
1.@ImportResource 주 해 는 Spring 의 프로필 을 가 져 오 는 데 사 용 됩 니 다.예 를 들 어 핵심 프로필 인'beans.xml'과 같이 프로필 안의 내용 을 유효 하 게 합 니 다.
2.응용 프로그램 에서 예전 xml 파일 의 설정 방식 을 사용 하려 면'beans.xml'과 같은'@ImportResource'주 해 를 사용 하면 쉽게 해결 할 수 있 습 니 다.
3.@ImportResource 를 하나의 설정 클래스 에 표시 하고 응용 시작 클래스 에 직접 배치 하 며@SpringBootApplication 과 함께 하면 됩 니 다.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
*
*
* @ImportResource , , value Spring
*/
@ImportResource(value = {"classpath:beans.xml"})
@SpringBootApplication
public class CocoApplication {
public static void main(String[] args) {
SpringApplication.run(CocoApplication.class, args);
}
}
4.그리고 클래스 경로 에서 원본 beans.xml 설정 파일 을 제공 할 수 있 습 니 다.
5.응용 콘 솔 을 시작 하면 인쇄 합 니 다:loading XML bean definitions from class path resource[beans.xml]는 불 러 오 는 데 성 공 했 음 을 표시 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.