Load beans/controller from external jar in spring boot without changing config class - spring-boot

I want to load a bean from a external jar into spring boot project. But I don't want to make changes to existing spring boot config file. I want same behavior like actuator. Just add dependency and bean or endpoint will be available in spring boot

Related

where does spring load the auto configuration files from

I have read some tutorial on old version of spring boot.
https://www.logicbig.com/tutorials/spring-framework/spring-boot/auto-config-mechanism.html
The boot configuration classes are loaded from
spring-boot-autoconfigure-1.4.2.RELEASE.jar!/META-INF/spring.factories
(in the file, search for the key
org.springframework.boot.autoconfigure.EnableAutoConfiguration)
https://github.com/spring-projects/spring-boot/blob/v1.4.4.RELEASE/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories It has "org.springframework.boot.autoconfigure.EnableAutoConfiguration"
My understanding is that spring loads/runs all these files defined by EnableAutoConfiguration in spring.factories. If the #Conditional* matches, the bean will be created.
But in the last 2.x version, there is no such property EnableAutoConfiguration
https://github.com/spring-projects/spring-boot/blob/2.7.x/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
where does spring load the auto configuration files from?
Updated:
I found them here:
https://github.com/spring-projects/spring-boot/blob/v2.7.4/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
Spring Boot 2.7 introduced a new ‘META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports’ file for registering auto-configurations, while maintaining backwards compatibility with registration in ‘spring.factories’. With Spring Boot 3.0 release, support for registering auto-configurations in ‘spring.factories’ has been removed in favor of the imports file.
we are using #SpringBootApplication annotations on spring boot project main class in which they have all others annotations.

Spring Boot - configure properties of a jar included as dependency into another jar

I have a Spring Boot web app A and its dependent on a Spring Boot jar library B. I have some properties that I want to configure within B and don't want the client apps (e.g. the web app A) to configure them. I have these properties files in B.
application-dev.properties
application-stage.properties
applicatino-live.properties
The issue is that these properties are not recognized when the library is added as a dependency in web app A. What is the way to achieve this?
The PropertySource annotation can be used in a Configuration class on webapp A to achieve what you're looking for:
#Configuration
#PropertySource("classpath:application-dev.properties"),
#PropertySource("classpath:application-stage.properties"),
#PropertySource("classpath:application-live.properties")
public class WebappAConfiguration {
}
I also found the order that spring boot looks for externalized configuration helpful here.

How Spring Boot autoconfigures the JCache when there is no JCache configuration file entry in spring.factories file?

I am new to spring boot project.
Currently I am working on a project with spring boot, Jcache with ehcache implementation.
I am trying to understand how spring boot autoconfigures the Cache Framework. I did my own research and identified spring boot #EnableAutoConfiguration reads the spring.factories file and autoconfigures the Cache related beans based on the classes available in classpath.
Here is the Spring Boot Cache auto configure Java based Configuration file available under spring.factories file
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
but for Jcache “ JCacheCacheConfiguration.java” is the Spring Boot auto configuration file , But this file is not available under spring.factroies file in autoconfigurer.jar file.
Then how spring boot auto configures the Jcache without entry in spring.factories file?
JCache implementations are providing a service (in META-INF). So Spring can found the implementation magically. The simpler is #EnableCaching which will find the provider and give you caching right away.
Then, you will want to provide a specific caching configuration. The easiest is by specifying spring.cache.jcache.config=ehcache.xml in your application.properties.
That's it. You will find a more complicated and Java (no xml) configuration in ehcache sample and pet clinic.

Possible to use Spring Boot 1.3.0.M1 with Spring Cloud?

I'd like to be able to start using Spring Boot 1.3.0.M1 (and by extension Spring 4.2.0.RC1) along with Spring Cloud (config server, eureka, ribbon, feign, and zuul).
I'm using a gradle build, so initially I was using the dependencyManagement plugin like this:
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-starter-parent:1.0.3.BUILD-SNAPSHOT"
}
}
But that means that no matter what version of spring boot I try to include, it gets overridden.
Instead I tried to manually include all the dependencies that the cloud starters include in the build and separately include spring boot 1.3.0.M1, but on server startup that leads to:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'servoMetricCollector' defined in class path resource [org/springframework/cloud/netflix/servo/ServoMetricsAutoConfiguration.class]
Any suggestions would be appreciated.
There is an open issue to support Spring Boot 1.3.0.

Error while including bean from org.springframewrok.orm jar file in spring

I am trying to make a crud application using Spring MVC and JPA with hibernate as a service provider.I already have a working CRUD application using spring MVC and jdbc.So I decided to make changes in already built application only
But the problem is when i include any class from org.springframewrok.orm jar file in beans.xml of spring it gives me HTTP 404- resource not found error. What could be the possible reasons??
I am using org.springframewrok.orm 3.1.1 release file.

Resources