Basic interrogation about Spring boot and #EnableAutoConfiguration - spring-boot

I have a basic question about Spring Boot:
Say I am developping a websocket app. It seems the idea behind Spring Boot is as follows:
As a developer I am responsible for:
Including the following mvn dependency: spring-boot-starter-websocket
Annotate my configuration class with: #EnableAutoConfiguration
Spring Boot is then responsible for applying the following config: WebSocketAutoConfiguration
In a nutshell, is it how it works? Can someone please confirm of infirm the above?

You are absolutely correct.
After adding spring-boot-starter-websocket to your configuration file and using the #EnableAutoConfiguration annotation, Spring will use you class path to automatically determine which configuration settings and beans need to be created for you.
Spring Boot will handle the WebSocketAutoConfiguration and any other necessary common configurations.
More information can be found here: https://spring.io/guides/gs/spring-boot/

Related

What's the difference between Spring Boot's #Configuration and #AutoConfiguration?

And most important, what's the reason behind the "Auto" prefix? Classes annotated with #Configuration rather than #AutoConfiguration are less automatic or something?
#Configuration is a spring framework annotation and not strictly bound to spring-boot. It was introduced when spring started to allow programmatic creation of spring-beans as to move forward from xml definitions of beans.
#AutoConfiguration is a spring-boot specific annotation not commonly available in spring framework. The reason it exists, is for external providers that cooperate with spring-boot to be able to mark some classes in some libraries they provide with this annotation as to inform spring-boot that those classes could be parsed and make some initializations during start up of spring application automatically.
So if some regular programmer that develops some application happens to have kafka in dependencies then some beans will automatically be created and added in application context and will be ready for the programmer to use, although he has not defined any configuration for them. Spring-boot already knows this as the kafka provider has already informed by marking some class in the jar they provide with the annotation #AutoConfiguration.
For this reason #AutoConfiguration has some more powerful configurations available as before, after, beforeName, afterName as to allow the provider to specify when the configuration is applied during application startup if some order is necessary.
So this annotation is not to be used from some regular programmer that develops an application using spring-boot. It is for someone that develops a library that other users might use with spring-boot. One such example is kafka library.
For this to work in a spring-boot project #EnableAutoConfiguration is needed as well, to enable auto configuration.
From spring documentation
Spring Boot auto-configuration attempts to automatically configure
your Spring application based on the jar dependencies that you have
added. For example, if HSQLDB is on your classpath, and you have not
manually configured any database connection beans, then Spring Boot
auto-configures an in-memory database.
#Configuration instead is to be used from some regular programmer that develops an application using spring-boot or spring-framework as to inform the framework for which beans should be created and how.
#AutoConfiguration was introduced in 2.7 with the idea to mark all auto-configurations with its dedicated annotation and move away from spring.factories for auto-configuration imports in 3.0 as described in Github issue.
According to Spring documentation:
[#AutoConfiguration] indicates that a class provides configuration that can be
automatically applied by Spring Boot. Auto-configuration classes are
regular #Configuration with the exception that
Configuration#proxyBeanMethods() proxyBeanMethods is always false.
Usually, #AutoConfiguration classes automatically configure an application based on the dependencies that are present on the classpath. Those classes are generally marked as #ConditionalOnClass and #ConditionalOnMissingBean annotations that detect the presence or absence of specific classes.
Additionally, if a configuration needs to be applied in a specific order, you can use the before, beforeName, after, and afterName attributes on the #AutoConfiguration, unlike #Configuration which doesn't provide those attributes.

Skip environment post processor for spring starter

I am currently using spring boot with some 3rd parties starter.
I want to disable those AutoConfiguration and the EnvironmentPostProcessor in the starter during testing
I can disable AutoConfiguration by using #EnableAutoConfiguration(exclude ={xxx},
but cannot find any method to disable the EnvironmentPostProcessor. This blocks me from doing any test. Anyone can help?

Spring Integration XML Configuration and Spring Boot Java Annotation Configuration

Is it possible to have spring integration xml configuration and spring boot java annotation configuration at the same time? Can you please refer with an example on this?
What is the best practice for this?
Thanks
As Marten pointed out it is fully fine to combine Spring Boot with an XML configuration. See Spring Boot docs: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.configuration-classes.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 #ImportResource annotation to load XML configuration files.
And here is the sample: https://github.com/spring-projects/spring-boot/blob/main/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-xml/src/test/java/smoketest/xml/SampleSpringXmlPlaceholderBeanDefinitionTests.java

Spring Boot 2.0.2 - Missing classes MetricRepositoryAutoConfiguration, MetricFilterAutoConfiguration

in Spring Boot 1.5 application the application class was annotated with:
#EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class})
These classes were I believe in the package:
import org.springframework.boot.actuate.autoconfigure.*;
Now upgrade to Spring Boot 2.0.2 but those classes are missing, can't find them anywhere.
What changed? How to solve?
Thanks!
Spring Boot’s own metrics have been replaced with support, including
auto-configuration, for Micrometer and dimensional metrics.
if you want to disable metrics set management.endpoint.metrics.enabled=false
there is a complete guide that makes migration a lot easier
Spring Boot 2.0 Migration Guide

Spring Security 3.1.3 #EnableWebSecurity

I'm having trouble finding which package contains #EnableWebSecurity in Spring Security 3.1.3. I've added core, config, web and ldap security packages, but the annotation remains unavailable. Has it been replaced by another annotation?
I think java config support is not yet released with Spring Security, but available as a separate module in spring-security-javaconfig. See instructions in the linked page about how to include the jar into your maven build.
I faced the same problem and was able to solve it by adding "spring-boot-starter-security" as a dependency in build.gradle as mentioned in the spring "Getting Started" guide available #
https://spring.io/guides/gs/securing-web/

Resources