JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

@EnableAutoConfiguration Annotation in Spring Boot

March 17, 2016 by Krishna Srinivasan Leave a Comment

@EnableAutoConfiguration annotation auto-configures the beans that are present in the classpath. This simplifies the developers work by guessing the required beans from the classpath and configure it to run the application. This annotation is part of the spring boot project.

For example, if you have tomcat-embedded.jar in the classpath, then you will need a TomcatEmbeddedServletContainerFactory bean to configure the tomcat server. This will be searched and configured without any manual XML configurations.

Spring Boot Enable Auto Configuration
Spring Boot Enable Auto Configuration

With the spring boot 1.2.0 release, the need for this annotation has been reduced because there is an alternative annotation @SpringBootApplication which combines the three annotations @Configuration,@EnableAutoConfiguration and code>@ComponentScan.

The package of the class that is annotated with @EnableAutoConfiguration has specific significance and is often used as a ‘default’. For example, it will be used when scanning for @Entity classes. It is generally recommended that you place @EnableAutoConfiguration in a root package so that all sub-packages and classes can be searched.

Auto-configuration classes are normal @Configuration annotated classes only. These are mentioned in the spring.factories file. Spring checks the spring.factories files under the folder META-INF in your project or JAR file to auto-configure the configuration classes.

@EnableAutoConfiguration Parameters

The following are the parameters that can be passed inside this annotation:

  • exclude – Exclude the list of classes from the auto configuration.
  • excludeNames – Exclude the list of fully qualified class names from the auto configuration. This parameter added since spring boot 1.3.0.

The above parameters helps you to exclude the list of configuration classes that are not required to be auto-configured.

Here is the sample snippet for how to use the parameters:

[code lang=”java”]
@EnableAutoConfiguration(exclude={Book.class})
[/code]

Write Custom Auto-Configuration

I have written a very simple auto-configuration module for spring boot application. Note that you can write auto-configuration as part of the current application or you can import the JAR file. The important point is to add the spring.factories file under the META-INF folder. This is the default behavior of spring application to search for that file.

Here is the steps to write your own auto-configuration class:

1.  Create a @Configuration class as one below:

[code lang=”java”]
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass({ String.class })
public class ConfigureDefaults {
Logger logger = LoggerFactory.getLogger(ConfigureDefaults.class);
@Bean
public String cacheManager() {
logger.info("Configure Defaults");
return new String("test");
}
}
[/code]

2. Create spring.factories file as below and put it under the META-INF folder:

[code]
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
net.javabeat.spring.data.autoconfigure.ConfigureDefaults
[/code]

3. Run your spring boot application.

You can notice that ConfigureDefaults will be invoked and the beans defined in that classes will be configured for the application use. Look at the below project structure I have used for testing my spring boot application with the custom auto-configuration module.

Spring Boot Auto Configuration
Spring Boot Auto Configuration

Filed Under: Spring Framework Tagged With: Spring Boot Annotations

@SpringBootApplication Annotation in Spring Boot

March 16, 2016 by Krishna Srinivasan Leave a Comment

@SpringBootApplication Annotation
@SpringBootApplication Annotation

@SpringBootApplication annotation is a convenience annotation introduced from Spring Boot 1.2.0. If you have worked on the earlier spring boot versions, it is common that main class always annotate with the following annotations:

  • @Configuration : This annotation is not specific to the spring boot applications. This annotation tags the class as the source for bean definitions. In short, this annotation is used for defining beans using the Java configuration.
  • @EnableAutoConfiguration : This is a spring boot annotation. This annotation enables the application to add the beans using the classpath definitions.
  • @ComponentScan : This annotation tells the spring to look for other components, configurations and services in the specified path.

The above three annotations have become quite common for all the spring boot application main class. This makes it worthwhile to add @SpringBootApplication as a single annotation that represents all the above three annotations. Here is the definition of this annotation looks in the source code:

[code lang=”java”]
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
……
[/code]

By looking at the code, you can easily understand that using the above three annotations are equivalent of using a @SpringBootApplication annotation.

However, if you want to customize the @EnableAutoConfiguration or @ComponentScan, then you can use the individual annotations as below.

[code lang=”java”]
@EnableAutoConfiguration(exclude={Book.class})
@ComponentScan({"net.javabeat"})
@Configuration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
[/code]

The same can be used in the @SpringBootApplication as below:

[code lang=”java”]
@SpringBootApplication(exclude=Book.class,scanBasePackages={"net.javabeat"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
[/code]

The following are the parameters accepted in the @SpringBootApplication annotation:

  • exclude – Exclude the list of classes from the auto configuration.
  • excludeNames – Exclude the list of fully qualified class names from the auto configuration. This parameter added since spring boot 1.3.0.
  • scanBasePackageClasses – Provide the list of classes that has to be applied for the @ComponentScan.
  • scanBasePackages – Provide the list of packages that has to be applied for the @ComponentScan. This parameter added since spring boot 1.3.0.

Filed Under: Spring Framework Tagged With: Spring Boot Annotations

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved