I am trying to use properties from a .properties
file, but it doesn\'t seem to work.
Here is my code:
@Service(\"ServiceFTP\")
@Transact
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.
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>
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.
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.
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"