1.Spring Boot 简介
a.优点
1.2014由于微服务提出开发的框架,目的简化spring开发
2.使用嵌入式的servlet容器,无需打包成war
3.starters启动器自动依赖与版本控制
4.大量自动配置,无需配置xml,无代码生成(写好的api)
5.准生成环境的运行时应用监控,与云计算的天然集成
b.缺点
入门容易,精通难。封装的api多。
2.微服务(Microservices)
是一种架构分格
一个应用应该是一组小型服务,可以通过http方式进行互通;
每个功能元素都可以替换更改,根据需求进行的动态组合
单体应用架构:且一发而动全身
3.Maven配置
<!-- 本地仓库-->
<localRepository>D:\Java\tools\Maven\res</localRepository>
<!-- 配置远程仓库 -->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>mirror2</id>
<mirrorOf>central2</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2</url>
</mirror>
<!-- 配置jdk -->
<profile>
<id>jdk-1.8</id>
<activation>
<jdk>1.8</jdk>
</activation>
<repositories>
<repository>
<id>jdk18</id>
<name>Repository for JDK 1.8 builds</name>
<url>http://www.myhost.com/maven/jdk18</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
4.创建springboot
1.创建maven项目
2.导入springboot依赖
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.21.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>
3.编写一个主程序
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
4.编写controller、service
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello world!";
}
}
5.简化部署
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
java -jar xxx.jar
5.helloworld探究
1.POM文件
1.父项目
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.21.RELEASE</version>
</parent>
他的父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.21.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
他来真正管理springboot里面的所有依赖版本;
springboot的版本仲裁中心;
以后我们导入依赖默认是不需要写版本;(除非developers没有)
2.导入依赖
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter-web;
spring-boot-starter:springboot场景启动器,帮我们导入了web模块正常运行所依赖的组件;
Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目引入这些starters场景的所有依赖都会导入进来,要用什么功能就导入什么场景的启动器;
2.主程序类
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
@SpringBootApplication springboot应用标注在类说明是springboot的主配置类,启动应用应该运行这个main方法;
@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的配置类;
@Configuration:配置类上来标注这个注解;spring定义的;
配置类—配置文件:配置类也是容器中一个组件;@Component
@EnableAutoConfiguration : 开启自动配置功能;
以前我们需要配置的东西,springboot帮我们自动配置,告诉springboot开启自动配置;
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage :自动配置包
@Import(AutoConfigurationPackages.Registrar.class)
spring底层注解@Import,给容器导入一个组件;导入组件由AutoConfigurationPackages.Registrar.class;
将主配置类(@SpringBootConfiguration标注的类)的所在包及下面子包里面所有组件扫描到spring容器;
**@Import(EnableAutoConfigurationImportSelector.class)**导入那些组件的选择器;
a.将所有需要导入的组件以全类名方式返回,这些组件就会被添加到容器;
b.会给容器自动导入非常多自动配置类----就是给容器中导入这个场景需要的所有组件,并配置好这些组件;
有了自动配置类,免去了手动编写注入功能组件等工作;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,beanClassLoader);
从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入容器中,自动配置类生效,帮我们自动配置;
==============================
J2EE整体解决方案和自动配置都在spring-boot-autoconfigure-1.5.21.RELEASE.jar
6.使用Spring Initializr快速创建SpringBoot项目
·主程序已经配置好
·resource文件夹中目录结构:
-static:所有静态资源
-templates:所有模板引擎;(spring Boot默认jar使用嵌入式Tomcat,默认不支持jsp页面,但可以使用模板引擎,如freemarker,thymeleaf)
-application.properties:Spring Boot应用配置文件;