Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

java - Spring Boot Logback DB Appender Properties

Hi I want to use a DBAppenderin my Spring Boot application. I want to retrieve database connection properties from the application.properties file. However it doesn't seem to recognize them. Keep in mind that I'm using Spring Boot 1.2.x so I can't use logback-spring.xml yet.

The configuration I'm using is the following:

<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">

            <driverClass>${spring.datasource.driver-class-name}</driverClass>
            <url>${spring.datasource.url}</url>
            <user>${spring.datasource.username}</user>
            <password>${spring.datasource.password}</password>
        </connectionSource>
    </appender>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Stumbled upon this while searching for a similar solution. Since this is still unanswered, here's a few approaches I found:

1) If you are using Spring Boot 1.3+ (which you already pointed out you're not but for future reference), I managed to use the <springProperty> tag to reuse the same values from application.properties.

application.properties (for embedded H2 DB):

spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

logback-spring.xml:

<springProperty name="spring.datasource.driverClassName" source="spring.datasource.driverClassName"/>
<springProperty name="spring.datasource.url" source="spring.datasource.url"/>
<springProperty name="spring.datasource.username" source="spring.datasource.username"/>
<springProperty name="spring.datasource.password" source="spring.datasource.password"/>

<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
    <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
        <driverClass>${spring.datasource.driverClassName}</driverClass>
        <url>${spring.datasource.url}</url>
        <user>${spring.datasource.username}</user>
        <password>${spring.datasource.password}</password>
    </connectionSource>
</appender>

2) Import application properties as property source: Unable to use Spring Property Placeholders in logback.xml

<property resource="application.properties" />

3) Maybe you're able to register the Datasource in the container JNDI and use logback's JNDIConnectionSource instead? Check out this other post: How to create JNDI context in Spring Boot with Embedded Tomcat Container


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...