๐Spring ์ธ๋ถ ์ค์ ํ์ผ๊ณผ ์ ์ ๋ฆฌ์์ค
์ธ๋ถ ์ค์ ํ์ผ
application.properties
application.yml
๋ก ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.- Key-Value ํํ๋ก ์ค์ ์ ์ ์ฅํ๋ค.
human.name = develup4
human.age = 35
- ์คํ๋ง ๋ถํธ๊ฐ Application์ ์คํํ ๋ ์๋์ผ๋ก loadํ๋ค.
์ ์ฉ ์ฐ์ ์์
- {ํ๋ก์ ํธ Root}/config/application.yml
- {ํ๋ก์ ํธ Root}/application.yml
- classpath:/config/application.yml
- classpath:/
์ฐธ์กฐ ๋ฐฉ๋ฒ
@Value
์ ๋ ธํ ์ด์ ์ ํตํด ์ฃผ์ ๋ฐ๋๋ค.
@Component
public class SimpleProperties implements ApplicationRunner {
@Value("${human.name}")
private String name;
}
Type-safeํ๊ฒ ์ด์ฉํ ์ ์๋ ๋ฐฉ๋ฒ
- property์ ๋์๋๋ Bean ํด๋์ค๋ฅผ ์ ์ํ ์ ์๋ค.
spring-boot-starter-validation
์ ์ด์ฉํด ์ ๋ ฅ๊ฐ ๊ฒ์ฆ(Not Null, Not Empty ๋ฑ)๋ ๊ฐ๋ฅํ๋ค.
@Component
@ConfigurationProperties("human")
@Validated
public class HumanProperties {
@NotEmpty
private String name; // ํ์
์ด ์ง์ ๋์ด ์ธ์ดํํ๋ค.
private int age;
// getter๋ ์ด๋ ํ ๋ก์ง์ด ๋ค์ด๊ฐ ์๋ ์๋ค.
public getAge() {
return age;
}
}
@Component
public class PropertyTest {
@Autowired
HumanProperties humanProperties; // ์ฃผ์
๋ฐ์ ์ฌ์ฉํ๋ค.
}
Profile
- ํ๊ฒฝ์ ๋ฐ๋ผ ๋ค๋ฅธ ์ค์ ๊ฐ์ ์ฌ์ฉํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค.
- ์๋ฅผ ๋ค์ด Production ํ๊ฒฝ, Test ํ๊ฒฝ๊ฐ์ด ๋๋ ์ ์๋ค.
@Profile("production")
@Configuration
public class BaseConfiguration {
@Bean
public String hello() {
return "hello";
}
}
@Profile("test")
@Configuration
public class TestConfiguration {
@Bean
public String hello() {
return "hello test";
}
}
- ์ฌ์ฉํ Profile์
application.properties
ํ์ผ์์ ์ ํํ ์ ์๋ค.
spring.profiles.active = production
Profile๋ง๋ค ๋ณ๋์ property ํ์ผ์ ์ค์ ํ๋ ๋ฐฉ๋ฒ
application-{Profile ์ด๋ฆ}.properties
๋ก ํ์ผ์ ์์ฑํ์ฌ ๋ณ๋์ ํ๊ฒฝ์ ์ค์ ํ๋ค.- ์๋ฅผ ๋ค์ด
application.properties
์์ ๊ณตํต์ ์ค์ ์ ์ ์ํ๊ณapplication-prod.properties
์์ ๋ผ์ด๋ธ ํ๊ฒฝ์ ์ธํ ์,application-test.properties
์์ ํ ์คํธ ํ๊ฒฝ์ ์ค์ ํ ์ ์๋ ๊ฒ์ด๋ค.
์ ์ ๋ฆฌ์์ค
- ์๋ ๊ฒฝ๋ก์ ๋ฆฌ์์ค ๋ค์ ์น ์๋ฒ์์ ๋ฐ๋ก ์ ๊ทผํ ์ ์๋ค.
classpath:
๋src/main/
์ ์๋ฏธํ๋ค.classpath:/static
classpath:/public
classpath:/resources/
classpath:/META-INF/resources
application.properties
์ ๊ฒฝ๋ก๋ฅผ ์ง์ ์ง์ ํ ์๋ ์๋ค.- spring.mvc.static-path-pattern=/static/**
- ๋ค๋ง ์ 1, 2, 3, 4์ ๊ธฐ๋ณธ ๊ฒฝ๋ก๋ ๋ฌด์๋๋ค.
- ๊ธฐ๋ณธ ๊ฒฝ๋ก์ ์ถ๊ฐ์ ์ผ๋ก ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐํ๊ณ ์ถ๋ค๋ฉด ์๋์ฒ๋ผ Configuration์ ์์ฑํ๋ค.
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandler(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/path/**")
.addResourceLocation("classpath:/m/")
.setCachePeriod(20)
}
}
index ํ์ด์ง์ ํ๋น์ฝ
- ์ ์ ์ ๋ฆฌ์์ค ๊ฒฝ๋ก์
index.html
์ด ์๋ค๋ฉด welcome ํ์ด์ง๋ก ์ด์ฉํ๋ค. - ์ ์ ์ ๋ฆฌ์์ค ๊ฒฝ๋ก์
favicon.ico
์ด ์๋ค๋ฉด ํ๋น์ฝ์ผ๋ก ์ด์ฉํ๋ค.
Error ํ์ด์ง
resources/static/error
์ {Status Error ์ฝ๋}.html์ ์์ฑํ๋ฉด ํด๋น ์๋ฌ ๋ฐ์์ ํ์ด์ง๋ฅผ ํ์ํด์ค๋ค.- ์๋ฅผ ๋ค์ด
404.html
,5xx.html
๊ณผ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
Web Jar
- ์คํ๋ง ๋ถํธ ๋ด์ ์ ์ ํ์ด์ง์์ ์ฌ์ฉํ javascript๋ css ๋ฑ์ ๋ฆฌ์์ค๋ค์ jar ํํ๋ก ํฌํจํ ์ ์๋ค.
- Maven Repository์์ ๊ฒ์ํด๋ณด๋ฉด
jQuery
์ ๊ฐ์ ํ๋ก ํธ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ค๋ ์์กด์ฑ์ ์ถ๊ฐํ ์ ์๋ค. <script src="/webjars/jquery/3.3.1/dist/jquery.min.js">
์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค.
[1] ๋ฐฑ๊ธฐ์ , ์คํ๋ง ๋ถํธ ๊ฐ๋ ๊ณผ ํ์ฉ, 4๋ถ ์คํ๋ง ๋ถํธ ํ์ฉ