spring的refresh方法解析
refresh方法会刷新spring的上下文 里面有多达13种方法,一一解析 prepareRefresh方法 表示在真正做refresh操作之前需要准备做的事情: 设置Spring容器的启动时间, 开启活跃状态,撤销关闭状态,。 初始化context environment(上下文环境)中的占位符属性来源。 验证环境信息里一些必须存在的属性 /** * Prepare this context for refreshing, setting its startup date and * active flag as well as performing any initialization of property sources. */ protected void prepareRefresh() { // Switch to active. // 记录刷新开始时间 this.startupDate = System.currentTimeMillis(); // 上下文活动状态设置为开启 this.closed.set(false); this.active.set(true); if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) { logger.trace("Refreshing " + this); } else { logger.debug("Refreshing " + getDisplayName()); } } // Initialize any placeholder property sources in the context environment. // 初始化上下文中占位符资源 initPropertySources(); // Validate that all properties marked as required are resolvable: //验证所有标记为必需的属性都是可解析的 // see ConfigurablePropertyResolver#setRequiredProperties getEnvironment().validateRequiredProperties(); // Store pre-refresh ApplicationListeners... // 准备预存的监听器 if (this.earlyApplicationListeners == null) { this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners); } else { // Reset local application listeners to pre-refresh state. // 不为空 则监听器重置 this.applicationListeners.clear(); this.applicationListeners.addAll(this.earlyApplicationListeners); } // Allow for the collection of early ApplicationEvents, // 为applicaion事件准备的集合 // to be published once the multicaster is available... // 一旦广播器可用,application事件就会被push进去 this.earlyApplicationEvents = new LinkedHashSet<>(); }
spring声明式事务失效原因
事务失效的原因 数据库引擎不支持事务 (这里以 MySQL 为例,其 MyISAM 引擎是不支持事务操作的,InnoDB 才是支持事务的引擎,一般要支持事务都会使用 InnoDB) 没有被 Spring 管理 方法不是 public的 – 该异常一般情况都会被编译器帮忙识别 When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods. ...
spring事务实战
事务隔离级别 PROPAGATION_REQUIRED 如果当前没有事务,就创建一个新事务,如果当前存在事务,就加入该事务,这是最常见的选择,也是Spring默认的事务传播行为。 一个service内,方法A和方法B事务隔离级别都是REQUIRED,insertAAndB方法无事务 @Override @Transactional(propagation = Propagation.REQUIRED) public void insertA() { User user = new User(); user.setAge(1); user.setName("test"); userDao.insert(user); // A方法抛异常 int i = 1/0; } @Transactional(propagation = Propagation.REQUIRED) public void insertB() { TUser user = new TUser(); user.setAge(1); user.setName("test"); tUserDao.insert(user); } @Override public void insertAAndB() { insertA(); insertB(); } ------------------分界线--------------------- @Test void insertAAndB() { // 一个service内 事务传播行为都是required 测试方法A有异常 方法A成功执行,方法B因为异常未执行 transactionTestAService.insertAAndB(); } 分析:方法A声明了事务,但是因为一个service内,方法 insertAAndB 调用了 方法A和方法B,属于自身调用,事务失效,所以方法A抛异常未回滚。 ...
tensorflow知识点
模块与架构 数据流图 ·