一、注解
@SpringBootApplication
点开查看源码是由多个注解合成的注解,其中主要的注解有:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
三个关键的注解:
@ComponentScan 自动扫描加载进来的包,-----------可以扫描自动加载的bean
@EnableAutoConfiguration 启动自动配置
@SpringBootConfiguration 继承了@Configuration,所以可以使用@Configuration内容
@Configuration是spring提供的注解,@SpringBootConfiguration是springboot提供的注解。效果几乎一样,用哪个看自己的习惯。
二、pom配置
<?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.springboot</groupId>
<artifactId>quick_start</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>quick_start</name>
<url>http://maven.apache.org</url>
<description>Demo project for Spring Boot</description> <!--<parent>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-parent</artifactId>-->
<!--<version>2.0.2.RELEASE</version>-->
<!--<relativePath/> <!– lookup parent from repository –>-->
<!--</parent>--> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--不使用parent方式进行依赖,需要scope和type设置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.2.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
三、Application调用的三种方法
方法一:静态方法调用
package com.springboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; /******************************
* @Author : liuyang
* @ClassName : QuickStartApplication
* @Date : 2018 五月 20
* @Time : 01:05:59
* @Type : SpringBoot
* @Version : 1.0
* @Return :
* @Description :
*******************************/ @ComponentScan
@EnableAutoConfiguration
public class QuickStartApplication { @Bean
public Runnable createRunnable() { return () -> {
System.out.println("Spring Boot is Run");
}; } public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(QuickStartApplication.class,args);
applicationContext.getBean(Runnable.class).run();
System.out.println(applicationContext.getBean(User.class));
}
}
方法二:非态方法调用,可以拥有多个资源入口
package com.springboot; import com.srpingboot.nostatic.ApplicationDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext; import java.util.HashSet;
import java.util.Set; /**
* Created by
*
* @author: liuya
* @Date: 2018/5/20 2:18
* @Description: quick_start
* <p>
* 指定多个源,调用非springApplication方法
*/ public class ManyApplication { public static void main(String[] args) { // new实例对象调用
SpringApplication application = new SpringApplication(); // 可以指定多个源的方式
Set<String> sets = new HashSet<>();
sets.add(ApplicationDemo.class.getName());
sets.add(User.class.getName());
application.setSources(sets); ConfigurableApplicationContext applicationContext = application.run(args);
applicationContext.getBean(Runnable.class).run();
System.out.println(applicationContext.getBean(User.class));
} }
建立ApplicationDemo
package com.srpingboot.nostatic; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; /**
* Created by
*
* @author: liuya
* @Date: 2018/5/20 2:23
* @Description: quick_start
*/
@ComponentScan
@EnableAutoConfiguration
public class ApplicationDemo { @Bean
public Runnable createRunnable() { return () -> {
System.out.println("Spring Boot is Run");
}; }
}
方法三 :构造函数指定方式
package com.springboot; import com.srpingboot.nostatic.ApplicationDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext; import java.util.HashSet;
import java.util.Set; /**
* Created by
*
* @author: liuya
* @Date: 2018/5/20 2:18
* @Description: quick_start
* <p>
* 指定多个源,调用非springApplication方法
*/ public class ManyApplication { public static void main(String[] args) { //构造函数指定方式
SpringApplication application = new SpringApplication(ApplicationDemo.class,User.class); ConfigurableApplicationContext applicationContext = application.run(args);
applicationContext.getBean(Runnable.class).run();
System.out.println(applicationContext.getBean(User.class));
} }
源码地址:
https://github.com/liushaoye/quick_start