MarioC
MarioC

Reputation: 3228

Spring boot - cannot use @Value annotation

I'm trying to use the @Value to load some value from application.properties based on the profile, but there is something that does not work...

In my application.properties I have

[email protected]@

I have

<resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

in my pom.xml. When compiling, i see that my application.properties has the right value of jdbc.url

Then i want to use this property when connecting to the DB

 @Value("${jdbc.url}") 
    private String dbUrl;

        @PostConstruct
        public Connection getConnection(){

                Class.forName("com.mysql.jdbc.Driver");
                return DriverManager.getConnection(dbUrl, "user", "xxxxxxx");
            }

but dbUrl is null... Do i need something else?

Upvotes: 0

Views: 437

Answers (2)

user4123084
user4123084

Reputation:

Maybe you need specify delimiters, inside your plugins.

 <configuration>
    <delimiters>
        <delimiter>@</delimiter>
    </delimiters>
    <useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>

Upvotes: 0

foxy
foxy

Reputation: 26

You should load the value like this:

@Value("${spring.datasource.url}") 
private String dbUrl;

Upvotes: 1

Related Questions