SpringBoot Part1 (4)
1. Enviroment profile
ApplicationContext๋ EnvironmentCapable ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ผ๋ฉฐ getEnvironment() ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
profile์ ๋ฐ๋ผ ํ๊ฒฝ์ด ๋ฐ๋๊ณ ๊ทธ์ ๋ฐ๋ผ properties๊ฐ ๋ฐ๋๋ค.
properties
: ๋ค์ํ ์ ํ๋ฆฌ์ผ์ด์ ์์ฑ์ property๋ก ์ ์ํ๊ณ ์ค์ ํ๋ค. ๋ค์ํ ๋ฐฉ์์ผ๋ก ์ค์ ์ด ๊ฐ๋ฅํ๋ค.
resources > application.properties
: springboot์์ default๋ก ์ฌ์ฉํ๋ property ํ์ผ
: key/value ํ์์ผ๋ก ์์ฑ
version = v1.0.0
kdt.version = v1.0.0
kdt.support-vendors = a,b,c,d,e,f,g
kdt.minimum-order-amount = 1
properties ํ์ผ ์ฝ์ด์ค๊ธฐ
- Configuration class์ PropertySource ์ถ๊ฐ
@Configuration
@ComponentScan
@PropertySource("application.properties")
public class AppConfiguration {
}
public static void main(String[] args) {
var applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
var environment = applicationContext.getEnvironment();
var version = environment.getProperty("kdt.version");
var minimunOrderAmount = environment.getProperty("kdt.minimum-order-amount", Integer.class);
var supportVendeor = environment.getProperty("kdt.supportVendors", List.class);
}
- Value ์ด๋ ธํ ์ด์ ์ผ๋ก ์ฌ์ฉํ์ฌ ํ๊ฒฝ๋ณ์ ์ฌ์ฉ
@Component
public class OrderProperties implements InitializingBean {
@Value("v.1.1.1") // ์์ฑ์๋ฅผ ๋ง๋ค์ง ์๊ณ ๊ฐ์ ์ฃผ์
private String version;
@Value("${kdt.minimum-order-amount:8}") //properties ๊ฐ ์ฃผ์
: default๊ฐ
private String minimumOrderAmount;
private List<String> supportVendors;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(MessageFormat.format("version -> {0}", version));
System.out.println(MessageFormat.format("minimumOrderAmount -> {0}", minimumOrderAmount));
System.out.println(MessageFormat.format("supportVendors -> {0}", supportVendors));
}
}
์์คํ ํ๊ฒฝ ๋ณ์๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ฉฐ ๋์ผํ ์ด๋ฆ์ผ ๊ฒฝ์ฐ ์ฐ์ ์์๊ฐ ๋๋ค.
- ํ๊ฒฝ๋ณ์๋ฅผ ์ ์ํ๋ class ๋ง๋ค๊ธฐ
ํ์ํ ์์คํ ์์ฑ๋ค์ ์ ๊ณตํด์ฃผ๋ ๋ชฉ์ ์ class๋ฅผ ๋ง๋ค์ด property๋ฅผ ์ ์ํ๋ค.
@Component
@PropertySource("version.properties")
public class VersionProvider {
private final String version;
public VersionProvider(@Value("${version:v0.0.0}") String version) {
this.version = version;
System.out.println(version);
}
}
2. YAML๋ก ํ๋กํผํฐ์์ฑ
YAML
: ๋ ๋ค๋ฅธ ๋งํฌ์ ์ธ์ด. ๋ฐ์ดํฐ ์ค์ฌ
: ์ค๋๋ XML, JSON์ด ๋ฐ์ดํฐ ์ง๋ ฌํ์ ์ฃผ๋ก ์ฐ์ด๊ธฐ ์์ํ๋ฉด์, ๋ง์ ์ฌ๋๋ค์ด YAML์ ‘๊ฐ๋ฒผ์ด ๋งํฌ์ ์ธ์ด’๋ก ์ฌ์ฉํ๋ ค ํ๋ค.
kdt:
version: "v1.0"
minimum-order-amount: 1
support-vendors:
- a
- b
- c
- d
description: |
line 1 hello world
line 2 xxx
line 3
Spring ํ๋ ์์ํฌ๋ PropertySource ์์ YAML์ ์ง์ํ์ง์์ PropertySourceFactory๋ฅผ ๊ตฌํํด์ผํ๋ค.
public class YamlPropertiesFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
var yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
yamlPropertiesFactoryBean.setResources(resource.getResource());
var properties = yamlPropertiesFactoryBean.getObject();
return new PropertiesPropertySource(resource.getResource().getFilename(), properties);
}
}
yaml์์ List๋ฅผ ๋ฐ์ธ๋ฉํ๊ธฐ ์ํด SpringBoot์์ ์ ๊ณตํด์ฃผ๋ ConfigurationProperties๋ฅผ ์ฌ์ฉํด์ผํ๋ค.
@Configuration
@ComponentScan
@PropertySource(value = "application.yaml", factory = YamlPropertiesFactory.class)
@EnableConfigurationProperties // SpringBoot์ ConfigurationProperties๋ฅผ ์ฐ๊ฒ ๋ค.
public class AppConfiguration {
}
@Configuration
@ConfigurationProperties(prefix = "kdt") // SpringBoot ์ด๋
ธํ
์ด
public class OrderProperties implements InitializingBean {
private String version;
private int minimumOrderAmount;
private List<String> supportVendors;
private String description;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(MessageFormat.format("version -> {0}", version));
System.out.println(MessageFormat.format("minimumOrderAmount -> {0}", minimumOrderAmount));
System.out.println(MessageFormat.format("supportVendors -> {0}", supportVendors));
System.out.println(MessageFormat.format("description -> {0}", description));
}
// ๊ฐ์ ์ฃผ์
ํ๊ธฐ ์ํด์ setter ํ์
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public int getMinimumOrderAmount() {
return minimumOrderAmount;
}
public void setMinimumOrderAmount(int minimumOrderAmount) {
this.minimumOrderAmount = minimumOrderAmount;
}
public List<String> getSupportVendors() {
return supportVendors;
}
public void setSupportVendors(List<String> supportVendors) {
this.supportVendors = supportVendors;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public static void main(String[] args) {
var applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
var orderProperties = applicationContext.getBean(OrderProperties.class);
System.out.println("test -- "+orderProperties.getVersion());
}
์ด๋ ต๋ค.
3. profile
: ํน์ ํ ์์ฑ์ผ๋ก ๊ทธ๋ฃนํ
: ์ค์ ์ด๋ Bean๋ค์ ๊ทธ๋ฃนํํด์ ํ๋์ profile๋ก ์ ์ํ๊ณ ์ฌ๋ฌ๊ฐ์ profile ์ค ์ํ๋ profile๋ก ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ๋ํ ์ ์๋ค.
Profile ์ด๋ ธํ ์ด์ ์ ์ด์ฉํด ์ด๋ฆ์ ์ค์
@Repository
@Profile("dev")
public class JdbcVoucherRepository implements VoucherRepository{
@Override
public Optional<Voucher> findById(UUID voucherId) {
return Optional.empty();
}
@Override
public Voucher insert(Voucher voucher) {
return null;
}
}
public static void main(String[] args) {
var applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(AppConfiguration.class);
var environment = applicationContext.getEnvironment();
environment.setActiveProfiles("local"); //profile ์ ํ
applicationContext.refresh(); //profile์ด ์ฌ๋๋ก ์ ์ฉ๋ ์ ์๊ฒ refresh
}
YAML ํ์ผ์๋ profile ์ ์ ๊ฐ๋ฅ - springboot ๊ธฐ๋ฅ
---
spring.config.activate.on-profile : local
kdt:
version: "v1.0"
minimum-order-amount: 1
support-vendors:
- a
- b
- c
- d
description: |
line 1 hello world
line 2 xxx
line 3
---
spring.config.activate.on-profile : dev
kdt:
version: "v1.0-dev"
minimum-order-amount: 1
support-vendors:
- a
- b
- c
- d
description: |
line 1 hello world
line 2 dev
line 3 dev
---
: ํ๋์ ํ์ผ์์ ์ฌ๋ฌ profile์ ๊ตฌ์ฑํ ์๋ ์๊ณ ์ฌ๋ฌ๊ฐ์ ํ์ผ์ ๋ง๋ค์ด์ ์ ์ฉ ํ ์๋ ์๋ค.
@SpringBootApplicateion : ์คํ ๋ฐ์ฒ๊ฐ ๋ค๋ฅด๋ค.
@SpringBootApplication
public class KdtApplication {
public static void main(String[] args) {
var springApplication = new SpringApplication(KdtApplication.class);
springApplication.setAdditionalProfiles("local");
var applicationContext = springApplication.run(args);
//var applicationContext = SpringApplication.run(KdtApplication.class, args);
}
}
: run/debug ๊ตฌ์ฑ ํ์ผ์์ Activate profiles ํน์ arguments๋ก profile์ ์ ์ฉํ ์ ์๋ค.
4. Resource
Spring์ ์ธ๋ถ ๋ฆฌ์์ค(์ด๋ฏธ์ง, ํ ์คํธ, key ํ์ผ ๋ฑ)๋ฅผ ํ๋์ API๋ก ์ฝ์ด์ฌ ์ ์๋๋ก ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค.
→ Resource / ResourceLoader ์ธํฐํ์ด์ค
: ResourceLoader๋ฅผ ํตํด Resource๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
: ๋ชจ๋ ApplicationContext๊ฐ ResourceLoader๋ฅผ ๊ตฌํํ๊ณ ์๋ค. (Resource ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค. )
public static void main(String[] args) throws Exception{
var applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
var resource = applicationContext.getResource("application.yaml"); //working directory ๊ธฐ์ค, classpath ๊ฐ ๊ธฐ๋ณธ๊ฐ
var resource2 = applicationContext.getResource("file:test/sample.txt"); //
var resource3 = applicationContext.getResource("<https://stackoverflow.com/>");
System.out.println(resource.getClass().getCanonicalName());
var file = resource2.getFile();
var strings = Files.readAllLines(file.toPath());
System.out.println(strings.stream().reduce("",(a,b)->a+"\\n"+b));
var readableByChannel = Channels.newChannel(resource3.getURL().openStream());
var bufferedReader = new BufferedReader(Channels.newReader(readableByChannel, StandardCharsets.UTF_8));
var contents = bufferedReader.lines().collect(Collectors.joining("\\n"));
System.out.println(contents);
}
Daliy Mssion
- ๊ณ ๊ฐ ๋ธ๋ ๋ฆฌ์คํธ ๋ช
๋จ ์์ฑ
- customer_blacklist.csv ํ์ผ์ ๋ง๋ค๊ณ ์กฐํํ ์ ์๋ค. → parsing
- YAML ํ๋ผํผํฐ๋ฅผ ๋ง๋ค๊ณ ์ด๋ค ์ค์ ์ ๋ง๋ค์ ์์์ง ๊ณ ๋ฏผํด๋ณด๊ธฐ
- ๋ฐ์ด์ฒ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์์ ๊ด๋ฆฌํ๋ ๋ ํฌ๋ ๊ฐ๋ฐ ํ๋กํ์ผ์์๋ง ๋์ํ๊ฒ ํด๋ณด๊ธฐ
'Back-end ๋ฐ๋ธ์ฝ์ค > week 03 - 05 TIL (Spring)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL] 221107 - SpringBoot Part2 : Spring Test ์์ํ๊ธฐ (0) | 2022.11.08 |
---|---|
[TIL] 221104 - SpringBoot Part1 : logging, SpringBoot (2) | 2022.11.06 |
[TIL] 221102 - SpringBoot Part1 : Dependecy injection, ์ปดํฌ๋ํธ ์ค์บ (0) | 2022.11.03 |
[TIL] 221101 - SpringBoot Part1 : IoC, DDD, ApplicationContext (0) | 2022.11.03 |
[TIL] 221031 - SpringBoot Part1 (0) | 2022.11.02 |
๋๊ธ