spring boot baeldung notes - technoangel/java GitHub Wiki

Based on work by Baeldung

Starters are used in Spring Boot to add new services and functionality to an existing Spring Boot application.

AutoConfiguration Classes

Spring Boot looks for a spring.factories file in the classpath under META-INF. It follows this comma-delimited format:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
<your namespaced autoconfiguration>

Spring Boot will try to run each of these configuration classes when it bootstraps. The success of the run depends on the presence of the dependent classes on the classpath. The configuration code per-module looks like this:

@Configuration
@ConditionalOnClass(MongoClient.class)
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
public class MongoAutoConfiguration {
    // configuration code
}

The @ConditionalOnClass checks if the MongoClient class is available on the classpath and will only execute if that is true.

Converting an Application into a Starter

The two stages for converting an app into a starter library include:

  1. Make an auto-configure class along with a properties class for custom configuration.
  2. Make a starter pom.xml to bring the dependencies of the library and the auto-configure project.

This is the sample library they will convert into an auto-starting library. Notice it has:

  • a pom.xml file,
  • a src/main/java/com/baeldung/greeter directory,
  • a com.baeldung.greeter namespace,
  • no resources,
  • The base functionality is a Greeter model that pulls in custom greeting messages and returns one based on the time that is requested (defaulting to the current moment).
  • The GreetingConfig model extends java.util.Properties and can be serialized
  • The GreetingConfigParams model is the wrapper for an enumeration identifying the different messages.

At the time I'm writing this, I have no idea how the GreetingConfig model is pulling in data from the GreetConfigProperties file

Autoconfiguration

The autoconfiguration module will be called greeter-spring-boot-autoconfigure. Notice this is the name of the application and maybe the pom.xml artifactId and not a reflection of the namespace or the pom.xml groupId

This greeter-spring-boot-autoconfigure module will have 2 classes:

  • A GreeterProperties class which will allow the setting of properties,
  • GreeterAutoConfiguration which will create the beans for the Greeter library.
@ConfigurationProperties(prefix = "baeldung.greeter")
public class GreeterProperties {
 
    private String userName;
    private String morningMessage;
    private String afternoonMessage;
    private String eveningMessage;
    private String nightMessage;
 
    // standard getters and setters
 
}

This is the GreeterProperties file

@Configuration
@ConditionalOnClass(Greeter.class)
@EnableConfigurationProperties(GreeterProperties.class)
public class GreeterAutoConfiguration {
 
    @Autowired
    private GreeterProperties greeterProperties;
 
    @Bean
    @ConditionalOnMissingBean
    public GreetingConfig greeterConfig() {
 
        String userName = greeterProperties.getUserName() == null
          ? System.getProperty("user.name") 
          : greeterProperties.getUserName();
         
        // ..
 
        GreetingConfig greetingConfig = new GreetingConfig();
        greetingConfig.put(USER_NAME, userName);
        // ...
        return greetingConfig;
    }
 
    @Bean
    @ConditionalOnMissingBean
    public Greeter greeter(GreetingConfig greetingConfig) {
        return new Greeter(greetingConfig);
    }
}

This is the GreeterAutoConfiguration file

When the application starts up, the GreeterAutoConfiguration class will run if it can find Greeter on the classpath. When it runs, it will populate Spring with the GreeterConfig and Greeter beans by reading the GreeterProperties class.

Setting up pom.xml

The application needs a pom.xml to describe the dependencies and to set up the application. The naming convention is -spring-boot-starter, so this will be called greeter-spring-boot-starter.

<project ...>
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.baeldung</groupId>
    <artifactId>greeter-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <greeter.version>0.0.1-SNAPSHOT</greeter.version>
        <spring-boot.version>1.5.2.RELEASE</spring-boot.version>
    </properties>
 
    <dependencies>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
 
        <dependency>
            <groupId>com.baeldung</groupId>
            <artifactId>greeter-spring-boot-autoconfigure</artifactId>
            <version>${project.version}</version>
        </dependency>
 
        <dependency>
            <groupId>com.baeldung</groupId>
            <artifactId>greeter</artifactId>
            <version>${greeter.version}</version>
        </dependency>
 
    </dependencies>
 
</project>

Using the Starter

Now we will create the app that will use our Spring starter. We will name this app greeter-spring-boot-sample-app. In that app's pom.xml we will add the greeter-spring-boot-starter dependency

<dependency>
    <groupId>com.baeldung</groupId>
    <artifactId>greeter-spring-boot-starter</artifactId>
    <version>${greeter-starter.version}</version>
</dependency>

Spring Boot will auto configure everything and Greeter will be ready for injection.

At this point, properties can be configured for the current project.

Finally, we can use the Greeter bean in the application:

@SpringBootApplication
public class GreeterSampleApplication implements CommandLineRunner {
 
    @Autowired
    private Greeter greeter;
 
    public static void main(String[] args) {
        SpringApplication.run(GreeterSampleApplication.class, args);
    }
 
    @Override
    public void run(String... args) throws Exception {
        String message = greeter.greet();
        System.out.println(message);
    }
}
⚠️ **GitHub.com Fallback** ⚠️