国庆学习:springBoot(一)
-
springBoot功能简介
1)创建独立的Spring应用程序
2)嵌入的Tomcat,无需部署war文件
3)提供和开发starter简化Maven配置
4)自动配置Spring
5)提供生产就绪功能,如metrics(指标), health checks(健康检查) and externalized configuration(外部配置)
6)绝对没有代码生成和对XML没有要求配置 -
系统的要求
-
参考手册:https://docs.spring.io/spring-boot/docs/current/reference/html/
-
配置:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
-
核心
1)父工程(Maven配置)温馨提示:我使用的是springframework4.x的版本
官网上给的是springboot2.x版本maven配置,使用的是springframework5.x,需要注意!。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
</parent>
2)启动类:
--------------maven的配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
--------------------------------下面是类的书写
package com.lz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@SpringBootConfiguration
public class ApplicationRun {
/**
run()方法接受启动类和参数
参数由虚拟机传来,例如:
--server.port=8080
**/
public static void main(String[] args) {
//启动springboot程序
SpringApplication.run( ApplicationRun.class, args);
}
}
3)打包:
插件:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
4)启动方式:
1.main方法运行。
2.mvn spring-boot:run
携带参数:
mvn spring-boot:run -Dspring.profiles.active=true
传参建议使用-D。
例如:
-Dspring.profiles.active=prod
也要可以Eclipse的程序参数里直接添加:spring.profiles.active=prod
或者Eclipse的虚拟机参数里添加:-Dspring.profiles.active=dev
3.打包运行:
(1)可以携带参数:
java -jar xx.jar --spring.profiles.active=prod
(2)webapp目录下的jsp文件不会打入jar包,但是可以打入war包,因此,如果含有jsp的话,需要打成war包。运行方式仍然是:
java -jar xxxx.war
4.原理图:
-
配置
1)application.peoperties
例如:`
application-dev.properties 开发环境
server.port=80
jdbc.url=jdbc:mysql//192.168.1.1/user
jdbc.username=root
jdbc.password=root
application-prod.properties生产环境
server.port=80
jdbc.url=jdbc:mysql//10.1.136.1/user
jdbc.username=root
jdbc.password=root
application.properties
spring.profiles.active=dev
或者
–spring.profiles.active=dev`
2)yml(推荐 层次结构清晰)
application-dev.yml 开发环境
----------------------------------------------------
server:
port: 80
context-path: /
servlet-path:
welcome-files: index.jsp
jdbc:
url: jdbc:mysql//192.168.1.1/user
username: root
password: root
application-prod.yml 生产环境
--------------------------------------------------------
server:
port: 80
context-path: /
servlet-path:
welcome-files: index.jsp
jdbc:
url: jdbc:mysql//10.1.136.1/user
username: root
password: root
application.yml
--------------------------------------------------------
spring:
profiles:
active: dev
启动类会根据application.yml找到指定配置,可以给启动类添加程序参数或虚拟机参数:
程序参数:spring.profiles.active=dev
虚拟机参数:-Dspring.profiles.active=dev
3)包含配置:
spring.profiles.include=
例如把JDBC配置提取出单独的properties文件
application-jdbc.properties
然后在主配置中包含:
spring.profiles.include=jdbc
4)读取默认配置:@ConfigurationProperties(prefix = “jdbc”)
例如读取默认配置文件的jdbc项:
@Component
@ConfigurationProperties(prefix = "jdbc")
public class Config {
@Value("${drvier}")
private String drive;
@Value("${url}")
private String url;
}
5)读取自定义配置:@PropertySource(“classpath:jdbc.properties”)
@Component
@PropertySource("classpath:jdbc.properties")
public class Config {
@Value("${jdbc.drvier}")
private String drive;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource getDataSource(){
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl(this.url);
config.setUser(this.username);
config.setPassword(this.password);
DataSource dataSource = new BoneCPDataSource(config);
System.out.println("数据源初始化完成\n" + this.url);
return dataSource;
}
}
- 日志:Spring-Boot默认使用的是logback,它是 log4j 2.0
1)简单配置:
例如:
logging.file=D:/mylog/log.log
logging.level.=INFO #相当于ROOT
logging.level.org.springframework.web=INFO
# Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.config=指定log4j的xml配置文件位置
# Conversion word used when logging exceptions.
logging.exception-conversion-word=%wEx
# Log file name. For instance `myapp.log`
logging.file=
# Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.level.*=
# Location of the log file. For instance `/var/log`
logging.path=
# Appender pattern for output to the console. Only supported with the default logback setup.
logging.pattern.console=
# Appender pattern for output to the file. Only supported with the default logback setup.
logging.pattern.file=
# Appender pattern for log level (default %5p). Only supported with the default logback setup.
logging.pattern.level=
# Register a shutdown hook for the logging system when it is initialized.
logging.register-shutdown-hook=false
2)使用xml
properties配置:
logging.config=classpath:log4j.xml
log4j.xml例子:
-----------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="CONSOLE" />
</root>
</loggers>
</configuration>
- 热部署:SpringBoot提供了一个maven插件来支持热部署spring-boot-devtools,仅仅是在开发环境中使用,如果已经打包了就无法使用。
1)热部署:
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
2)增强配置:
禁用重启
spring.devtools.restart.enabled=false
实时加载:内部嵌入了LiveReload服务器,
默认会所有目录改变都加载,可以添加额外监控目录,一旦目录文件改变,就会触发重新加载
#spring.devtools.restart.additional-paths=src/main/...............
排除某些目录改变后不加载(例如静态资源目录):
spring.devtools.restart.exclude=static/**,public/**
当以下指定的文件被修改,则触发重新载。例如可以在生产环境某个位置放一个触发文件,当其它配置修改后需要重启,则可以修改这个文件触发。
spring.devtools.restart.trigger-file=文件
一些学习的分享!理解不正确的地方希望大佬指点。觉得有用请踊跃评论!下次分享一下学习的jpa。希望得到赞赏!谢谢!