(004) Spring Boot of SpringApplication.run Why can run without annotated class, and contrast

  Generally speaking class above springBoot initial run will add SpringBootApplication comments, but we found without annotation can be run successfully, the following example:

  pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.edu.spring</groupId>
    <artifactId>springboot</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>springboot</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.6.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>
View Code

  App.java

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

public class App 
{
    @Bean
    Runnable createRunnable(){
        return () -> {System.out.println("spring boot is run");};
    }
    
    public static void main( String[] args )
    {
        
        ConfigurableApplicationContext context=SpringApplication.run(App.class, args);
        context.getBean(Runnable.class).run();
        
    }
}
View Code

   Results are as follows:

   Can be seen, springboot normal operation, and the bean can be created, because the run method will ultimately call the constructor of SpringApplication SpringApplication class, which pass in a parameter (a pass type) will be used as a source, the spring container management, part of the source code as follows:

   So why do we still usually add SpringBootApplication comment it? This is because the notes contain ComponentScan notes, and other annotations, ComponentScan annotation can be configured to scan path spring. So App.java can not run properly annotated, and create bean, but does not scan to other bean, tests are as follows:

  Add User.java

package com.edu.spring.springboot;

import org.springframework.stereotype.Component;

@Component
public class User {

}
View Code

  App.java

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

public class App 
{
    @Bean
    Runnable createRunnable(){
        return () -> {System.out.println("spring boot is run");};
    }
    
    public static void main( String[] args )
    {
        
        ConfigurableApplicationContext context=SpringApplication.run(App.class, args);
        context.getBean(Runnable.class).run();
        System.out.println(context.getBean(User.class));
        
    }
}
View Code

  Results are as follows:

   ComponentScan add annotations App.java above, may be scanned to User Class

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
public class App 
{
    @Bean
    Runnable createRunnable(){
        return () -> {System.out.println("spring boot is run");};
    }
    
    public static void main( String[] args )
    {
        
        ConfigurableApplicationContext context=SpringApplication.run(App.class, args);
        context.getBean(Runnable.class).run();
        System.out.println(context.getBean(User.class));
        
    }
}
View Code

  The results will not run error, as follows:

   SpringApplication.run (App.class, args); App parameter as a source, is initialized spring containers, this parameter is not necessarily an operating method where the class itself may be another type, for example:

  App.java

package com.edu.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

public class App 
{
    
    public static void main( String[] args )
    {
        
        ConfigurableApplicationContext context=SpringApplication.run(App2.class, args);
        context.getBean(Runnable.class).run();
        System.out.println(context.getBean(User.class));
        
    }
}
View Code

  App2.java

package com.edu.spring.springboot;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
public class App2 
{
    @Bean
    Runnable createRunnable(){
        return () -> {System.out.println("spring boot is run");};
    }

}
View Code

  User can also get Runnable and the bean results are as follows:

   If at this time the method will create Runnable written in the App.java will not get the bean, program error. As it has been specified source is App2.java. ComponentScan with a scan package, or sub-packet, the source so to place the outermost package. SpringBootApplication source can add annotations or ComponentScan.

Guess you like

Origin www.cnblogs.com/javasl/p/11827829.html