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
Related
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!
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.
When I worked with grails I faced a plugin https://grails.org/plugin/spring-security-ui which allowed to CRUD Users and Roles with corresponding UI so one didn't need to write code for CRUD/filter Users and Roles. Now I work with Spring Boot and writing web-application and looking for something similar (to https://grails.org/plugin/spring-security-ui) some dependency plugin etc which would introduce similar functionality. Is there someting similar for Spring Boot or I need to write the functionality from the scratch?
Thanks, cheers
I think Spring Vaadin should manage with that.
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.
Can anybody tell me a basic configuration to use String dependency injection? What are the minimum required jars?
At the moment I'd like to use only Inversion Of Controll, maybe later I'll integrate ORM.
Thanks
Try Spring roo, there is nothing simpler than that to get a full fledged Spring based web application working, with all the dependencies wired together.
Spring STS (an Eclipse distribution customized for Spring available for free from SpringSource) contains several Spring project wizards that produce IoC enabled projects that you could use as examples.
Almost all Spring examples use Maven to define dependencies and download jars automatically from repositories available on the internet.