How to load Application.properties file dynamically based on Profile in Spring MVC?
up vote
2
down vote
favorite
Hi I am learning Spring MVC and I want to know How to load application.properties file dynamically.
I am adding HibernateConfig.java file and AppConfig.java file. I want to load application properties file dynamically using profiles. For Example: dev, test, prod. I have tried to use dynamic name application-{profile}.properties and also tried profile annotation. but not able to understand how they are actually working. I have created a different application.properties files.
- application-dev
- application-test
- application-prod
This property file contains my DB related data. but I don't know how to set profile and how to load PropertySource based on a profile.
I have set the active profile in my appConfig file. Please help me in understanding how to configure profile and application.properties using spring MVC Java-based configuration. I have searched and found many solutions for XML based configuration but I haven't found any proper answer for java based configuration.
HibernateConfig.java
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.project.configuration"})
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean sessionFactory(){
return sessionFactory;
}
@Bean
public DataSource dataSource(){
/* loading DB */
return dataSource;
}
@Bean
public Properties hibernateProperties(){
}
}
AppConfig.java
@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
super.onStartup(servletContext);
servletContext.setInitParameter("spring.profiles.active", "dev");
}
java spring spring-mvc
add a comment |
up vote
2
down vote
favorite
Hi I am learning Spring MVC and I want to know How to load application.properties file dynamically.
I am adding HibernateConfig.java file and AppConfig.java file. I want to load application properties file dynamically using profiles. For Example: dev, test, prod. I have tried to use dynamic name application-{profile}.properties and also tried profile annotation. but not able to understand how they are actually working. I have created a different application.properties files.
- application-dev
- application-test
- application-prod
This property file contains my DB related data. but I don't know how to set profile and how to load PropertySource based on a profile.
I have set the active profile in my appConfig file. Please help me in understanding how to configure profile and application.properties using spring MVC Java-based configuration. I have searched and found many solutions for XML based configuration but I haven't found any proper answer for java based configuration.
HibernateConfig.java
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.project.configuration"})
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean sessionFactory(){
return sessionFactory;
}
@Bean
public DataSource dataSource(){
/* loading DB */
return dataSource;
}
@Bean
public Properties hibernateProperties(){
}
}
AppConfig.java
@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
super.onStartup(servletContext);
servletContext.setInitParameter("spring.profiles.active", "dev");
}
java spring spring-mvc
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Hi I am learning Spring MVC and I want to know How to load application.properties file dynamically.
I am adding HibernateConfig.java file and AppConfig.java file. I want to load application properties file dynamically using profiles. For Example: dev, test, prod. I have tried to use dynamic name application-{profile}.properties and also tried profile annotation. but not able to understand how they are actually working. I have created a different application.properties files.
- application-dev
- application-test
- application-prod
This property file contains my DB related data. but I don't know how to set profile and how to load PropertySource based on a profile.
I have set the active profile in my appConfig file. Please help me in understanding how to configure profile and application.properties using spring MVC Java-based configuration. I have searched and found many solutions for XML based configuration but I haven't found any proper answer for java based configuration.
HibernateConfig.java
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.project.configuration"})
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean sessionFactory(){
return sessionFactory;
}
@Bean
public DataSource dataSource(){
/* loading DB */
return dataSource;
}
@Bean
public Properties hibernateProperties(){
}
}
AppConfig.java
@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
super.onStartup(servletContext);
servletContext.setInitParameter("spring.profiles.active", "dev");
}
java spring spring-mvc
Hi I am learning Spring MVC and I want to know How to load application.properties file dynamically.
I am adding HibernateConfig.java file and AppConfig.java file. I want to load application properties file dynamically using profiles. For Example: dev, test, prod. I have tried to use dynamic name application-{profile}.properties and also tried profile annotation. but not able to understand how they are actually working. I have created a different application.properties files.
- application-dev
- application-test
- application-prod
This property file contains my DB related data. but I don't know how to set profile and how to load PropertySource based on a profile.
I have set the active profile in my appConfig file. Please help me in understanding how to configure profile and application.properties using spring MVC Java-based configuration. I have searched and found many solutions for XML based configuration but I haven't found any proper answer for java based configuration.
HibernateConfig.java
@Configuration
@EnableTransactionManagement
@ComponentScan({"com.project.configuration"})
@PropertySource(value = {"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean sessionFactory(){
return sessionFactory;
}
@Bean
public DataSource dataSource(){
/* loading DB */
return dataSource;
}
@Bean
public Properties hibernateProperties(){
}
}
AppConfig.java
@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
super.onStartup(servletContext);
servletContext.setInitParameter("spring.profiles.active", "dev");
}
java spring spring-mvc
java spring spring-mvc
asked Nov 14 at 18:46
Aks 1316
477
477
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I think you cant set this parameter at that time, its already too late. You have to start the app with specified profile (or set it in bootstrap file) . You can pass it as an argument or place it in:
application.properities
Under key: spring.profiles.active
When you set this to 'dev' it will read main application.properities and then the profile one. More about how to set it:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.
– Aks 1316
Nov 16 at 1:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I think you cant set this parameter at that time, its already too late. You have to start the app with specified profile (or set it in bootstrap file) . You can pass it as an argument or place it in:
application.properities
Under key: spring.profiles.active
When you set this to 'dev' it will read main application.properities and then the profile one. More about how to set it:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.
– Aks 1316
Nov 16 at 1:41
add a comment |
up vote
0
down vote
I think you cant set this parameter at that time, its already too late. You have to start the app with specified profile (or set it in bootstrap file) . You can pass it as an argument or place it in:
application.properities
Under key: spring.profiles.active
When you set this to 'dev' it will read main application.properities and then the profile one. More about how to set it:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.
– Aks 1316
Nov 16 at 1:41
add a comment |
up vote
0
down vote
up vote
0
down vote
I think you cant set this parameter at that time, its already too late. You have to start the app with specified profile (or set it in bootstrap file) . You can pass it as an argument or place it in:
application.properities
Under key: spring.profiles.active
When you set this to 'dev' it will read main application.properities and then the profile one. More about how to set it:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
I think you cant set this parameter at that time, its already too late. You have to start the app with specified profile (or set it in bootstrap file) . You can pass it as an argument or place it in:
application.properities
Under key: spring.profiles.active
When you set this to 'dev' it will read main application.properities and then the profile one. More about how to set it:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html
answered Nov 15 at 15:01
marekk
637
637
I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.
– Aks 1316
Nov 16 at 1:41
add a comment |
I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.
– Aks 1316
Nov 16 at 1:41
I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.
– Aks 1316
Nov 16 at 1:41
I have tried to put spring.profiles.active in application.properties. But It is giving me an error and I am not sure it will work in Spring MVC. spring.profiles.active is for spring boot only.
– Aks 1316
Nov 16 at 1:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306872%2fhow-to-load-application-properties-file-dynamically-based-on-profile-in-spring-m%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown