In my application, I want my users to be able to configure their db-properties the first time they launch the application. The db-properties will be stored in a property-file locally and spring will use this file for db-setup in the future.
The workflow will be something like:
Start application for the first time
-> 2. display a page with a db-configuration form
-> 3. user submits the form and the input is written to the db property-file locally
-> 4. spring reads the db-properties from the property-file and instantiates datasource, transaction-nmanager, entitymanager-factory and so on.
So the challenge is how spring can start without any db-setup and then instantiate it dynamically without restarting the server when the form is submitted.
Any suggestions? :-)
You can use Spring's Java Config rather than XML, to instantiate the beans programmatically. This will allow you to read the properties file.
Related
To give you some context, we have various microservices and a config server where we store store all the application.yml files.
The problem is when some issue happens, we have to temporarily enable logs for every application so that we can trace the activities done by the user. But this creates a hassle as we have to change the log configs.
So we want to enable logging for only certain users (say, when some issue happens and we want to debug) via changing the yml files so we don't have to touch the code or redeploy the application. Basically we are thinking of creating a application-logging.yml file for all the microservices and in this when we mention
logging:
users:
id: 123
level: warn
all the microservices will pick it up and start generating logs only for this user.
How can I implement this in Spring Boot, if its possible?
Put user ID into the thread context then use a MutableThreadContextMapFilter.
You can specify values via a JSON file, which log4j polls periodically to pick up changes.
I have a single server for Multiple countries lets say [c1,c2,c3 countries have p1,p2,p3 property files], i want to load all of them at once and want fetched/read its values based on country flag(This country flag will be derived based on login user configuration).
I'll say you should use Zookeeper or Spring Cloud config for this.
For a native spring solution you can refer to this answer: Load spring boot app properties from database
However, using Spring cloud will make this easier.
do not judge this question) I want to implement WEB installer for my Spring Boot application, and very interesting moment is that my application plays 2 role: Installer and Backend(Application). When i first run my app i need to tell Spring to not initialize particular beans(Hibernate(while startup application must not to be failed to start because database may not exist), ActiveMq and others beans that will be added in installation process) and show some html pages with installation guide. Also i need to prevent access to endpoints where some logic with database occurs. When installation finished i will create new application.properties or some other file with settings and i tell Spring to initialize all required beans with Hibernate, ActiveMQ and others. Maybe i will make restart of application and new behaviour that based on installation will occur. And in next starts my application will not show installation guide. To simplify the question: I need to change startup behaviour of Spring Boot Application. For fun i can give an example with human: I need to make human live with no organs, and this human will live very good, and if i want i can add organs to human and he will be live very well))
You can use #Profile annotation. Check this link: https://www.mkyong.com/spring/spring-profiles-example/
This is fairly straight forward with a simple Spring DAO approach. However, using MyBatis, is there a way to setup multiple potential datasources?
The best approach I can think of is to use an ArraList of a Bean each containing datasource.driverclass,datasource.url, datasource.username, datasource.password etc.
The values for the datasources are stored in individual properties files. There could be 1 or 10 of these property files (or more).
So for example, one application startup all the property files would be loaded one at a time into an ArrayList. Then, based on the NAME=value line from the property file, we would know which datasource to hit.
So http:localhost:8080/name=db1
... would access all the data from the datasource configured with the name "09". Each property file would contain:
name=db1
datasource.driverclass=jdbc:sqlserver
datasource.url=jdbc:sqlserver://localhost:1433;databaseName=someDBname
datasource.username=user1
datasource.password=pass1
So the identifier here is "name=db1".
Would the best approach from a MyBatis implementation utilise an ArrayList of Beans?
Here are some leads if you want to keep up with multiple DB:
Anyway, I would say datasources shall be managed in the server confiquration instead of in the App.
Then Mybatis main configuration file must be placed in a location added to the classpath, but outside of the app package, because every new datasource must be referenced there inside an environment element.
And for every user request or session (in case of a web app), the configuration will be parsed because SqlSessionFactoryBuilder.build(reader, environment=NAME); must be called to choose the environment (=> the DB).
I ended up using a hierarchical application.yml file detailing the multitenant connection values, based on a selected tenant code.
I m writing one application and want to make it opensource. My app uses access database, and i created manually the datasource using run->odbcad32. When I post that app in site , those who download need to run it without much efforts, and they should not need to create access file and data source.,
1.how can i create the ms access file programatically?
2.how can i create the datasource programatically?
any other ideas to do the same?
Write a batch (*.bat) that start odbcad32.exe and your application.
This sort of thing can be set outside of the application in a config file, not so different from how Spring does it. You can even give them a small gui to set this sort of thing in your application as well, either in the options or even when the application starts up for the first time.
It may make more sense to allow users of your application to pass in the data source.