Could not resolve placeholder in string value

后端 未结 11 1548
故里飘歌
故里飘歌 2020-12-01 01:00

I am trying to use properties from a .properties file, but it doesn\'t seem to work.

Here is my code:

@Service(\"ServiceFTP\")
@Transact         


        
相关标签:
11条回答
  • 2020-12-01 01:29

    In my case, I was careless while merging the application.yml file, and I've unnecessary indented my properties to the right.

    I've indented it like this:

    spring:
        application:
           name: applicationName
    ............................
        myProperties:
           property1: property1value
    

    While the code expected it to be like this:

    spring:
        application:
            name: applicationName
    .............................
    myProperties:
        property1: property1value
    
    0 讨论(0)
  • 2020-12-01 01:32

    My solution was to add a space between the $ and the {.

    For example:

    @Value("${project.ftp.adresse}")
    

    becomes

    @Value("$ {project.ftp.adresse}")
    
    0 讨论(0)
  • 2020-12-01 01:33

    This Issue occurs if the application is unable to access the some_file_name.properties file.Make sure that the properties file is placed under resources folder in spring.

    Trouble shooting Steps

    1: Add the properties file under the resource folder.

    2: If you don't have a resource folder. Create one by navigating new by Right click on the project new > Source Folder, name it as resource and place your properties file under it.

    For annotation based Implementation

    Add @PropertySource(ignoreResourceNotFound = true, value = "classpath:some_file_name.properties")//Add it before using the place holder

    Example:

    Assignment1Controller.Java

    @PropertySource(ignoreResourceNotFound = true, value = "classpath:assignment1.properties")
    @RestController  
    public class Assignment1Controller {
    
    //  @Autowired
    //  Assignment1Services assignment1Services;
        @Value("${app.title}")
        private String appTitle;
         @RequestMapping(value = "/hello")  
            public String getValues() {
    
              return appTitle;
    
            }  
    
    }
    

    assignment1.properties

    app.title=Learning Spring
    
    0 讨论(0)
  • 2020-12-01 01:34

    Deleting or corrupting the pom.xml file can cause this error.

    0 讨论(0)
  • 2020-12-01 01:35

    You can also try default values. spring-value-annotation

    Default values can be provided for properties that might not be defined. In this example the value “some default” will be injected:

    @Value("${unknown.param:some default}")
    private String someDefault;
    

    If the same property is defined as a system property and in the properties file, then the system property would be applied.

    0 讨论(0)
  • 2020-12-01 01:37

    I got the same error in my microservice project.The property itself missed in my yml file.So I added property name and value that resolves my problem

    0 讨论(0)
提交回复
热议问题