#ConfigurationProperties usage in spring - java

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

Related

Binding application.properties to a class from external library in Spring

Let's say I have an external jar (that supposed to work in spring boot env) that has this simple class:
#Component
#ConfigurationProperties("test")
public class NetworkConfig {
//getters/setters
...
}
Now I use this jar as dep in a Spring project (NOT Spring Boot!!).
I have an application.properties file in that project and want to load properties from it to this class and it should be available in a context. How would I do it?
I also need to mention that external jar is my lib and I can modify it if needed.
#ConfigurationPropertiesScan ("path_to_property")

Spring Boot Property file precedence

In my Spring Boot program I'm getting a failure due to a bad property value on load. In particular, it uses the DB2 hibernate dialect but it's not defined in the property file I thought I was using.
Assuming no annotations, where does Spring look for the properties file? Yes I know it normally resides in src/main/resources/application.properties
What if I have a property in my test cases; does it ignore the one in main and use the one in test? Or does it start with the main version and let the test one override the main where it applies?
Does the application profile affect the property file used? Some people use the same application.properties file name in both main and test.
If I do have a TestSource annotation with a class path location, does it still augment it with something somewhere else?
Finally, how can I get Spring to tell me everywhere it looked for properties and not just one of them?
#Woodsman
It's possible to use many settings in each profile/environment in spring application on src/main/resources/ folder
application-dev.properties
application-local.properties
application-onlytests.properties
application-prd.properties
application.properties
the name after hyphen will be the name profile got by spring
Ex: application-{myenviroment}.properties
When you start spring application will be used application.properties by default.
You can tell spring to use an specific properties passing the environment in one of ways below:
Putting spring.profiles.active=prd inside application.properties file
Passing by parameters when start spring app --spring.profiles.active=local
Running your jar on command line java -jar myjar.jar --spring.profiles.active=dev
Setting an environment var in your machine/docker/container SET SPRING_ACTIVES_PROFILE=local
There are other ways using annotations on beans, passing jvm arguments and others
If you need run your tests in a specific configuration ( vars, database, settings ), It's possible to pass which .properties will be used
#ExtendWith(SpringExtension.class)
#AutoConfigureMockMvc
#SpringBootTest
#TestPropertySource(locations = "classpath:application-onlytests.properties")
public class RunTest_from_onlytests_properties {
#Autowired
private MockMvc mockMvc;
#Test // org.junit.jupiter.api.Test
public void test() throws Exception{
// ...
}
}

Spring Boot configuration conventions

For Spring Boot app, we use application.yml or .properties variant to write spring-specific app configuration. For example:
server:
port: 7779
management:
endpoints:
web:
exposure:
include: gateway
Is there some convention how to name application domain-specific configuration? Where to put this config: in same application.yml or it should be separate file? How to name those properties? Example
adder-service:
max-number : 33
some-other:
a:
b: 44
Should we generate additionally META-INF/additional-spring-configuration-metadata.json for this app-specific config?
A good practice is to think about configuration file as about a configuration structures with defaults:
myapp:
myprop1: abc
myprop2: xyz
The actual values come from the environment:
MYAPP_MYPROP1=abc
MYAPP_MYPROP2=xyz
Use type-safe configuration in code with an application specific prefix.
#ConfigurationProperties("myapp")
class MyAppProperties {
private String myprop1;
private String myprop2;
// getters & setters ...
}
If the configuration is specific fo a particular deployment scenario, put it into a separate file managed by a Spring profile eg. application-aws.yml etc.
spring.profiles.active=aws

springboot how to load properties from custom yml

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=***

Run my integration tests with a different *.properties file using Spring 4.0.6.RELEASE

I am currently using Spring 4.0.6.RELEASE and have the following Spring configuration file:
#Configuration
#PropertySource({"classpath:config.properties"})
public class MyServiceConfig {
...
I was wondering if there is a way to run integration tests for my component-annotated-classes with a different properties file (let's say test-config.properties) in order to give different values for my value and autowired annotated properties and methods.
NOTE: I know that Spring 4.1.x comes with #TestPropertySource which helps to achieve it. But upgrading Spring to later versions is not an option.
Yes. Specify "profile" for integration tests.
#Configuration
#PropertySource({"classpath:test-config.properties"})
#Profile("integration-test")
public class MyServiceTestConfig {
...
In order to use this profile when testing repository use #ActiveProfiles annotation
#ActiveProfiles("integration-test")
#RunWith(SpringJUnit4ClassRunner.class)
public class MyRepositoryTest {
...

Categories