I made a java library that has component and configuration classes.
When I use the library in other spring boot services, the beans are not registered because the component and configuration classes are not in the classpath.
I know we can use #ComponentScan but I don't want to change the service's code.
Is there a way of adding the classes to the classpath using application.properties?
Or is there anything I can do in the library so that the beans get registered?
If you are using Spring Boot, you can take advantage of Spring Autoconfiguration.
For that, you need to place a file spring.factories in META-INF/spring.factoriesin your library's .jar file. If you are using Gradle or Maven as a build tool and the standard folder structure, the file path is src/main/resources/META-INF/spring.factories.
Here's an example from a library I wrote:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.requirementsascode.spring.behavior.web.BehaviorConfiguration,\
org.requirementsascode.spring.behavior.web.SerializationConfiguration,\
org.requirementsascode.spring.behavior.web.BehaviorController
As you can see, after the first, Spring specific line, you list all of your configuration classes.
You can learn more about autoconfiguration here, for example.
You can do this using the autowireBean() method of AutowireCapableBeanFactory.
Read more here: AutowireCapableBeanFactory
Related
We have a java project which is not on spring boot.
Its a normal spring project (spring version 4.1.4) and we have a lot of values defined under the spring bean annotated function, using the #Value annotation.
I am required to override those properties using a property file without having to tinker with the java class. Could you please help me understand, how can I achieve that?
I understand in Spring Boot projects we can do that using "spring.config.location" cmd argument but that will not work here I assume?
I am going to create Java Application that can load external jar files at runtime by FileChooser. I am using Spring Framework, and I want to load jar file and its applicationContext.xml file and inject its dependencies dynamically. I tried to achieve this by OSGi, but it seems very complicated so that I am searching another appropriate variants.
I want to make something like Intellij IDEA plugin installation from the disk.
How can I do this? (After the jar file chosen restarting an application also accepted)
I realy like your approach, unfortunately spring has lifecycles that are strict. As you might know, spring autowires "beans" only. Exactly one lifecycle registers the different bean candidates. After that lifecycle spring (by default) does not accept new classes.
You must use the spring-osgi.
If you only need the CDI part out of spring, you might like to use a different CDI like red hat's jboss server.
I am new to spring-boot. I am building a rest based application using spring-boot and was working on setting up security using spring-security. It is my understnading that I can setup spring-security with either xml config or with Java config.
However, I found the following in spring-boot documentation. https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-configuration-classes.html
It is in favor of using Java Config as opposed to XML config. Changes in Java config requires recompilation. Yet, it makes me think why the documentation favors Java Config.
Configuration classes Spring Boot favors Java-based configuration. Although it is possible to call SpringApplication.run() with an XML
source, we generally recommend that your primary source is a
#Configuration class. Usually the class that defines the main method
is also a good candidate as the primary #Configuration.
Many Spring configuration examples have been published on the Internet
that use XML configuration. Always try to use the equivalent
Java-based configuration if possible. Searching for Enable*
annotations can be a good starting point.
15.1 Importing additional configuration classes You don’t need to put all your #Configuration into a single class. The #Import annotation
can be used to import additional configuration classes. Alternatively,
you can use #ComponentScan to automatically pick up all Spring
components, including #Configuration classes.
15.2 Importing XML configuration If you absolutely must use XML based configuration, we recommend that you still start with a #Configuration
class. You can then use an additional #ImportResource annotation to
load XML configuration files.
There are some advantages
Java is type safe. Compiler will report issues if you are configuring right bean class qualifiers.
XML based on configuration can quickly grow big. [Yes we can split and import but still
Search is much simpler, refactoring will be bliss. Finding a bean definition will be far easier.
There are still people who like XML configuration and continue to do it.
For more info you can refer Java configuration advantages Some more reasons
I am new to Spring and I have tried Googling this, but I am not finding any practical guidance.
I have a Maven Spring utility application that has its own Spring configuration.xml.
I would like other consuming applications (also Maven and Spring most likely) to call this utility by including it in their classpath, and with a minimimum of adjustment, just have the utility application work.
In summary, I would like to have a jar containing my utility code and Spring xml files, then place that jar in the classpath of a consumer application with a minimum of adjustment to the consumer's spring config or command line, and have the utility just work in the consumer application context.
What is the best way to accomplish this?
The scenario you're describing is what Maven was made for. You can simply make the utility module a stand-alone Maven module (with its own pom.xml) that will produce a jar artifact (which is the default type). This jar should and will contain all the Spring configs as well. Later on, when ever you need it, you just declare a dependency to the utility artifact and Maven will make sure it ends up on the classpath so you can use its features.
I am trying to create a java email batch program that sends an email with an attachment each day to a specific email address, and I have to use Spring as the framework for this program. It is not going to be a web application, but since I'm implementing Spring into this, how would I go about this? I am totally new to Spring (and Java for that matter), but am unsure of which direction I need to go. Which jar files do I need? Spring Batch or Spring Framework? Also, where can I download the jar files for Spring Framework? The spring.io site won't let me download those jar files.
I very strongly suggest you use a build tool that handles dependency management. Such tools are Ant+Ivy, Maven and Gradle. They will take care of downloading the appropriate jars based on your declaration of what dependencies you need and will take care of all the transitive dependencies.
One good way of getting started with Spring Batch is to follow this tutorial using either Maven or Gradle (the latter would probably be easier since you don't need to install it - the tutorial's code has a wrapper).
The tutorial uses Spring Boot which vastly simplifies Spring configuration (which is a serious benefit especially for someone who is new to Spring)
As others already told you, I personally would not start any spring based project (means: any project) without maven! You have so much benefits from it, not only depencency management.
To start a spring app outside an application context:
#Configuration
public class AppConfig {
//any bean configurations here
}
//your entry class
static void main(String args[]) {
//get a reference to the spring context. use this context throughout your app!
ApplicationContext ctx = new AnnotationConfigApplicationContext(CacheConfig.class).get();
//optain any beans from the context. inside these beans, you can use any spring feature you like, eg #Autowired
ctx.getBean(YourBean.class).executeMethod();
}
I'd recommend starting with Spring Boot which will handle all of that for you. As others have mentioned, pick a build tool (Maven or Gradle) and follow the guide we provide on building a batch application here: http://spring.io/guides/gs/batch-processing/.