2

I am trying to read second database connection information from application properties in my module. My configuration module should read values.

Configuration

@Configuration
@PropertySource(value={"classpath:application.properties"})
@ConfigurationProperties(prefix = "spring.second-datasource")
public class DatabaseConfiguration {

    @Value("${url}")
    private String url;
    @Value("${username}")
    private String user;
    @Value("${password}")
    private String password;

    public String getUrl() {
        return url;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }
}

When I call DatabaseConfiguration inside of the controller, it gives an error.

private DatabaseConfiguration databaseConfiguration;
databaseConfiguration.getUrl();
.
.
.

Error

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'url' in value "${url}"

I found some solutions such as this one

Spring boot could not resolve placeholder in string

I implemented it.

POM.XML

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<artifactId>//artifactID</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>fraud-core</name>
<description>Demo project for Spring Boot</description>

<parent>
   //Parent
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <HMN_CDM_Facade.version>TEST-SNAPSHOT</HMN_CDM_Facade.version>
    <hmn.dependency.scope>compile</hmn.dependency.scope>
</properties>

<dependencies>
    //DEPENDENCIES
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
</build>

Thanks.

Berkin
  • 1,565
  • 5
  • 22
  • 48

2 Answers2

2

You are mixing two ways of reading configurations data in spring-boot.

@Value should be used when you have an individual property that has to be accessed inside a @Component.. basically it is a one of thing like

 Ex: max.threads=10
 Which can be accessed in a component using @Value.

The Configuration properties way is when you have multiple values and are functionally grouped.

Ex: server.names:
     -
      server1
     -
      server2

Now for this you should use configuration properties.

Do not mix both. You should not use @Value inside a configuration properties java file.

0

@Value("${url}") ,this way of writing corresponds to the 'application.propertice' or 'application.yml' file. The correct way of writing below.

enter image description here

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Simon
  • 29
  • 7
  • 1
    I believe that there is no 'correct way' of property naming. In this case correct property name in `.yml` or `.properties` is exactly `spring.second-datasource.url` ... – rxn1d Oct 31 '18 at 09:04