설정클래스 상단에 @PropertySource("classpath:your.properties")을 사용하면 yml 구성을 로딩한것과 같은 효과다.
@Component
@PropertySource(value = {"classpath:static/config/authorSetting.properties"},
ignoreResourceNotFound = false, encoding = "UTF-8", name = "authorSetting.properties")
public class AuthorTest {
@Value("${author.name}")
private String name;
@Value("${author.age}")
private int age;
}
@PropertySource 의 속성 요약
1.value:설정파일 경로.
2.ignoreResourceNotFound:지정파일 존재하지 않을시 에러! 기본옵션 false 이다.true 설정해야 파일 없으면 오류. 실제 개발시 ignoreResourceNotFound 를 false로 설정한다.
3.encoding:파일 읽어드릴시 UTF-8 인코딩.
@Value 사용이 좀 많을때 등장한것이 바로 @ConfigurationProperties 옵션이다.
@Component
@ConfigurationProperties(prefix = "author")
@PropertySource(value = {"classpath:static/config/authorSetting.properties"},
ignoreResourceNotFound = false, encoding = "UTF-8", name = "authorSetting.properties")
public class AuthorTest {
private String name;
private int age;
}
@RestController
@EnableConfigurationProperties
public class DemoController {
@Autowired
AuthorTest authorTest;
@RequestMapping("/")
public String index(){
return "author's name is " + authorTest.getName() + ",ahtuor's age is " + authorTest.getAge();
}
}
마지막으로
@ConfigurationProperties 사용하기위 위해서는 꼭 @EnableConfigurationProperties 를 써서 활성화 한다.