- curl
curl http://localhost:8080/hello
hello world
- actuator
curl http://localhost:8080/actuator/health
{"status":"UP"}
- 启动Java进程
java -jar java -jar Spring-0.0.1-SNAPSHOT.jar
// nohub 表示后台启动程序,打印日志到指定文件 (linux)
nohub java -jar java -jar Spring-0.0.1-SNAPSHOT.jar
- CommandLineRunner
// 在springboot项目启动后,需要执行的操作
@FunctionalInterface
public interface CommandLineRunner {
/**
* Callback used to run the bean.
* @param args incoming main method arguments
* @throws Exception on error
*/
void run(String... args) throws Exception;
}
- http://localhost:8080/actuator/beans 无法访问
// 暴露actuator所有端点
management.endpoints.web.exposure.include=*
- SpringBoot 自动配置
- DataSourceAutoConfiguration
- 配置DataSource
- DataSourceTransactionManagerAutoConfiguration
- 配置DataSourceTransactionManager
- JdbcTemplateAutoConfiguration
- 配置JdbcTemplate
- DataSourceAutoConfiguration
符合条件时才进行配置
- H2: Springboot 默认内嵌的内存数据库
resources下,
schema.sql 初始化表
data.sql 初始化数据
- 数据源配置
# 数据库
spring.datasource.driver-class-name=
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
-
多数据源配置
- @ConfigurationProperties 读取指定前缀的配置,返回绑定不同配置的数据源
-
Springboot 2.x 连接池
- 默认使用
HikariCP
- 配置spring.datasource.hikari.*
- 默认使用
-
Springboot 1.x
- 默认使用Tomcat连接池,需要移除tomcat-jdbc依赖
- spring.datasource.type=com.zaxxer.hikari.HikariDataSource
-
Alibaba Druid
1. 详细的监控
2. SQL防注入
3. 内置加密配置
4. 扩展点多
- 数据源配置
- 直接配置DruidDataSource
- 通过druid-spring-boot-starter
- spring.datasource.druid.*
- JdbcTemplate
- query
- queryForList
- queryForObject
- update
- execute
- batchUpdate (BatchPreparedStatementSetter)
- NamedParameterJdbcTemplate
/**
* Template class with a basic set of JDBC operations, allowing the use
* of named parameters rather than traditional '?' placeholders.
* /
-
Spring的事务抽象
-
一致的事务模型
- JDBC/Hibernate/myBatis
- DataSource/JTA
-
事务抽象的核心接口
-
PlatformTransactionManager
- DataSourceTransactionManager
- HibernateTranscationManager
- JtaTransactionManager
-
void commit(TransactionStatus var1) throws TransactionException; void rollback(TransactionStatus var1) throws TransactionException;
- TransactionDefinition
- propagation 事务传播特性
- isolation 事务隔离级别
-
-
编程式事务
- TransactionTemplate
- 有返回 TransactionCallback
- 无返回 TransactionCallbackWithoutResult
- TransactionTemplate
-
声明式事务
-
开启事务注解的方式
- @EnableTransactionManagement
- proxyTargetClass 基于接口还是基于类
- mode 是否AspectJ
- order
- @Transactional
- transactionManager
- propagation
- isolation
- timeout
- readOnly
- 怎么判断回滚
- @EnableTransactionManagement
-
@Component
public class FooServiceImpl implements FooService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
@Transactional
public void insertRecord() {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('AAA')");
}
@Override
@Transactional(rollbackFor = RollbackException.class)
public void insertThenRollback() throws RollbackException {
jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('BBB')");
throw new RollbackException();
}
@Override
public void invokeInsertThenRollback() throws RollbackException {
insertThenRollback();
}
}