Issue
How Spring Boot auto-configuration classes are activated based on the maven starter projects that are included in project pom.xml
as dependencies?
for example, by adding spring-boot-starter-security
to pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
class SecurityAutoConfiguration
is activated. how does this happen? How SecurityAutoConfiguration
is related to spring-boot-starter-security
?
the starter project themselves are completely empty, there is no java code in these starter projects, and just a couple of dependencies is defined in pom.xml
. I want to know exactly what happens when I add a starter project to my Spring Boot project and exactly what configurations are applied.
Solution
Autoconfiguration is one, or maybe the main one, feature of SpringBoot Basically, depending on certain conditions, some configurations or/and beans are made available. In your case here is what happens :
- spring-boot-starter-security, as other starters, is just here to make some others dependencies available for your project, particulary spring-security-config which itself, has a dependency on spring-security-core
- If your looking at the code source of SecurityAutoConfiguration you can see that is configuration class depends on the availabality of the class DefaultAuthenticationEventPublisher, which is true as now you have a (transitive) dependency on spring-security-core.
- This SecurityAutoConfiguration class is listed as a Autoconfiguration class inside the config file org.springframework.boot.autoconfigure.AutoConfiguration.imports (spring.factories for some previous Spring Boot version) of the spring-boot-autoconfigure project, which make it active for your application.
Here you can find the documentation : https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.developing-auto-configuration.understanding-auto-configured-beans
Answered By - cerdoc
Answer Checked By - Senaida (JavaFixing Volunteer)