Spring

[오류] springboot 외부 properties 설정

ysk223 2020. 8. 17. 22:06

##환경

  • ubuntu 16.04

bulid.gradle



buildscript {
ext { // ext라는 키워드는 build.gradle에서 사용하는 전역변수를 설정하겠다는 의미
springBootVersion = '2.3.2.RELEASE' // springBootVersion이라는 전역변수 생성
}
repositories {
mavenCentral()
jcenter()

}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}

}

plugins {
id 'java'
id 'org.springframework.boot' version '2.3.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}

group 'xyz.cafeaddy.rest'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories { // 각종 의존성(라이브러리)들을 어떤 원격 저장소에서 받을지 정하는것.
mavenCentral()
jcenter() // 라이브러리 업로드 난이도 때문에 jcenter를 요즘 많이 사용
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile('org.springframework.boot:spring-boot-starter-web')

compile('org.projectlombok:lombok')
compile('org.springframework.boot:spring-boot-starter-data-jpa:')
runtimeOnly('mysql:mysql-connector-java')

testCompile('org.springframework.boot:spring-boot-starter-test')

annotationProcessor('org.projectlombok:lombok')
testAnnotationProcessor('org.projectlombok:lombok')
}


프로젝트 내 properties 파일

  • application.properties

spring.profiles.include=real,db
spring.jpa.show-sql=true

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.session.store-type=jdbc

리눅스 외부 properties 파일


spring.jpa.hibernate.ddl-auto=none
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:포트번호/DB명?characterEncoding=UTF8&serverTimezone=UTC
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.username=root
spring.datasource.password=root비밀번호

오류 로그

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-17 21:00:19.929 ERROR 17595 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :


APPLICATION FAILED TO START


Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine suitable jdbc url

Action:

Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (the profiles real,db are currently active).

애플리케이션 실행시 외부 프로퍼티 포함


nohup java -jar \

 -Dspring.config.location=classpath:/application.properties,classpath:/application-real.properties,/root/app/application-db.properties \

 -Dspring.prifiles.active=real,db \

 $REPOSITORY/$JAR_NAME 2>&1 &

여기서 외부 프로퍼티가 /root/app/application-db.properties 인데,
다른 프로퍼티들이랑 이어 쓸때 공백이 있으면 안되고 반드시 절대 경로로 써야 프로퍼티를 읽을 수 있다.