Could not resolve placeholder in string value

后端 未结 11 1549
故里飘歌
故里飘歌 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:39

    In your configuration you have 2 PropertySourcesPlaceholderConfigurer instances.

    applicationContext.xml

    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="environment">
            <bean class="org.springframework.web.context.support.StandardServletEnvironment"/>
        </property>
    </bean>
    

    infraContext.xml

    <context:property-placeholder location="classpath:context-core.properties"/>
    

    By default a PlaceholderConfigurer is going to fail-fast, so if a placeholder cannot be resolved it will throw an exception. The instance from the applicationContext.xml file has no properties and as such will fail on all placeholders.

    Solution: Remove the one from applicationContext.xml as it doesn't add anything it only breaks things.

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

    This error appears because the spring project doesn't read the file properties (bootstrap.yml or application.yml). In order to resolve this, you must add dependency in your pom.xml

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
        </dependency>
    
    0 讨论(0)
  • 2020-12-01 01:41

    With Spring Boot :

    In the pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    Example in class Java

    @Configuration
    @Slf4j
    public class MyAppConfig {
        @Value("${foo}")
        private String foo;
        @Value("${bar}")
        private String bar;
        @Bean("foo")
        public String foo() {
            log.info("foo={}", foo);
            return foo;
        }
        @Bean("bar")
        public String bar() {
            log.info("bar={}", bar);
            return bar;
        }
        [ ... ]
    

    In the properties files :

    src/main/resources/application.properties

    foo=all-env-foo
    

    src/main/resources/application-rec.properties

    bar=rec-bar
    

    src/main/resources/application-prod.properties

    bar=prod-bar
    

    In the VM arguments of Application.java

    -Dspring.profiles.active=[rec|prod]
    

    Don't forget to run mvn command after modifying the properties !

    mvn clean package -Dmaven.test.skip=true
    

    In the log file for -Dspring.profiles.active=rec :

    The following profiles are active: rec
    foo=all-env-foo
    bar=rec-bar
    

    In the log file for -Dspring.profiles.active=prod :

    The following profiles are active: prod
    foo=all-env-foo
    bar=prod-bar
    

    In the log file for -Dspring.profiles.active=local :

    Could not resolve placeholder 'bar' in value "${bar}"
    

    Oups, I forget to create application-local.properties.

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

    I had the same problem, resolved it by adding

    <filtering>true</filtering> 
    

    in pom.xml :

    before (didn't work):

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>               
            </resource>
        </resources>
    </build>
    

    after(it worked):

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

    After that you just run mvn clean install and deploy application.

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

    I got same error in my Micro-service, whenever you declare @Value annotation in program i.e @Value("${project.api.key}")

    make sure that your application.properties file with same values should not be blank project.api.key= add some values

    MostIMP :otherwise it will throw error "Error creating bean with name 'ServiceFTP': Injection of autowired dependencies"

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