springBoot第一弹

qq_37105917 2020-05-06 09:58:17
1、springBoot 简介
简化Spring应用开发
整个spring技术栈的大整合
J2EE开发的一个一站式解决方案
2、微服务
微服务一种架构风格
一个应用应该是一组小型服务
可以通过HTTP的方式进行沟通
单体应用:ALL IN ONE
说明:scale ——扩展
每一个功能元素最终都有一个课独立替换、可独立升级的软件单元
springBoot shiyong spring Cloud 来互联互相调用
3、环境准备
-jdk1.8
-idea
-maven
-SpringBoot 1.5.9版本及以上 需要使用jdk1.8版本

1、maven设置
设置maven的setting文件 添加变异设置 jdk1.8

2、Idea 整合maven
4、SpringBoot
1、创建maven 项目
2、导入springBoot 的依赖
3、编写主程序:启动springboot
Psvm idea 快捷键 创建main方法
5、运行主程序测试

6、简化部署
达成jar包 spring 官方文档 11.5 创建一个可执行jar包
Springboot 打成 jar包配置

org.springframework.boot

Spring-boot-maven-plugin
7、hello word 探究
1、pom文件
Springboot 项目依赖一个父项目
所依赖的父项目 标注了所有相关依赖包的依赖
该 pom文件 管理了springboot 版本仲裁中心
以后直接导入jar包 并不需要导入版本号
说明:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/><!--lookupparentfromrepository-->
</parent>
创建的springboot项目依赖的parent项目,在该项目中还依赖了一个spring-boot-dependencies 项目,pom文件如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
进入该项目的配置文件中我们可以看到,springBoot 为我们配置好了需要依赖的包版本信息
说明:若是引入的jar不在 springboot仲裁 那就需要 标注版本号
2、导入依赖
Spring-boot-start-web
Spring-boot-start
springboot场景启动器
帮我们导入 web模块相关的依
3、springboot 启动器分类
springBoot将所有的功能场景都抽取出来,做成启动器,字需要在项目引入这些starter 相关场景所有的依赖导入进来,只要导入场景启动器就可以了。
在 spring-boot-starters项目中我们可以看到以下子项目(都是启动器)
<module>spring-boot-starter</module>
        <module>spring-boot-starter-activemq</module>
        <module>spring-boot-starter-amqp</module>
        <module>spring-boot-starter-aop</module>
        <module>spring-boot-starter-artemis</module>
        <module>spring-boot-starter-batch</module>
        <module>spring-boot-starter-cache</module>
        <module>spring-boot-starter-cloud-connectors</module>
        <module>spring-boot-starter-data-cassandra</module>
        <module>spring-boot-starter-data-couchbase</module>
        <module>spring-boot-starter-data-elasticsearch</module>
        <module>spring-boot-starter-data-gemfire</module>
        <module>spring-boot-starter-data-jpa</module>
        <module>spring-boot-starter-data-ldap</module>
        <module>spring-boot-starter-data-mongodb</module>
        <module>spring-boot-starter-data-neo4j</module>
        <module>spring-boot-starter-data-redis</module>
        <module>spring-boot-starter-data-rest</module>
        <module>spring-boot-starter-data-solr</module>
        <module>spring-boot-starter-freemarker</module>
        <module>spring-boot-starter-groovy-templates</module>
        <module>spring-boot-starter-hateoas</module>
        <module>spring-boot-starter-integration</module>
        <module>spring-boot-starter-jdbc</module>
        <module>spring-boot-starter-jersey</module>
        <module>spring-boot-starter-jetty</module>
        <module>spring-boot-starter-jooq</module>
        <module>spring-boot-starter-jta-atomikos</module>
        <module>spring-boot-starter-jta-bitronix</module>
        <module>spring-boot-starter-jta-narayana</module>
        <module>spring-boot-starter-logging</module>
        <module>spring-boot-starter-log4j2</module>
        <module>spring-boot-starter-mail</module>
        <module>spring-boot-starter-mobile</module>
        <module>spring-boot-starter-mustache</module>
        <module>spring-boot-starter-actuator</module>
        <module>spring-boot-starter-parent</module>
        <module>spring-boot-starter-security</module>
        <module>spring-boot-starter-social-facebook</module>
        <module>spring-boot-starter-social-twitter</module>
        <module>spring-boot-starter-social-linkedin</module>
        <module>spring-boot-starter-remote-shell</module>
        <module>spring-boot-starter-test</module>
        <module>spring-boot-starter-thymeleaf</module>
        <module>spring-boot-starter-tomcat</module>
        <module>spring-boot-starter-undertow</module>
        <module>spring-boot-starter-validation</module>
        <module>spring-boot-starter-web</module>
        <module>spring-boot-starter-websocket</module>
        <module>spring-boot-starter-web-services</module>

8、helloword
主程序类
注解 @SpringBootApplication
说明该类是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@interfaceSpringBootApplication{

组合注解
@SpringBootConfiguration
该注解是标识该类是springboot配置类
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public@interfaceSpringBootConfiguration{

@Configuration 配置类上标注该注解
配置类——配置文件
Spring 注解
配置类也是容器中的一个组件:@Component
@EnableAutoConfiguration
开启自动配置功能

以前需要配置的东西,现在spring Boot 帮我们配置

@EnableAutoConfiguration 告诉springboot

AutoConfigurationPackage:自动配置包
@Import({Registrar.class})
spring底层注解 ,给容器中导入一个组件:导入的组件由Registrar.class
AutoConfigurationPackage 将主配置类所在包以及所有子包里面的所有组件扫描到spring容器中


@Import({EnableAutoConfigurationImportSelector.class}) 注解 给容器导入EnableAutoConfigurationImportSelector

该类的父类有个方法selectImports ()
将所有导入的组件以全雷鸣的方式返回;这些主键就会以犬类名的方式加到容器中;

那么到底导入了哪些组件:
List<String> configrations 会给容器中导入非常多的自动配置类(xxxAutoConfiguration):j就是给容器中导入这个场景所需要的所有组件,并配置好这些组件;


有了自动配置类,就免去了手动配置,和注入功能组件的工作。

那么如何做到自动配置的那?
List<String>configurations=this.getCandidateConfigurations(annotationMetadata,attributes);

这个方法里面说明了问题:
SpringFactoriesLoader.loadFactoryNames()

该方法参数:EnableAutoConfiguration.class,his.beanClassLoader;

该方法逻辑



J2EE 的整体解决方案和自动配置都在:




——————————————————
9、使用 spring Initializer 创建向导 创建一个springBoot项目

10 、springboot 配置

1、配置文件
Application.properties
Aplication.yml

这两个都可以作为全局配置文件,文件名是固定的

配置文件的作用:来修改springBoot 的默认值。
2、yml 基本语法
1、K: V :表示一个键值对(V前面必须有空格)
"":不会转移字符串里面的特殊字符:特殊字符会做为本身表达的意思
‘’:会转移特殊字符 例如 ‘小明 \n 信你’ 输出 ”小明 \n 信你“
属性和值是大小写敏感的。
2、值的写法
字面量:普通的值(数字、字符串、布尔)
对象、map(属性和值)(键值对)
数组(list\set)

用- 值表示数组中的一个元素

Pets:
- cat
- pig
- xiaoming
行内写法类似 json 格式

11、配置文件值注入
配置文件:
person:
name:小明
age:18
dog:
name:艾瑞克
age:3
注解配置实体类
importsun.misc.Contended;

/**
*将配置文件中的属性值映射到对象中
*ConfigurationProperties告诉springBoo将本类中所有的属性和配置文件中相关的配置进行绑定
*只有该组件是容器中的组件,才能使用
*Component加入容器
*/
@Component
@ConfigurationProperties(prefix="person")
导入配置文件处理器
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

导入处理器之后会有yml提示

lastName 和last-name 等效
12、springBoot 配置properties配置文件编码问题
1、配置对象属性
person.name=小明
Person.age=18
Person.map.k1=xiaoming
2、乱码问题
Idea配置编码是 utf-8

3、配置 @configurationProperties 和@value的区别
configurationProperties 支持松散绑定
value 不支持松散语法绑定
value 支持spring语法绑定(Spel)
configurationProperties 不支持spring语法绑定(Spel)
configurationProperties Jsr3030数据校验
value 不支持
configurationProperties 支持复杂类型注解
value 不支持

如果说,我们只是需要获取配置文件中的某个值,推荐使用@value
如果说,我们编写了一个javaBean来和配置文件映射,那么推荐使用configurationProperties ,一次性全部注入
4、数据校验
/**
*将配置文件中的属性值映射到对象中
*ConfigurationProperties告诉springBoo将本类中所有的属性和配置文件中相关的配置进行绑定
*只有该组件是容器中的组件,才能使用
*Component加入容器
*Validated需要校验
*/
@Component
@Validated
@ConfigurationProperties(prefix="person")
publicclassPerson{

@Email//标识需要是邮箱格式
13、@PropertySource 、@ImportResource 、@Bean

@PropertySource 加载外部配置文件(类路径下的文件)
@ImportResource 导入spring的配置文件,让配置文件里面的配置内容生效;
@ImportResource 标注主配置类上,在容器启动的时候回加载该路径下的文件
例如加入组件:
2、springBoot 推荐给容器中添加组件的方式;使用全注解的方式
1、写个配置类
创建配置类并添加注解
@Configration 指明当前类是一个配置类,就是替代之前的spring配置文件的
@Configration
Public class Myconfigration(){
@Bean
Public HelloWorldService helloworldService(){
Return new HelloWorldService ();
}
}
14、配置文件的占位符
1、配置文件中可以使用随机数
随机数:${random.uuid}
2、站位符
${person.nameq:小明}_dog : person.name 如果在配置文件中存在则引用存在值,弱不存在测使用默认值:小明
15、Profile 多环境支持
1、多profile的方式
我们在主配置文件的时候
server:
port:8081

person:
name:小明
age:18
dog:
name:艾瑞克
age:3
#指定激活使用的配置文件
spring:
profiles:
active:uat
---#文档块
server:
port:8083
spring:
profiles:dev
---
server:
port:8084
spring:
profiles:uat //指定当前文档块属于哪个环境
---

2、命令行指定环境配置模式
--spring.profiles.active=dev
Idea 配置位置:

优先 idea 命令行参数
jar包启动的时候 设置spring.profiles.active=dev参数
3、设置虚拟机参数
-Dspring.profiles.active=dev


当存在虚拟机参数的时候优先虚拟机参数
16、配置文件的加载位置
概述:
spring Boot 启动的时候会扫描一下位置的application.properties 或者application.yml文件作为spring Boot的默认配置文件
—file:./config/
—file:./
—classpath:/config/
—classpath:/
—以上是按照优先级从高到低的顺序,所有位置的文件都会被加载,搞优先级配置内容会覆盖低优先级配置内容
—我们可以通过配置spring.config.location来该表默认配置
1、优先级加载顺序,加载完成之后,会形成配置文件之间的互补。
2、可以通过spring.config.location改变默认配置
该参数只用来项目大号包之后启动命令使用改配置改变配置文件,改配置文件会和spring项目配置的配置文件一起起作用。
优点:如果项目发布参数配置不对,可以添加外部配置文件的方式修改回来,而不用从新打包发布。
3、外部配置的加载顺序
·springBoot支持多种外部配置方式
这些优先级如下:
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config
SpringBoot 可以从以下位置加载配置文件,所有的配置会形成互补配置。
1、命令行参数
2、来自java:comp/env的JNDI属性
3、java系统属性(System.getProperties)
4、操作系统环境变量
5、RandomValuePropertySource配置的random.*属性值
6、jar包外部的application-{profile}.properties或者application-{profile}.yml(带spring.profile)配置文件的
7、jar包内部的application-{profile}.properties或者application-{profile}.yml(带spring.profile
...全文
77 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧