框架学习之SpringBoot

框架学习之SpringBoot
开发环境的约束:jdk1.7+,maven3.3+
开发前准备:
1.maven配置:maven安装目录下conf文件夹——settings.xml

<!-- java编译插件,配jdk的编译版本-->
  <profile>    
          <id>jdk-1.8</id>    
          <activation>    
               <activeByDefault>true</activeByDefault>    
               <jdk>1.8</jdk>    
            </activation>    
          <properties>    
               <maven.compiler.source>1.8</maven.compiler.source>    
               <maven.compiler.target>1.8</maven.compiler.target>    
               <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    
         </properties>    
    </profile> 
  </profiles>

2.idea配置maven:
框架学习之SpringBoot
创建SpringBoot项目:
1.创建maven工程,手动导入依赖:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.使用Spring Initializr快速创建,选择所需依赖即可
–eclipse中:使用Spring Starter Project快速创建
框架学习之SpringBoot
简化部署:
这个插件可以将应用打包成一个可运行的jar包(java -jar)
Maven Projects——对应项目名下的Lifecycle——package

<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
   </plugins>
</build>

启动方式:
[email protected]:自动配置,扫包范围默认当前类
[email protected](“包路径”)
[email protected]:当前包和同级包
在类上加注解:@SpringBootApplication,代表主程序类、主入口类、主配置类
框架学习之SpringBoot
框架学习之SpringBoot
@SpringBootConfiguration:SpringBoot的配置类
@Configuration:配置类上加这个注解(配置类——配置文件)
@Component:容器中的组件
@EnableAutoConfiguration:开启自动配置
@AutoConfigurationPackage:自动配置包
@Import({Registrar.class})——spring底层注解@Import,给容器中导入一个组件

配置文件:可以修改一些默认值(名字不可变,全局配置文件)
1.application.properties
2.application.yml
框架学习之SpringBoot
application.properties:
框架学习之SpringBoot
application.yml:
k: v表示一对键值对(属性和值用空格隔开),属性和值大小写敏感
框架学习之SpringBoot
配置文件中,值注入的两种方式:
[email protected]:默认从全局配置文件中获取
[email protected](“配置文件中对应的属性名”)

 @Value("${person.name}")
    private String name;

如果说,我们只是在某个业务逻辑中需要获取配置文件中某项值,使用@Value
如果说,编写了一个JavaBean来和配置文件进行映射,使用@ConfigurationProperties
框架学习之SpringBoot
配置文件中编码问题:
配置文件默认utf-8
框架学习之SpringBoot
@PropertySource:加载指定的配置文件
框架学习之SpringBoot
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效
框架学习之SpringBoot
框架学习之SpringBoot
框架学习之SpringBoot
SpringBoot推荐给容器中添加组件的方式:配置类
框架学习之SpringBoot
框架学习之SpringBoot
配置文件占位符:
1.随机数
占位符获取之前配置的值,如果没有可以用**:**指定默认值

person.name=小一${random.uuid}
person.age=${random.int}
person.boss=false
person.birth=1998/2/20
person.map.k1=v1
person.map.k2=v2
person.list=a,b,c
person.dog.name=小黄${person.hello:hello}_dog
person.dog.age=1

Profile:SpringBoot对不同环境提供不同配置功能的支持
1.多Profile文件
2.yml支持多文档块
3.**指定Profile:
spring.profiles.active
命令行:
框架学习之SpringBoot
虚拟机参数:
框架学习之SpringBoot
配置文件加载位置:
SpringBoot启动会扫描application.properties或application.yml文件作为默认的配置文件
-file:./config/
-file:./
-classpath:/config/
-classpath:/
以上优先级由高到低,所有位置的文件都会被加载,高优先级会覆盖低优先级的配置内容
还可以通过配置spring.config.location来改变默认配置

外部配置加载顺序:优先级由高到低,所有配置形成互补配置
1.命令行参数
Java -jar打的jar包名(例:SpringBoot04-0.0.1-SNAPSHOT.jar) --server.context-path=值
多个配置用空格分开 --配置项=值

2.来自java:comp/env的NDI属性
3.Java系统属性(System.getProperties())
4.操作系统环境变量
5.RandomValuePropertySource配置的random.*属性值
6.Jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
7.Jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
8.Jar包外部的application.properties或application.yml(不带spring.profile)配置文件
9.Jar包内部的application.properties或application.yml(不带spring.profile)配置文件
[email protected]注解类上的@PropertySource

11.通过SpringApplication.SetDefaultProperties指定的默认属性