Connection to Any one Data base in SpringBoot - java

Is there any way to load the respective hibernate config based on the property configured in a properties file.
I have an application to connect to Any database with the same schema through hibernate configs. Right now, I have created two hibernate configs One for DynamoDB another for all hibernate supported SQL's
I wanted to load only respective config by ignoring other config's.
i think we can specify like below
#Profile("prod")
#Configuration
Is it possible to create another property like profile.

There can be multiple profiles active at the same time in your application.
So you can have profiles for specific databases along with the profiles responsible for environment type.
Then, you can define your properties in files called application-dynamodb.properties and application-other.properties

Yes you can use #profile concept here.You can maintain multiple properties file with different configuration and activate that specific configurations for example by adding
spring.profiles.active=dev in application.properties, if application-dev.properties contains the required db configurations.But then you need to add #profile("dev") in configuration class while you are initializing/creating DB connection working with profile

Related

How to load the properties based on the environment defined in application.properties in Spring application. NOT with Springboot

I'm new to spring and I'm studying it. And stumbled upon the #Profile annotation.
I want to write a simple project with Spring (not Springboot) to learn how to load properties based on the environment using #profile annotation. Almost everywhere, the examples (Ex1, Ex2) I see only with the Springboot. I'm wondering whether we cannot write a Spring application that can dynamically load the properties based on the environment (dev, prod).
Some examples ( Ex3, Ex4, Ex5) show with the #Profile but those have hardcoded the bean details for each environment like below. Is this how we have to write the property loading?
#Profile("dev")
#Bean
public String devDBCcnnection() {
System.out.println(dbConfiguration.getUrl());
return "DB Connection for Dev";
}
#Profile("test")
#Bean
public String devTestCcnnection() {
System.out.println(dbConfiguration.getDriverClassName());
return "DB Connection for Test";
}
#Profile("prod")
#Bean
public String devProdCcnnection() {
System.out.println("DB Connection for Prod");
return "DB Connection for Prod";
}
It has to write a bean for each profile like in the above example?
Can someone tell me using #Profiles, can't dynamically load the property values like in Spring applications?
Appreciate it if you can give the samples with Spring 5
Almost everywhere, the examples (Ex1, Ex2) I see only with the
Springboot. I'm wondering whether we cannot write a Spring application
that can dynamically load the properties based on the environment
(dev, prod).
Spring boot uses the spring context. The spring context allows you to use profiles. Therefore no problem using profiles with simple Spring project (non spring-boot).
There are many ways that you can use Profiles.
One of them is the example that you gave with specific beans that have #Profile and get registered in spring for a specific profile.
Another one, more commonly used in enteprise applications is to ship a jar application with multiple application.yaml files. So for example you ship your application, containing dev-application.yaml and qa-application.yaml. You can then start your application selecting a specific profile to be active. Then that specific application.yaml will be used when the application starts up to build the spring context. So the aplication will be started with qa-application.yaml and will have a connection to the QA database.
But be careful the default application.yaml will also be loaded. The specific application.yaml for example qa-application.yaml will be loaded on top of default application.yaml.
The following article contains very good information about spring profiles
spring profiles article
Considering my example here, I quotte something relevant from that article.
The Default Profile The default profile is always active. Spring Boot
loads all properties in application.yml into the default profile. We
could rename the configuration file to application-default.yml and it
would work the same.
Other profiles will always be evaluated on top of the default profile.
This means that if a property is defined in the default profile, but
not in the qa profile, the property value will be populated from the
default profile. This is very handy for defining default values that
are valid across all profiles.
In order to activate a specific profile
For non spring-boot projects here is a very good answer spring active profile
For spring-boot projects you can
Use a system variable to start your jar file
java -Dspring.profiles.active=qa -jar myApp.jar
Use an environment property to start your jar file
export SPRING_PROFILES_ACTIVE=qa
java -jar myApp.jar

Spring boot Load configuration per environment using PropertySource

I'm trying to add authorization to several microservices. Given all the services share similar authorization process, I want to extract the logic to a shared library.
I managed to create library, but I realise all the configurations need to be set in the application.yml file in the microservice which calls the library. I don't want to expose some of the configurations at service layer though.
After some searches, I found I could set #PropertySource("library.properties") in my library's configuration class to force reading properties from the specified .properties file within the library.
The problem now is I want to set different values for different environments, e.g. authorization URL for test and production would be different. How can I configure the file so that the configuration class would read same property value based on active profile (e.g. environment = test/staging/production)?
You can have multiple property files such as "application-environment.yml” in your resource folder. Spring framework picks the right one based on the active profile.
For example, if you define a “staging” environment and have a staging profile and then your property file should be named as application-staging.yml.

Multiple schema for SQL Server in Spring boot

My schema names are different for Test and Prod environments. How to access them from properties file? I want to read the schema from the properties file based on the environment. Tried the link suggested by adding in datasource config but it is not working. The database is SQL server.
you need to create a properties file for each profile (applications-test.properties and applications-prod.properties) and for each env you put your database configuration.
Make sure to use the right profile in your code.

How to load configurations in spring application from two sources where configuration from first source is required to load second one

I have spring based java application, The application support different databases and can be deployed on various containers. We have two type of configuration sources:
- Properties file: Bare minimum configuration to start the application. i.e application datasource JNDI name and database type.
- Config stored in DB as JSON/ hocon: Other configurations used by application.
what is the best way to have it implemented in spring context file, so that these configurations can be made available to the spring beans. The key problem here is to extract configuration from DB I need values from properties file (to locate the datasource).
Is to possible to have overriding feature where properties file have some default configurations, which are overridden by configuration loaded from DB. I have tried using multiple properties file, but not sure how I can have two property sources.

Profile Inheritage In spring boot

Is it possible to have a profile in spring boot and also another profile that inherits most of the parent values and beans?
For example I have two profiles
staging and staging-task.
I want staging-task to inherit the database configuration of the staging profile however I want it to override the jpa configuration.
Is profile inheritance available to #Configuration beans.
Yes. Its possible to have spring profiles in spring boot.
For your problem, put your common database configuration in application.yml(default profile).
And you can override the other properties in application-stage.yml.
Spring will read the properties from application.yml and override from application-stage.yml when active profile is stage.
TL;DR
The active profiles will cause respective application-$profile.properties to be read (if they exist) in the order the active profiles are defined. Later read properties override earlier ones. That will give you the means to do smth. like an hierarchy.
Long Version
There is no such thing as profile-inheritance in spring but it can be mimicked as written in the answer by JRR.
For how it actually works read: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties.

Categories

Resources