스프링에서 API Key 또는 비밀스러운 정보를 따로 관리해야 할 때(에를 들어 public git 시스템에 푸쉬), 역할에 따른 설정파일이 필요할 때는 application.yml 이외의 설정파일을 추가해야합니다.
이 떄, 스프링 부트에서 설정파일을 나누는 방법으로, 별도의 설정 없이 ***.yml파일 추가 후, 아래 코드와 같이 실행파일의 경로를 등록해주면 됩니다.
@SpringBootApplication@EnableGlobalMethodSecurity(securedEnabled = true)
public class JclipProtoApplication {
//static {
// System.setProperty("spring.config.location", "classpath:/application.yml,classpath:/demo.yml");
// }
private static final String APPLICATION=
"spring.config.location="+
"classpath:/application.yml,"+
"classpath:/private.yml";
public static void main(String[] args) {
new SpringApplicationBuilder(JclipProtoApplication.class).properties(APPLICATION).run(args);
// SpringApplication.run(JclipProtoApplication.class, args);}
참고로, maven test 기능을 이용할 경우에는, @SpringBootTest 에도 프로퍼티 설정을 추가해줘야합니다.
추가하지 않을 경우에는 메이븐 테스트 시 demo.yml
프로퍼티를 읽어들이지 못하여 아래와 같은 BUILD FAILURE이 발생됩니다.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openAPI' defined in class path resource [me/jiniworld/demo/configs/OpenApiConfig.class]:
Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException:
Could not resolve placeholder 'demo.version' in value "${demo.version}"
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] DemoApplicationTests.contextLoads » IllegalState Failed to load ApplicationCon...
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 57.176 s
[INFO] Finished at: 2020-08-15T20:39:54+09:00
[INFO] ------------------------------------------------------------------------
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
static {
System.setProperty("spring.config.location", "classpath:/application.yml,classpath:/demo.yml");
}
...
}
출처: https://blog.jiniworld.me/81
private static final String PROPERTIES =
"spring.config.location="
+"classpath:/application.yml"
+",classpath:/google.yml"
+",classpath:/mail.yml";
public static void main(String[] args) {
new SpringApplicationBuilder(RecruitJogbo.class)
.properties(PROPERTIES)
.run(args);
}
아래방법이 가장 간편한 듯.
ftp:
port: 2121
uploadDir: C:\\kangminkyu\\FTPfileUpload
userlist:
- kang/1234
- min/1234
- kyu/1234
# api: #요건 내부 클래스로
# name: kakao
# key: 123123
package net.lunalabs.central.config;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
Properties properties = factory.getObject();
return new PropertiesPropertySource(resource.getResource().getFilename(), properties);
}
}
package net.lunalabs.central.config.ftp;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import lombok.Getter;
import lombok.Setter;
import net.lunalabs.central.config.YamlPropertySourceFactory;
@Setter //이걸로 값을 집어넣는 가보다.
@Getter
@Configuration
//@PropertySource(value = "classpath:ftpconfig.yml",
//factory = YamlPropertySourceFactory.class, ignoreResourceNotFound = true)
@PropertySource(value = "classpath:commonproperties.yml", factory = YamlPropertySourceFactory.class)
@ConfigurationProperties(prefix = "ftp")
public class FtpProperties {//일단은 하드코딩
public Integer port;
public String[] userlist;
public String uploadDir;
}
https://www.baeldung.com/spring-yaml-propertysource
https://www.latera.kr/reference/java/2019-09-29-spring-boot-config-externalize/