Spring 小计

  • 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

​ 符合条件时才进行配置

  • 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
  • 声明式事务

    • 开启事务注解的方式

      • @EnableTransactionManagement
        • proxyTargetClass 基于接口还是基于类
        • mode 是否AspectJ
        • order
      • @Transactional
        • transactionManager
        • propagation
        • isolation
        • timeout
        • readOnly
        • 怎么判断回滚
@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();
    }
}
问:invokeInsertThenRollback 事务能生效吗? 不能 why?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值