본문 바로가기

개발/프로젝트

[스프링부트 게시판] 6. application.properties 설정

이번 게시글에서는 스프링부트의 전반적인 설정을 담당하는 application.properties 설정에 대해 설명드리려고 합니다.

 

먼저 .properties 에 대해서 간단히 말씀드릴 것은 'Key=Value형식으로 파라미터 정보들을 저장하기 위한 파일 확장자를 의미' 라고 불립니다. 이것을 사용하는 것에서 나중에 배울 Spring message들을 묶어 놓는 message.properties 등이 있습니다. 스프링부트의 전반적인 설정을 담당하는 것은 yml도 있지만 이는 나중에 리팩토링 하면서 바꿔보도록 하겠습니다.

 

아래의 링크는 properties에 전반적으로 쓰이는 스프링 공식 홈페이지 Document 입니다.

 

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

 

Common Application Properties

 

docs.spring.io

 

이전에 MySQL을 연결할 때 사용했던 application.properties 입니다.

 

application.properties

 

여기에서 더 추가하여 설정하겠습니다. properties에서 주석처리는 '#' 을 넣어서 처리하면 됩니다.

 

#Server(서버)
server.port=3000
server.servlet.context-path=/toy

## Encoding(인코딩)
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true

# Spring(스프링)
spring.thymeleaf.prefix=classpath:templates/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false

## DataSource(데이터베이스)
spring.datasource.url=jdbc:mysql://localhost:3306/[해당 데이터베이스 명]?autoReconnect=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=[해당 데이터베이스 아이디]
spring.datasource.password=[해당 데이터베이스 비밀번호]

## JPA(Java Persistense API)
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true

# Log
logging.level.org.hibernate.type.descriptor.sql=trace

 

이번 게시글에서는 Server와 Spring 해당하는 설정에 설명드리겠습니다. 나머지는 진행하면서 같이 추가하여 설명드리겠습니다.

 

1. server

우선 server 부분에서는 port와 servlet, encoding 등을 설정하고 있습니다. 서버를 연결하는 port와 기본 고정하는 url을 설정하는 등 사용할 수 있습니다.

 

바뀐 properties를 통한 URL에서 port 번호를 3000(기본은 8080입니다.), /http/get -> /toy/http/get으로 변경되었습니다.

 

2. spring

다음으로 spring은 mvc, datasource 등을 설정할 수 있습니다. 저와 같은 경우에 Thymeleaf를 사용하고 있어 이에 대한 설정을 하였고 웹 사이트의 View를 보여줄 폴더인 prefix와 View를 담당하는 확장자를 뜻하는 suffix를 설정하였습니다. 위에 cache 설정을 false로 했는데 Thymeleaf 템플릿의 cache가 true이면 새로고침을 해도 변경된 값을 확인 할 수가 없어 false로 수정하였습니다.

 

다음 datasource는 이전 MySQL 설정때도 보셨지만 사용하려는 데이터베이스의 연동을 위한 부분입니다. 이전 Spring에서는 jdbc로 사용하였던 것이 이렇게 변경되었습니다.