-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spring은 설정 파일을 어떻게 사용할까? #15
Comments
Spring XML Config직접 Spring XML 설정해 보기먼저 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans> 생성한 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<property name="jdbcUrl" value="jdbc:h2:tcp://localhost/~/code/study/db/config"/>
<property name="username" value="sa"/>
<property name="password" value="1234"/>
<property name="driverClassName" value="org.h2.Driver"/>
</bean>
</constructor-arg>
</bean>
</beans> 다음과 같이 @SpringBootApplication
@ImportResource("classpath:application.xml") // XML 파일을 임포트
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
대략 30분 동안 왜 사용하지 않는가?먼저 아래의 코드들은 각각
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<property name="jdbcUrl" value="jdbc:h2:tcp://localhost/~/code/study/db/config"/>
<property name="username" value="sa"/>
<property name="password" value="1234"/>
<property name="driverClassName" value="org.h2.Driver"/>
</bean>
</constructor-arg>
</bean>
</beans>
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:tcp://localhost/~/code/study/db/config
password: 1234
username: sa
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:tcp://localhost/~/code/study/db/config
spring.datasource.username=sa
spring.datasource.password=1234 각각의 파일에 따른 설정들이며 공통된 점으로는 과거에 간결성과 가독성
편리한 리스트 및 맵 표현
더 많은 프로그래밍 언어 및 플랫폼과 호환
Spring Boot 의 기본 지원이제
요약
Ref |
👍 문제
Spring은 XML을 중심으로 설정 정보를 보관하던 때가 있었다. 현재는 XML 설정을 완전히 배제한 채로 처리하도록 장려하고 있다. 그 대안으로는 .properties나 .yaml, .yml 파일이 있는데 이렇게 바뀐 이유는 무엇일까?
또, .properties, .yaml 등 이런 파일이 스프링에서 설정 정보를 보관할 수 있도록 만든 원리는 무엇일까? <- 이건 어려우면 패스하자..ㅎ
지난주 암살주제 때문에 이슈 난이도를 좀 낮추고자 서버 개발자에게 친숙한 Spring을 가지고 오고 싶었다.
📺 관련 챕터 및 레퍼런스
story. 13 XML과 JSON도 잘 쓰자.
🐳 비고
서버 개발자가 되고 싶은 사람이 이 주제를 다루면 찰떡일 거 같아요!
The text was updated successfully, but these errors were encountered: