Below is my application.property file
codes:"{alpha:'green',bravo:'red'}"
my config class looks likes below,
#Configuration
#ConfigurationProperties
#RefreshScope
public class ConfigFile {
????
}
My Question is how can I set my git repo values using ConfigurationProperties class ?
Note: Using #Value is not recommended
Thanks in advance.
Related
I have a config folder of xml files that my spring boot app needs , its located on the same level of src folder ... Now i now to locate it in Resources Folder on the same level of application.properties.. There is any way to be able to do that ?
There many way to read your file .properties:
You can read with plain java like this:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
you can use the #PropertySource annotation in a Spring #Configuration class:
#Configuration
#PropertySource("db.properties")
public class ApplicationConfig {
// more config ...
}
//Or if you your config outside of your classpath, you can use the file: prefix:
#PropertySource("file:/path/to/application.properties")
Using Multiple file :
#PropertySources({
#PropertySource("classpath:foo.properties"),
#PropertySource("classpath:bar.properties")
})
public class PropertiesWithJavaConfig {
//...
}
And so on ...
I have a project built up on struts framework but I've integrated Spring Framework in same(using a servlet-config.xml) and using the MVC and AOP in it, now in one requirement I must use one project which is built up on SpringBoot and using #ConfigurationProperties binding an application.yml file with a POJO.
I just need to use the code of that project and hence added the dependency of that project. Is there any way I can use #ConfigurationProperties to bind the POJO with the yml file in my project. I tried but bean is not binding with the yml file entries.
As far as I know #ConfigurationPropertiesis part of SpringBoot features only.
Please give your useful suggestion.
You can do something like for example below:
if your yml file has properties containing same prefix then you could use as below:
POJO class:
#Configuration // add this if your spring boot version is < 2.2 or else please donot add.
#ConfigurationProperties(prefix = "spring.db.props") //example
public class MyPojoProps{
private String userName;
private int password;
private String uri;
// standard getters and setters
}
your application.yml may be something like below:
application.yml
spring:
db:
props:
userName:
password:
uri:
Please refer here for more information
I have a property bean (example):
#Data
public class MyProperty {
private String name;
private String address;
}
and I have a custom yml file in classpath named: my_property.yml
my.property:
name: testName
address: testAddress
How to load this file to my property bean?
Not using #PropertyResource, because I want to use the yml file.
Thanks.
#PropertySource is the way to include external/custom properties but unfortunately is doesn't work with yaml files due to this issue.
You need to load these yaml files on your own by simply writing EnvironmentPostProcessor and adding it META-INF/spring.factories.
This is described in below Spring documentation (slight difference among versions).
Spring boot 1.5.x
Spring boot 2.x
There are two properties you can look at -
spring.config.name - If you don't like application as your file name
for eg-
java -Dspring.config.name = my_property myjar.jar
spring.config.location - To tell where your files lie
More information is well-documented here
spring documentation
Follow: http://www.baeldung.com/spring-yaml
The relative path of application.yml file is /myApplication/src/main/resources/application.yml.
and access is:
#Autowired
private YAMLConfig myConfig;
I put my application.properties file at src/main/resources/
End my #SpringBootApplication read.
application.properties file
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=***
spring.datasource.username=***
spring.datasource.password=***
This is how I have set up external configuration file:
/** for .yaml file
#Configuration
#PropertySource("classpath:waterlvl.yaml")
#ConfigurationProperties("Alerts")
public class WaterLvlAlertConfig {
/** for .properties file
#Component
#PropertySource("classpath:waterlvl.properties")
public class WaterLvlConfig {
Properties are not being picked up, and my files are inside the resources file. I think the path I'm providing is wrong. What am I doing wrong?
In spring boot #PropertySource cannot be used when using .yaml configuration. Refer to point 24.6.4 YAML shortcomings in externalized configuration documents. Also, a similar question is answered earlier, refer - Spring boot external configuration of property file
Is it possible to move a hibernate configuration file containing the db connection details outside of the hibernate project and connect to it?
My current configuration is as follows, but I would like to move my file from outside the project and still connect to my data sources. How can I do so?
#Configuration
#EnableTransactionManagement
#PropertySource({"classpath:hibernate.properties"})
#EnableJpaRepositories(basePackages = "com.my.packages", entityManagerFactoryRef = "schemaOneManagerFactory", transactionManagerRef = "schemaOneTransactionManager")
public class SchemaOneDataSourceConfig
{
//configuration methods
}
Do I need to make a change to line: #PropertySource({"classpath:hibernate.properties"}) ?
From the JavaDocs of PropertySource
Indicate the resource location(s) of the properties file to be loaded. For example, "classpath:/com/myco/app.properties" or "file:/path/to/file".
You just need to change a prefix to file:
#PropertySource({"file:/path/to/hibernate.properties"})
If you are using Spring Boot, you can put the properties in application.properties and you can have that file in your JAR, but also override it by putting it in a config sub-folder (relative to your running directory).
See http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files for more info.