Is #Import annotation necessary with Spring Boot? - spring

I'm working with Spring Boot 2.1.5. I have multiple classes annotated with #Configuration.
I want to know if it's necessary to list all of these configuration classes with #Import? Or will the component scanner find all the #Configuration annotated classes and do it automatically?

You don’t need to do that, scanning will be done for you automatically , all you need is to make your main class on the root package, and all other stereotypes classes including configuration in sub packages

It's not necessary to use #Import annotation. As the #Configuration is a Meta-annotation for #Component , the Component scanner will pick it . But Spring docs suggest to use #Import annotation when you have many #Configuration classes. This is just for better management.

Related

Prevent AutoConfiguration of all included Spring Cloud dependencies

Excluding several AutoConfiguration classes in Spring Boot is easy. Just exclude it from the application:
#SpringBootApplication(exclude = {
org.springframework.cloud.autoconfigure.RefreshAutoConfiguration.class,
org.springframework.cloud.openfeign.FeignAutoConfiguration.class,
})
But how can all AutoConfiguration from the package org.springframework.cloud can be excluded without adding all Spring Clouds AutoConfiguration classes to the exclude? That would be dozens if not hundred entries. And it would not be future proof as with every new Spring Cloud version new AutoConfiguration classes come along.
Using #ComponentScan would only work if #SpringBootApplication respective #EnableAutoConfiguration is not used. But that would remove all the convenient auto configuration capabilities, too.
Is there a way to achieve that programmatically? And how?
Removing Spring Clouds dependencies from the application is unfortunately not a feasible solution.
Why you want to exclude all classes from auto configuration ?
Most of the beans in Autoconfigurations are #conditional , they will not be created if you are providing your own.
Anyway you can look into AutoConfigurationImportSelector . As of now Spring boot allow only class and name for #EnableAutoConfigurtion exclude . if you want to write customize "AutoConfigurationImportSelector" , you can subclass and filter exclude by package after writing your own extended interface for EnableAutoConfiguration to write package method.
As I mentioned this will be the last thing I would like to do . just check #conditional dependency for auto configuration and provide your own beans is better solution.

Enable Actuator health endpoint without enabling auto config

In my project, I don't want to use #EnableAutoConfiguration. My Application.java has #ComponentScan, #Configuration and #Import annotation.
I have added spring boot actuator dependency in my pom.xml. But, when I try to access http://<>/acutuator/health, I get 404. I believe I need to specify some config class as part of Import annotation. I would need help in figuring out what that config would be.
#EnableAutoConfiguration makes Spring guess configuration based on the classpath, and that's what spring boot all about. If you find that specific auto-configuration classes that you do not want are being applied, you can use the exclude attribute of #EnableAutoConfiguration to disable them. For example:
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

Spring boot auto configuration with dependency and without #ComponentScan

Spring boot provides #ComponentScan to find packages to be scanned.
I am building a library which has #RestControllers inside with package com.mylib.controller. There are other classes as well with stereotype annotations in different packages.
So, if some one is developing SpringBoot Application with com.myapp base package.
He uses my library in his application. He need to mention #ComponentScan("com.mylib") to discover stereotype components of library.
Is there any way to scan components without including library package in #ComponentScan?
As spring-boot-starter-actuator expose its endpoints just with dependency, without defining #ComponentScan. OR any default package which is scanned regardless of application base package.
You could create a Spring Boot Starter in the same style as the Spring Provided Starters. They are essentially a jar'd library with a a spring.factories file pointing to the #Configuration class to load with some other annotations on there to provide overriding/bean back off (#ConditionalOnMissingBean) and generally provide their own #ConfigurationProperties.
Stéphane Nicoll provided an excellent demo of how to build one.
https://github.com/snicoll-demos/hello-service-auto-configuration
It is also documented in the Spring Boot documentation. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html
The library approach would also work but I see no benefit in not making it a starter. Additionally for any library/starter I'd recommend dropping the #ComponentScan and just defining the beans in a #Configuration. This will work for sterotypes like #RestController etc. will function as normal if you create an #Bean out of it in a configuration.
Spring boot starter are special artifacts designed by Spring and used by Spring.
You can check that in the source code that contains mainly a
spring.provides file :
provides: spring-boot-actuator,micrometer-core
I don't know the exact way to process in the same way as Spring Boot Starter but as probably acceptable workaround, you could define in your jar a #Configuration class that specifies #ComponentScan("com.mylib").
#Configuration
#ComponentScan("com.mylib")
public class MyLibConfig {
//...
}
In this way, clients of the jar need "only" to import the #Configuration class :
#Import(MyLibConfig.class)
#Configuration
public class ClientConfig{
//...
}

why do we need componentScan in spring

This question might be trivial but still i'm unable to find a good reason or best practice towards #ComponentScan in Spring
DI works just by self annotating the class then why do we need #ComponentScan
What is the best practice towards this?
#ComponentScan tells Spring in which packages you have annotated classes which should be managed by Spring.
Spring needs to know which packages contain spring beans, otherwise you would have to register each bean individually in(xml file). This is the use of #ComponentScan.
Take a simple example, you have a class and annotated with #Controller in a package package com.abc.xyz; , you have to tell the spring to scan this package for Controller class, if spring didn't scan this package, then spring will not identifies it as a controller class.
Suppose if your dealing with configuration file,
<context:component-scan base-package="com.abc.xyz">
like this,
When spring loads the xml file, this tag will search the all the classes present in the package com.abc.xyz, so any of the class containing #controller, #Repository #Service etc.., if it found then spring will register these annotated class in the bean factory.
Suppose if your using spring boot application,
Then your spring-boot application is annotated with The#SpringBootApplication.
#SpringBootApplication annotation is equivalent to using #Configuration, #EnableAutoConfiguration and #ComponentScan with their default attributes.
One more point if you didn;t specify the base package name in #ComponentScan,
it will scan from the package, where the #Springbootapplication present

Spring Boot : Disable AutoConfiguration of all modules

In Spring Boot, is there a way to prevent Auto Configuration of all modules? Basically am looking for something like #DisableAutoConfiguration instead of excluding specific configurations with class names.
Auto-configuration is enabled by the #EnableAutoConfiguration annotation. If you don't want to use auto-configuration, then omit this annotation. Note that #SpringBootApplication is itself annotated with #EnableAutoConfiguration so you'll have to avoid using it too. Typically, this would leave your main application class annotated with #ComponentScan and #Configuration.

Resources