I am having following properties in a file-
dir.path=path/of/directory
file.path=${dir.path}/file_name
In Spring Bean, I am referring it as given below -
#Value("${file.path}")
String filePath;
However filePath is set by Spring as '${dir.path}/file_name' and it does not resolved completely. I am using Java Configuration ( no xml). I will appreciate all suggestions.
Related
I am developing a spring boot project, and I need some external configuration. So I am storing that in a file called "config.yml" placed in "src/main/resources" folder.
Now I want the properties in config.yml to be injected in my class. Now if we use SnakeYML or any other parser, we would need to make Java classes to define the schema.
What I want is I can read the yml just like properties using #Value annotation. For e.g.
logging:
class:
name:
location:
I need to access "name" or "location" property using
#Value(${logging.class.name})
private String name;
Is there a way to do that in spring boot?
you don't need to add separate yml. you add custom properties to application.yml or application-{env}.yml
Spring recognise it and you can it via
#Value
Spring environment
Using Spring ConfigurationProperties
I have a spring boot 2 app. I am trying to access my application properties file properties through spring Environment as well as #Value property, neither of them works.
#Autowired
Environment env;
logger.info(env.getProperty("app.environment"));
#Value("${app.environment}")
private String _env;
logger.info(_env);
app.environment=LOCAL
Looks like spring boot is not detecting application.properties file at all.
What am I doing wrong here, Thanks in advance
If the injected property is null, it most probably means your bean has not been created yet. Spring boot injects values to bean properties only after the bean has been created.
Here's a simple experiment to demonstrate this behavior:
#Value("${spring.application.name}")
private String instanceApplicationName;
public UserController(#Value("${spring.application.name}") String appNameInConstructor) {
System.out.println("Constructor app name: " + appNameInConstructor);
System.out.println("Instance application name: " + this.instanceApplicationName);
}
Which property would be null? instanceApplicationName or appNameInConstructor?
Yes, it's the instanceApplicationName property that's null because, the constructor is called first and the instance property has not been initialized yet. The appNameInConstructor correctly logs the application name.
So, where you reference the injected properties and how you inject them, both matter.
You can check the entire project here: https://github.com/nkumashi/springboot-webmvc-demo.
How can I put a string stored in JNDI to Spring Boot properties?
A bit more details:
I get my DataSource from JNDI using this property in application.properties:
spring.datasource.jndi-name=my_data_source_jndi_name. Currently, the DB schema is hard coded in application.properties in the following way: spring.jpa.properties.hibernate.default_schema=my_schema. There is a JNDI record that containd the DB schema name. How can I get the schema from JNDI too?
As outlined here:
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
Spring will resolve properties from JNDI and so you can therefore parameterise it in various ways. You can bypass the properties file and use the #Value() annotation directly in Java config
#Value("my.property.in.jndi")
private String schema;
or you can do as below in the properties file:
spring.jpa.properties.hibernate.default_schema=${my.property.in.jndi}
I'm trying to follow the demo https://github.com/sdeleuze/spring-kotlin-functional to create a new Spring Boot application using the annotation-free new approach, released in Spring Boot 2. My problem is how to continue to use Yaml files to configure my application, without using annotations? I would guess it would be something inside the Beans configuration but I dont find any documentation on that subject. Thanx
The beans dsl has an env property that you can use to retrieve any environment property defined in yaml, properties files or command line parameters:
fun beans() = beans {
bean<SomeBeanThatNeedsConfig> {
SomeBeanThatNeedsConfig(env.getProperty("config.value"))
}
}
I found an spring boot sample code in which properties are being injected directly with #Value.
The point is that those properties come from a bean which is not present actually in the code.
The annotations have this aspect:
#Value ("#{envPC['desktop.url']}")
private String url = "";
So I have a couple of questions:
1-how can I define env bean?
2-when defining the env bean, how can I modelate the correct structure for references like: desktop.url, desktop.port....
I hope the questions are clear.
Thanks in advance
Environment bean is implicitly defined within Spring context. Spring collects properties from multiple sources into this bean: JVM options, property files added via #PropertySource, etc.
Also it can be injected like any other bean:
#Autowired
private org.springframework.core.env.Environment environment;
More details can be found at https://docs.spring.io/ or in this nice article: http://www.baeldung.com/properties-with-spring