Does #ComponentScan need to be on the same class as #Configuration - spring

Why do #ComponentScan and #Configuration always go hand in hand?
It seems like a class that has #Configuration always needs to be annotated with #ComponentScan.

Related

what is the difference between using #EnableWebMvc + WebMvcConfigurer and WebMvcConfigurer?

what is the difference between using #EnableWebMvc + WebMvcConfigurer and WebMvcConfigurer??
#Configuration
#EnableWebMvc
class WebMvcConfig(): WebMvcConfigurer {}
#Configuration
class WebMvcConfig(): WebMvcConfigurer {}
#Configuration
class WebMvcConfig(): WebMvcConfigurer {}
This will only take effect if #EnableWebMvc is used. The WebMvcConfigurer instancs are detected by an instance of DelegatingWebMvcConfiguration which is registered through #EnableWebMvc. So for this to work you need #EnableWebMvc.
NOTE: When using Spring Boot, this is automatically done when Spring Boot detects Spring MVC classes on the classpath!.
Without #EnableWebMvc a WebMvcConfigurer doesn't do anything but take up memory. When not using #EnableWebMvc the DispatcherServlet will install some defaults. Those defaults are hardcoded in the DispatcherServlet.properties file. Those defaults are hard to modify without the use of #EnableWebMvc.
WARNING: When using Spring Boot andadding #EnableWebMvc, will actually disable a large part of the MVC auto-configuration done by Spring Boot, which can lead to other surprises!

Does #ComponentScan override #SpringBootApplication?

My #SpringBootApplication annotation is present in com.abc.def package.
According to this article , using #SpringBootApplication annotation is equivalent to using #Configuration, #EnableAutoConfiguration, and #ComponentScan with their default attributes :-
https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html
If i supply my own #ComponentScan, does it add new packages to the default value or completely override the default ?
#SpringBootApplication annotation can component scan classes from packages under Application class belong to . Also you can add #ComponentScan to scan classes not under package Application class belongs to.
#ComponentScan("external.pkg")
#SpringBootApplication
class MyApp{
}
the annotation #SpringBootApplication contains #EnableAutoConfiguration annotation, which will scan through your jar, class, and maven dependencies in the classpath to register beans. If you declare beans cross packages and only want to use specific beans from specific packages, then use #ComponentScan
#ComponentScan gives you a fine-grained package or class level control on what beans you declared would be registered.
The #SpringBootApplication annotation doesn't allow all the filter customizations supported by #ComponentScan. We observed in our application that spring boot was automatically loading a #Configuration class from a library outside the package hierarchy of the class annotated with #SpringBootApplication.
If you want more control, one idea is to omit the #SpringBootApplication annotation and just specify the individual annotations #Configuration, #EnableAutoConfiguration, and #ComponentScan with your desired customizations.
More information is available in Spring Boot's Documentation:
18. Using the #SpringBootApplication Annotation
Good luck

How is #ConfigurationProperties-annotated classes detected automatically with #SpringBootApplication Annotation

I am learning Spring Boot and have a question with one example in the reference documentation.
Following section of the documentation mentions
6. Using the #SpringBootApplication Annotation
A single #SpringBootApplication annotation can be used to enable those
three features, that is:
#EnableAutoConfiguration: enable Spring Boot’s auto-configuration
mechanism
#ComponentScan: enable #Component scan on the package where the
application is located (see the best practices)
#Configuration: allow to register extra beans in the context or import
additional configuration classes
and the following example to replace this single annotation by any of the features that it enables is bit confusing for me . The example
package com.example.myapplication;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
#Configuration(proxyBeanMethods = false)
#EnableAutoConfiguration
#Import({ MyConfig.class, MyAnotherConfig.class })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Explanation for the example
In this example, Application is just like any other Spring Boot
application except that #Component-annotated classes and
#ConfigurationProperties-annotated classes are not detected
automatically and the user-defined beans are imported explicitly (see
#Import).
The only major difference I see in the example code above is that it does not have #ComponentScan annotation. I also read in the comments section of an SO answer (Stephane Nicoll May 5 '17 at 11:07) that #Component annotation is not recommended officially to auto detect #ConfigurationProperties. So my assumption is that Spring framework classes with #ConfigurationProperties are not annotated with #Component.
Also I checked the #SpringBootApplication annotation source and couldn't identify anything that should enable the automatic detection of #ConfigurationProperties annotated classes.
The reference document 2.8.3. Enabling #ConfigurationProperties-annotated types section shows the following way to scan and autodetect #ConfigurationProperties
#SpringBootApplication
#ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" })
public class MyApplication {
}
With all these details , I would like to understand
Why is it explicitly mentioned for this example that #ConfigurationProperties-annotated classes are not detected automatically ? and How is #ConfigurationProperties annotated classes automatically detected when #SpringBootApplication is used.
Additional note : I saw a small difference between the prior version of the documentation and the current one. The following reference is missing the current one
Keep in mind that the #EnableConfigurationProperties annotation is
also automatically applied to your project so that any existing bean
annotated with #ConfigurationProperties is configured from the
Environment
Following is what I understand from my analysis.
#ConfigurationProperties annotated types can be registered to the ApplicationContext by
Annotating the class with #ConfigurationProperties with an
annotation that falls in the scope of #ComponentScan (
#Component, #Service and the like ) . Not recommended as per the comment from Stephane Nicoll , which makes sense to me now.
Using annotation
#EnableConfigurationProperties . For this to
work the class annotated with #EnableConfigurationProperties
should be annotated with an annotation that falls in the scope of
#ComponentScan ( #Component, #Service and the like )
Using annotation #ConfigurationPropertiesScan and by making sure
the classes annotated with #ConfigurationProperties is placed
under its radar. The usage is similar to #ComponentScan .
Now , when we replace #SpringBootApplication with individual annotations that it enables and omit #ComponentScan (as in example) , the #EnableConfigurationProperties way (Point 2) of registering the types with #ConfigurationProperties will not work. This probably answers both my questions on why and how .
This was explicitly mentioned probably because the connection between #EnableConfigurationProperties and #ComponentScan is not that obvious for people like me.
Additional details.
The registration of #ConfigurationProperties annotated types when we use #EnableConfigurationProperties happens through of the EnableConfigurationPropertiesRegistrar class that is imported by the annotation
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Import(EnableConfigurationPropertiesRegistrar.class)
public #interface EnableConfigurationProperties {..}

What is SpringBootApplication functionality?

I understand that #SpringBootApplication is used to mark the main class of a spring application.
Here I see that it "is equivalent to using #Configuration, #EnableAutoConfiguration and #ComponentScan". But why is there needed a #Configuration if it is already made automatically with #EnableAutoConfiguration.
#EnableAutoConfiguration means Spring boot will look what libraries you use and try to set up beans accordingly. It has nothing to do with #Configuration.

#ContextConfiguration how to use XML based config and Java-based at same time?

I'm writing integration tests with SpringJUnit4. I got question. How in #ContextConfiguration I can use XML based config and Java-based at same time. As I know I couldn't do it, but maybe there exist backdoor?
Thanks in advance!
You could create static inner #Configuration class in your test class and use #ContextConfiguration annotation on your class without any parameters. As stated in the article below, Spring will automatically look for static inner #Configuration class if no XML locations or config classes are passed to the annotation.
You can then import your XML config and Java config classes using #Import and #ImportResource annotations. So your base class for your Spring tests could look something like this:
#ContextConfiguration
#RunWith(SpringJUnit4ClassRunner.class)
public class BaseSpringTest {
#Configuration
#Import(BaseConfig.class)
#ImportResource({ "classpath:applicationContext-hibernate.xml" })
public static class ContextConfig {}
}
Sources
Testing with #Configuration Classes and Profiles
Import annotation JavaDoc
ImportResource annotation JavaDoc
Use #ImportResource on #Configuration class to import XML based config.

Resources