Spring Boot运行原理

来源:互联网 发布:淘宝二手书怎么卖 编辑:程序博客网 时间:2024/06/01 21:26
查看当前项目已启用和未启用的自动配置的报告
1.运行jar时增加--debug参数
java -jar xx.jar --debug
2.在application.properties中设置属性
debug=true
本质还是基于条件来配置Bean

当某个类存在时,自动配置这个类的Bean,并将Bean属性在application.properties中配置。
写一个starter.pom和自动配置
新建starter的Maven项目

pom.xml修改
<projectxmlns="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.hand</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-starter-hello</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>1.5.8.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>


属性配置的类
packagecom.hand;

importorg.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix ="hello")
public classhelloServiceProperties {
private static finalStringMSG="world";
privateStringmsg=MSG;
publicString getMsg(){
returnmsg;
}
public voidsetMsg(String msg){
this.msg=msg;
}
}
判断依据类
packagecom.hand;

public classhelloService {
privateStringmsg;
publicString sayHello(){
return"hello"+msg;
}
publicString getMsg(){
returnmsg;
}
public voidsetMsg(String msg){
this.msg=msg;
}
}

自动配置类
packagecom.hand;

importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.autoconfigure.condition.ConditionalOnClass;
importorg.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
importorg.springframework.boot.context.properties.EnableConfigurationProperties;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(helloServiceProperties.class)//开启属性注入
@ConditionalOnClass(helloService.class)
@ConditionalOnProperty(prefix ="hello",value ="enabled",matchIfMissing =true)//设置hello为enabled,没设置默认true,即条件符合
public classhelloServiceAutoConfiguration {

//根据helloServiceProperties提供参数,通过ConditionalOnClass判断helloService类在类路径是否存在,容器中无此Bean的情况下
// 则自动配置Bean
@Autowired
privatehelloServicePropertieshelloServiceProperties;
@Bean
@ConditionalOnMissingBean(helloService.class)
publichelloService helloService(){
helloService helloService=newhelloService();
helloService.setMsg(helloServiceProperties.getMsg());
returnhelloService;
}
}

注册配置
若想自动配置生效,需要注册自动配置类
在src/main/resources下新建META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
xxxx(配置类)
若多个自动配置,则“,”隔开,此处\是为了换行后仍能读到属性

将上面构建的starter安装到本地
mvn clean install

新建Spring Boot项目,将starter作为依赖
pom.xml加入
<dependency>
<groupId>com.hand</groupId>
<artifactId>spring-boot-start</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

运行类代码:
packagecom.hand;

importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
importcom.hand.spring_boot_starter.helloService;

@RestController
@SpringBootApplication
public classSpringBootTestApplication {
@Autowired
privatehelloServicehelloService;

@RequestMapping("/")
publicString index(){
returnhelloService.sayHello();
}

public static voidmain(String[] args) {
SpringApplication.run(SpringBootTestApplication.class, args);
}
}
访问结果

在application.properties新增debug=true,查看自动配置报告


原创粉丝点击