Java七大热门技术框架源码解析
核心代码,注释必读
// download:
3w ukoou com
Spring Boot架构设计解析 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
-
可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;
-
内嵌Tomcat或Jetty等Servlet容器
-
提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;
-
尽可能自动配置Spring容器;
-
提供准备好的特性,如指标、健康检查和外部化配置;
-
绝对没有代码生成,不需要XML配置
SpringBoot自带的很多配置减少了开发人员的手动配置,而且还减少了XML文件的配置。框架中的两个重要的策略:开箱即用,约定优于配置。所带来的优势相较于之前的SpringMVC项目需要众多的XML文件,springboot的框架已经都进行了封装,以组件的形式,在需要的时候,在pom中添加依赖即可。
简单来说, 就是快,能够在最短的时间内完成之前spring框架要配置许久的事情。
封装有好处,就有坏处,好处是用起来真的好爽,什么都不用考虑,坏处是一旦出现问题,或者想自己搞一个自定义的组件,就无从入手。
Java七大热门技术框架源码解析 - Spring Boot自定义Starter简化模块集成
starter 是 Spring Boot 中一种非常重要的机制,它可以将繁杂的配置统一集成到 starter 中,我们只需要通过 maven 将 starter 依赖导入到项目中,Spring Boot 就能自动扫描并加载相应的默认配置。
starter 的出现让开发人员从繁琐的框架配置中解放出来,将更多的精力专注于业务逻辑的开发,极大地提高了开发效率。
在一些特殊情况下,我们也可以将一些通用功能封装成自定义的 starter 进行使用,以下详细介绍如何自定义 starter。
1、创建两个工程
我们需要先创建两个工程 hello-spring-boot-starter 和 hello-spring-boot-starter-autoconfigurer
hello-spring-boot-starter-autoconfigurer
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>hello-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>autoconfigurer</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
项目结构:
项目结构
HelloProperties.java
package com.example.autoconfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* hello 配置属性
*
* @author lz
* @date 2019/8/23
*/
@ConfigurationProperties(prefix = HelloProperties.HELLO_PREFIX)
public class HelloProperties {
public static final String HELLO_PREFIX = "project.hello";
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
HelloService.java
package com.example.autoconfigurer;
/**
* Hello 服务
*
* @author lz
* @date 2019/8/23
*/
public class HelloService {
HelloProperties helloProperties;
HelloProperties getHelloProperties() {
return helloProperties;
}
void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name) {
return helloProperties.getPrefix() + " " + name+" " + helloProperties.getSuffix();
}
}