0

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?

4
  • Read your code again (small hint spring.datasource.url != jdbc.url)..... Also I would say what you are doing (first using maven to replace variables) is wrong to begin with... Commented Jan 27, 2017 at 9:28
  • thanks.. what an error... but why using maven with profiles to set properties is wrong? Commented Jan 27, 2017 at 9:43
  • Why are you constructing the Connection manually anyway? Doing this sort of thing automatically is the whole point of using Boot. Commented Jan 27, 2017 at 9:47
  • Why it is wrong, because it means you are creating artifacts per environment whereas you should be using Spring Profiles and only build a single artifact. When building artifacts per environment (dev, test, prod) you are basically going live with an untested artifact. Commented Jan 27, 2017 at 10:16

2 Answers 2

1

You should load the value like this:

@Value("${spring.datasource.url}") 
private String dbUrl;
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you need specify delimiters, inside your plugins.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.