一、注解
@SpringBootApplication
点开查看源码是由多个注解合成的注解,其中主要的注解有:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
三个关键的注解:
@ComponentScan 自动扫描加载进来的包,-----------可以扫描自动加载的bean
@EnableAutoConfiguration 启动自动配置
@SpringBootConfiguration 继承了@Configuration,所以可以使用@Configuration内容
@Configuration是spring提供的注解,@SpringBootConfiguration是springboot提供的注解。效果几乎一样,用哪个看自己的习惯。
二、pom配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.springboot</groupId> 7 <artifactId>quick_start</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>quick_start</name> 12 <url>http://maven.apache.org</url> 13 <description>Demo project for Spring Boot</description> 14 15 <!--<parent>--> 16 <!--<groupId>org.springframework.boot</groupId>--> 17 <!--<artifactId>spring-boot-starter-parent</artifactId>--> 18 <!--<version>2.0.2.RELEASE</version>--> 19 <!--<relativePath/> <!– lookup parent from repository –>--> 20 <!--</parent>--> 21 22 <properties> 23 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 24 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 25 <maven.compiler.source>1.8</maven.compiler.source> 26 <maven.compiler.target>1.8</maven.compiler.target> 27 <java.version>1.8</java.version> 28 </properties> 29 30 <dependencies> 31 <!--不使用parent方式进行依赖,需要scope和type设置--> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-dependencies</artifactId> 35 <version>2.0.2.RELEASE</version> 36 <scope>import</scope> 37 <type>pom</type> 38 </dependency> 39 40 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-web</artifactId> 44 </dependency> 45 46 <dependency> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-starter-test</artifactId> 49 <scope>test</scope> 50 </dependency> 51 </dependencies> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 63 </project>
三、Application调用的三种方法
方法一:静态方法调用
1 package com.springboot; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.context.ConfigurableApplicationContext; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.ComponentScan; 9 10 /****************************** 11 * @Author : liuyang 12 * @ClassName : QuickStartApplication 13 * @Date : 2018 五月 20 14 * @Time : 01:05:59 15 * @Type : SpringBoot 16 * @Version : 1.0 17 * @Return : 18 * @Description : 19 *******************************/ 20 21 @ComponentScan 22 @EnableAutoConfiguration 23 public class QuickStartApplication { 24 25 @Bean 26 public Runnable createRunnable() { 27 28 return () -> { 29 System.out.println("Spring Boot is Run"); 30 }; 31 32 } 33 34 public static void main(String[] args) { 35 ConfigurableApplicationContext applicationContext = SpringApplication.run(QuickStartApplication.class,args); 36 applicationContext.getBean(Runnable.class).run(); 37 System.out.println(applicationContext.getBean(User.class)); 38 } 39 }
方法二:非态方法调用,可以拥有多个资源入口
1 package com.springboot; 2 3 import com.srpingboot.nostatic.ApplicationDemo; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.context.ConfigurableApplicationContext; 6 7 import java.util.HashSet; 8 import java.util.Set; 9 10 /** 11 * Created by 12 * 13 * @author: liuya 14 * @Date: 2018/5/20 2:18 15 * @Description: quick_start 16 * <p> 17 * 指定多个源,调用非springApplication方法 18 */ 19 20 public class ManyApplication { 21 22 public static void main(String[] args) { 23 24 // new实例对象调用 25 SpringApplication application = new SpringApplication(); 26 27 // 可以指定多个源的方式 28 Set<String> sets = new HashSet<>(); 29 sets.add(ApplicationDemo.class.getName()); 30 sets.add(User.class.getName()); 31 application.setSources(sets); 32 33 34 35 ConfigurableApplicationContext applicationContext = application.run(args); 36 applicationContext.getBean(Runnable.class).run(); 37 System.out.println(applicationContext.getBean(User.class)); 38 } 39 40 41 }
建立ApplicationDemo
1 package com.srpingboot.nostatic; 2 3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.ComponentScan; 6 7 /** 8 * Created by 9 * 10 * @author: liuya 11 * @Date: 2018/5/20 2:23 12 * @Description: quick_start 13 */ 14 @ComponentScan 15 @EnableAutoConfiguration 16 public class ApplicationDemo { 17 18 @Bean 19 public Runnable createRunnable() { 20 21 return () -> { 22 System.out.println("Spring Boot is Run"); 23 }; 24 25 } 26 }
方法三 :构造函数指定方式
1 package com.springboot; 2 3 import com.srpingboot.nostatic.ApplicationDemo; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.context.ConfigurableApplicationContext; 6 7 import java.util.HashSet; 8 import java.util.Set; 9 10 /** 11 * Created by 12 * 13 * @author: liuya 14 * @Date: 2018/5/20 2:18 15 * @Description: quick_start 16 * <p> 17 * 指定多个源,调用非springApplication方法 18 */ 19 20 21 public class ManyApplication { 22 23 public static void main(String[] args) { 24 25 //构造函数指定方式 26 SpringApplication application = new SpringApplication(ApplicationDemo.class,User.class); 27 28 29 30 ConfigurableApplicationContext applicationContext = application.run(args); 31 applicationContext.getBean(Runnable.class).run(); 32 System.out.println(applicationContext.getBean(User.class)); 33 } 34 35 36 }
源码地址:
https://github.com/liushaoye/quick_start
原文地址:https://www.cnblogs.com/liuyangfirst/p/9062346.html
时间: 2024-10-09 07:48:49