Where to save a configuration file uploaded by admin? - java

I am working on a web application, developed using spring mvc and server is tomcat. Now one of the requirement is that admin can upload a spring related service configuration file(which is different from the spring service configuration file residing in src code) with some changes because we want to make it configurable. Now server will be restarted to get the modified changes.
Now I am confused about one thing, where I should upload this file(file system?) so that when server is started then configured listener will pick up new configuration file.
Pls suggest solution considering it as enterprose aplication.

Find out the reasonable place to keep your system files, other than in webapps/.
(I had kept next to log folder.)
Use Spring's PropertyPlaceholderConfigurer to read file locations from properties file.
Use same locations and Create ApplicationContext object, in a factory method (You can pass any number of files).
Use getBean on applicationContext object to use beans.

Related

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.

Externalized configuration setting out of fat-JAR file

I am looking for a way to purely externalize some configuration settings in Spring boot application. For example: when double clicked on the fat-JAR file then it loads configuration from that, say myConfig.config, file which is in the same folder in which the fat-JAR file is. Then read the configuration from there and deploy the web-app. One use case is reading the port number from the config file and start the web-app on port number specified in the config file. If port number needs to be changed then only config file needs to be updated and restart the web-app.
I know that it is possible in .NET. I tried this link[1], but it is specifying config file in command line. Also, the #PropertySource can be used but again it winds up being in fat-JAR. There is Spring Cloud Config as well but I think that it would be overkill for small application. There are lots of tutorials available but they use one of the above mentioned method.
So, Is there any way to achieve that?
If yes, then what are the steps/link for that?
[1] Springboot externalizing log4j configuration
All you need to do is place an application.properties file at the same level as your jar. Spring Boot will find and use the application.properties w/o anything extra.

best way to maintain properties files in spring mvc application

In my Spring MVC application application we are maintaining many configuration files like .properties and .txt files. I want to read all configuration files and keep them in application context level, so that I can access configuration data in my entire application.
Example:
db.properties
db.driveClassName=packages.DataSource
db.URL=some url....
errorCodes.properties
<error-code>=<error description>
ERR001=time out error occured
ERR002=The request was tampered
ERR003=transaction id is missing
Best way is to use #ConfigurationalProperties then they are type safe and you can even validate them look at this guide http://www.baeldung.com/configuration-properties-in-spring-boot

Store properties in database, but override locally

Currently, we store our application's environment properties in a .properties file in the WEB-INF. We want to move them to a database table. But we still want to specify the jndi name, and when running in our test environment locally, we want to be able to override certain properties just for our workspace for test and development.
Apache commons' DatabaseConfigurator seemed nice, but wouldn't play nice with the jndi name being defined as a property in the file. Nothing I did to ask it to look at the property file first worked.
I decided to subclass apache commons' AbstractConfiguration to try to create a single configurator that would check the file and database as I wished, but again, it didn't really work. Spring wants that jndi name absolutely first, probably because the data source has to be passed into the configurator as a parameter.
How can I get what I am after here? Mostly properties in the database, but those that are in the file override them. And jndi name for the datasource should not have to be hardcoded in the spring config.
Why don't you write a ApplicationContext listener that will read the configuration from your DB and inject them in the JNDI? Then you can override the configuration in the JNDI with a context.xml file that will be placed in the src/local/webapp/META-INF/.
This is how we get this working in our webapp.

How to config routes in Spring Cloud Zuul

In Spring Cloud Zuul server we can define all routes which need be redirected via "application.properties".
For example:
zuul.routes.resource.path=/api/**
zuul.routes.resource.url=http://api.com:9025
Once the fat jar is created the "application.properties" is encapsulated into jar, and it's not possible to edit and reload the rules inside the file.
Is there any to inform Zuul about the routes in an external file, and at the same time reload them without stopping the server?
You can use spring cloud config for that.
Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments
http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_spring_cloud_config_client
...a bit late to the party, but...
You can do all that you want with the ConfigServer!
Create an application.yml for config that is common across ALL applications
Create profile specific application-mycommonprofile.yml. As 1. but for the 'mycommonprofile' profile.
Create an appX.yml for each application that is specific to that application.
Create profile specific appX-myprofile.yml. As 3. but for the 'myprofile' profile.
All of these files are optional and are not dependent on any others. You can have an application-mycommonprofile.yml without an application.yml for example.
Hope that helps!
Another late-to-the-party answer, but another way is to use a profile config file, which lives in the filesystem, outside the fatjar.
If the configuration name of your Zuul proxy is 'zuul' and your normal config file is 'zuul.properties' or 'zuul.yaml', then it looks for a profile-specific config file in 'zuul-.properties' or 'zuul-.yaml'.
If you do not specify a profile, then the profile named 'default' is active.
So you can load properties from an external file name 'zuul-default.properties' or 'zuul-default.yaml' (or 'zuul-default.yml', if you use a 3-letter filename extension).
This will then be loaded when no other profile is specified.

Categories

Resources