1.配置Tomcat端口 和项目名称
server.port = 8080
server.context-path=/springboot
2.配置热部署
第一步:添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
第二步:配置插件
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
第三步:打开自动编译
File -> Settings -> Build,Execution,Deployment->Compiler下,
打开Build project automatically选项
第四步:打开运行时编译
按快捷键 Shift+Ctrl+Alt+/ ,选择 Registr,打开
compiler.automake.allow.when.app.running选项
3.配置filter
第一步:创建自己的filter类实现filter接口,重写doFilter方法
第二步:在核心类中添加bean的管理
@Bean
public FilterRegistrationBean init(){
FilterRegistrationBean rbean = new FilterRegistrationBean();
rbean.setFilter(new MyFilter());
rbean.addUrlPatterns("/*");
rbean.setName("myFilter");
return rbean;
}
4.配置application.properties中配置数据:
com.name = hello
在bean中注入配置数据
@Value("${com.name}")
修改默认的配置文件
SpringApplication app = new SpringApplication(SpringbootTestApplication.class);
Properties properties = new Properties();
properties.load(SpringbootTestApplication.class.getClassLoader().getResourceAsStream("test.properties"));
app.setDefaultProperties(properties);
app.run(args);
5.配置自定义父工程,修改pom文件,将原来的parent节点换成指定依赖
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.10.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
6.springboot使用yml文件配置
springboot使用两种配置文件,一种是application.properties,另外一种是application.yml文件。yml文件层次感强些如下:
spring:
profiles:
active: prod
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: root
yml 文件在写的时候层次感强,而且少写了代码。
yml的一些配置:
server:
port: 8090 //配置端口
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8
spring:
datasource: //数据库配置
url : jdbc:mysql://localhost:3306/mydb
username : root
password : root
driverClassName : com.mysql.jdbc.Driver
在代码中获取yml文件的数据与之前是一致的。
配置格式:
spring:
datasource:
driver: abc
获取方式:
@Value("${spring.datasource.driver}")
private String driver;
yml的多套配置:
根据不同的环境编写多套配置:
application-aa.yml和application-bb.yml
在主配置中(application.yml)编写如下代码:
spring:
profiles:
active: aa