简体   繁体   中英

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

spring.datasource.url=@jdbc.url@

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?

You should load the value like this:

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

Maybe you need specify delimiters, inside your plugins.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM