springboot特性
以jar包方式独立运行(jar -jar xxx.jar)
内嵌Servlet容器(tomcat, jetty),无需以war包形式部署到独立的servlet容器中
提供starter简化maven依赖包配置
自动装配bean(大多数场景)
零配置(理论上),Spring 4.x新特性,提倡使用java配置和注解配置结合而无需xml配置
官方向导搭建boot应用
http://start.spring.io/
Spring boot支持需要引入其提供的父POM
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
CI 持续集成 Spring boot 就是一个大管家
Spring boot热加载/部署
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
Spring boot应用打包部署
<build>
<plugins>
<plugin><!-- 项目的打包发布 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.boot.SpringBootMain</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
修改端口
server.port=8888
java -jar xx.jar --server.port=8888
1.@EnableAutoConfiguration:开启自动配置功能,默认本包和子包
@ComponentScan(basePackages={"com.example.boot"}) 包扫描
2.@SpringBootApplication配置详解:
他是一个组合注解,他内部主要包含三个子注解:@SpringBootConfiguration、
@EnableAutoConfiguration、@ComponentScan
@SpringBootConfiguration:他继承@Configuration,说明这是一个配置类,什么是配
置类呢?就相当于我们以前写的xml配置,例如我们我们的bean标签,用来实例化一个bean。
那么在这个配置类中就是实现以前我们xml配置的功能
@EnableAutoConfiguration:开启自动配置功能,他会扫描带有@Configuration的类,
然后初始化这些配置类中的信息并且加入到应用上下文中去,同时完成一些基本的初始化工作
@ComponentScan:组件包扫描,也就是我现在需要扫描哪些包下面的注解,可自动发现和
装配一些bean。默认扫描当前启动类所在包下面的类和下面的所有子包
参数引用
application.properties
teacher.id=1
teacher.name=zhangsan
teacher.info=Teacher ${teacher.name}'s number is ${teacher.id}
随机内容生成
# 随机字符串
random.string=${random.value}
# 随机int
random.number=${random.int}
# 随机long
random.long=${random.long}
# 1-20的随机数
random.b=${random.int[1,20]}
环境配置
总结:
1.application.properties中配置通用内容,并设置spring.profiles.active=dev,
以开发环境为默认配置
2.application-{profile}.properties中配置各个环境不同的内容