config mongodb in spring, application.properties OR AbstractMongoConfiguration? - java

We can use AbstractMongoConfiguration (http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/config/AbstractMongoConfiguration.html) to do the mongodb configuration. Also, we can use application.properties to do config (http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html). Which one is better?

This is definitely an opinion-based question and answer.
The answer is simple: My opinion is that spring-boot really encourages you to use application.properties whenever possible. So, I'd says:
it is better to use application.properties with spring-boot, rather
than java config
spring-boot's AutoConfiguration happens so early in the spring lifecycle, that it is nearly impossible not to use application.properties (to allow AutoConfigurations to load their properties).
Also, spring-cloud-config allows loading properties remotely, so that would be another possible future advantage of using this mechanism.

Related

what is recommended to store project configuration details in spring boot?

I'm a beginner with spring boot and java. My question is what is recommended to store Environment configuration details such as bigquery database name, aws service unqiue ids which are used in my project.
So, It is better to store in a class which is in core project or it is better to store in application.yml file
Because spring boot is so lightweight, a lot of projects use Spring boot for microservices. and in a microservice world, it's best and almost needed to store your configs in a centralized and secure place, so you can change any of them when you want.it's hard to save them in Java classes but you can easily save them with .yml or properties. in almost all cases .yml files will be better.
I think there are some options to save the information of the configurations in an application. One of them is use a *.yml to save all the information because you can have different configurations each of them per environment

Dynamically loading Spring Beans and Components in Spring Boot

I'm trying to make a Spring Boot app where plugins are loaded dynamically from JARs at runtime. I also want the plugins to have access to all the Spring Boot features, most prominently Spring Data JPA. I've already figured out how to load classes from JARs, and now my problem is how to "hook up" the loaded classes (that might be Beans, JpaRepositories etc.) to "work with" my main Spring Boot application.
I also might in the future want to have my own annotation system for doing different things with the main app from the plugins, (that I know how to do using reflection) and I would want to still be able to do it after I manage to sort the Spring stuff out.
I imagine I have to tell Spring somehow to additionally look for #Components and other meaningful classes from those JARs, when it's scanning for annotations. I tried with #ComponentScan's basePackageClasses attribute but that needs to be constant, and hard-coding this is not an option for what I wanna do.
So is what I want to achieve even possible? And if it is, then can I do it through Java code, or is it maybe achievable by writing some XML configs?
When you start an spring app, beans are loaded and hooked in its contexts, so if you want to add more to it manually you might need to reload the whole context which may not be a good idea for an spring boot app.
Instead, I would suggest to use spring profiles, so you can define different configurations and based on what you want you can simple enable the one you need.
Find out more at:
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-profiles
Hope this helps!

How can I specify an external configuration file for a java application that is not spring boot

Background
I have built a console java application using kotlin and gradle.
The gradle file creates a fat jar which I can run from the command line using
java -jar <project>.jar
The jar contains the application.properties file from which properties are read.
Problem
I would like to specify on the command line that the application.properties file should be read from some external path.
When using spring boot, I have used
java -jar -Dspring.config.location=somepath/application.properties <project>.jar
and this works.
But it does not seem to be working in the non-spring boot application
Question
Is it possible to specify external configuration on the command line for non spring boot applications?
Spring boot has a whole chapter in the documentation which deals with various ways of configuration.
Obviously if you don't have spring boot you should implement something similar to it by yourself.
First thing you should decide - at which level you need the configuration to be integrated into your application:
Do you only want to read the key/values from command line or maybe rely on environment variables or system properties?
In general, what is the source of your configuration: Yaml? Properties file? maybe consul or etc.d?
Do you want to create a java object that reflects the configurations that you've read (like classes annotated with #ConfigurationProperties in spring boot do?
Do you want to support only one source of configuration or you want the various sources of configurations to be supported?
If you ware using Spring, do you want configuration properties to be automatically injected into beans?
If you're planning to use properties/yaml (like application.properties in spring boot) - where do you want to place them? Non spring boot application won't read them "auto-magically", you'll have to implement this logic.
Are you planning to deal with profiles (non-spring-boot application still supports flavors of loading different beans depending on specified profile).
Spring boot has answered all these questions and more.
Here are some options that you might want to give a try to if you're running outside the spring boot context but still have spring application:
Since spring 3.1, I guess, there is a#PropertySource annotation that you can use to make spring load properties from the file in the classpath or some "place" in the filesystem. This article summarizes the usage of this method as well as compares what spring boot has up on its sleeves as opposed to regular spring application. This is also a nice tutorial that covers regular spring features.
Something out of spring eco-system but still can be useful: apache common configuration project. There are some workarounds to integrate it with spring application, see here
Considering all the answers here I concluded that though it is possible to enable external configuration in applications that are not spring boot, it does require some effort.
Therefore I decided to use Spring Boot in the container.

How to configure DEV part in Spring Application?

I started Spring Project. I have added some dependencies.
Previously, I were commenting some dependencies as Security, Mysql driver, if it is not needed for me.
But how to disable them without commenting dependencies?
Maybe some properties are needed for that?
For example, I want to use H2Database to test project, but PostgreSQL I will use later. Or Spring Security, for example.
Do I have to use Spring Boot Profiles?
You are probably looking for profiles. You can read about it more eg: https://www.mkyong.com/spring/spring-profiles-example/
You may need to implement profiling for this. You can read about it more.
https://dzone.com/articles/spring-boot-profiles-1

Implementing Caching in existing application

If I am supposed to implement Caching in existing Spring application for all web service as well as database call, what would be the best way to implement it? I mean any of the design patterns and caching mechanism that can be used with other required stuffs.
I would appreciate any suggestion provided.
Since you are already using Spring stack, Spring Caching could be a alternative you can consider as it will require very less integration and most of the things come out of the box. You can take a look at simple examples here and here too to get a feel of how it works. However if you want more control on the actual underlying cache implementation and the code to interact with that you can roll out your own easily too, though that will require more code to write at your end.
If you are using springboot you can use the
#EnableCaching and #Cacheable
Since Spring Boot automatically configures a suitable CacheManager to serve as a provider for the relevant cache.
You can find more on https://spring.io/guides/gs/caching/
In addition to Guru's answer.
You can find more info about Spring Boot Caching on https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html and https://docs.spring.io/spring/docs/4.3.14.RELEASE/spring-framework-reference/htmlsingle/#cache
#EnableCaching is for configuration and #Cacheable is for trigger cache object.

Categories

Resources