Springboot annotation @value always reported could not resolve placeholder problem

Source: Internet
Author: User

Scene:

Two configuration files: db.properties,application.properties

Referencing db.properties in the database configuration

class= "..." >        <property name= "Locations" >            <list>                <value>classpath*:/ db.properties</value>                <value>file:/etc/db.properties</value>            </list>        </ Property>    </bean>

At this time, application.properties inside the property will not be loaded into the, if you use @value, will be reported could not resolve placeholder

@Controller  Public class Firstcontroller {        @Value ("${welcome.message}")    private  String test;    @RequestMapping ("/GETW")    public String Welcome (map<string, object>  Model) {        //model.put ("message", this.message);         System.out.println (test);         return "My";    }}

Such use will be reported could not resolve placeholder

Solve:

Put the contents of the db.properties into the application.properties, and then cite this side:

class= "..." >        <property name= "Locations" >            <list>                <value>classpath*:/ application.properties</value>                <value>file:/etc/application.properties</value>            </ list>        </property>    </bean>

Or two of files are loaded

class= "..." >        <property name= "Locations" >            <list>                <value>classpath* :/application.properties</value>                <value>classpath*:/db.properties</value>                < value>file:/etc/application.properties</value>            </list>        </property>    </ Bean>

The reason is spring's loading mechanism: the spring container uses the discovery mechanism of reflection scanning, A bean that detects a org.springframework.beans.factory.config.PropertyPlaceholderConfigurer in the spring container stops the remaining propertyplaceholderconf Igurer Scan (Spring 3.1 already uses propertysourcesplaceholderconfigurer instead of propertyplaceholderconfigurer), so depending on the order of loading, The second property-placeholder configured is not loaded by spring, so the placeholder is not parsed when you use @value injection

Springboot annotation @value always reported could not resolve placeholder problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.