Spring boot来简化Spring应用开发,约定大于配置,整个Spring技术栈的一个大整合,Spring boot是J2EE一站式解决方案,Spring Cloud是分布式整体解决方案。
Spring boot优点:
快速创建独立运行的Spring项目以及与主流框架集成
使用嵌入式的Servlet容器,应用无需打成WAR包
starters自动依赖于版本控制
大量的自动配置,简化开发,也可修改默认值
无需配置XML,无代码生成,开箱即用
准生产环境的运行时应用监控
与云计算的天然集成
微服务
是一种架构风格,一个应用应该是一组小型服务,服务之间可以通过HTTP的方式进行互通。一个微服务加工把每个功能元素放进一个独立的服务中。
创建Springboot项目基本流程
1、创建maven工程,导入Springboot相关依赖,这里使用的是阿里云的仓库
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
2、创建SpringlBoot启动类
@SpringBootApplication
public class HelloWorld {
public static void main(String[] args) {
SpringApplication.run(HelloWorld.class,args);
}
}
3、创建相应的controller、Service等业务逻辑类
@Controller
public class Hello {
@RequestMapping("/hello")
@ResponseBody
public String testHello(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("hello world");
return "hello";
}
}
4、打包项目成可执行的jar 包,需要导入Spring的相关插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
在IDEA中项目右侧Maven Projects可以直接使用Maven 的插件
POM文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
上述父项目的中还依赖父项目,如下,定义各个依赖的版本号,使我们在导入大部分依赖无需写版本号
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web
spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件。
Spring Boot将所有的功能场景都抽取出来,都做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来,需要使用什么场景就导入什么场景启动器。
主程序类:
@SpringBootApplication
public class HelloWorld {
public static void main(String[] args) {
SpringApplication.run(HelloWorld.class,args);
}
}
@SpringBootApplication用于标注这个类是SpringBoot的主配置类,告诉main方法来启动SpringBoot应用。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
@SpringBootConfiguration:SpringBoot的配置类,标注在某个类上表明这是一个SpringBoot的配置类
@EnableAutoConfiguration:开启自动配置功能,告诉SpringBoot开启自动配置功能
@AutoConfigurationPackage:自动配置包,将主配置类(@SpringBootApplication)的所在包及下面所有自爆里面的所有组件扫描到Spring容器
@Import(AutoConfigurationPackages.Registrar.class):Spring的底层注解@Import,给容器中导入一个组件,导入的组件由AutoConfigurationPackages.Registrar确定
@Import(EnableAutoConfigurationImportSelector.class):导入哪些组件的选择器,将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到Spring容器中,最终会导入非常多的自动配置类(xxxAutoConfiguration)
有了自动配置类,免去了我们手动编写配置注入功能组件等的工作,这个配置类如何获取到相应的配置信息呢,从META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作。
AutoConfigurationImportSelector:
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
try {
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
AnnotationAttributes attributes = getAttributes(annotationMetadata);
//获取配置类信息
List<String> configurations = getCandidateConfigurations(annotationMetadata,
attributes);
configurations = removeDuplicates(configurations);
configurations = sort(configurations, autoConfigurationMetadata);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
return configurations.toArray(new String[configurations.size()]);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
Assert.notEmpty(configurations,
"No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
SpringFactoriesLoader:
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
try {
//public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";从jar包的类路径下的这个地址获取相应的autoconfigure配置类
Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
List<String> result = new ArrayList<String>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
String factoryClassNames = properties.getProperty(factoryClassName);
result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
}
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() +
"] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}
自动配置jar包,包含了J2EE的整体整合解决方案和自动配置
resource目录下需的相关目录和资源
static:表示保存所有的静态资源,js,css,images
templates:保存所有的模板页面(Springboot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面),但是可以使用模板引擎(freemaker,thymeleaf)
application.properties:SpringBoot 的应用配置文件,所有SpringBoot的默认配置都可以在这个配置文件中修改。