IOC
这里IoC容器就解决了这个问题。这个容器可以自动对你的代码进行初始化,你只需要维护一个Configuration(可以是xml可以是一段代码),而不用每次初始化一辆车都要亲手去写那一大段初始化的代码。这是引入IoC Container的第一个好处。 ...
这里IoC容器就解决了这个问题。这个容器可以自动对你的代码进行初始化,你只需要维护一个Configuration(可以是xml可以是一段代码),而不用每次初始化一辆车都要亲手去写那一大段初始化的代码。这是引入IoC Container的第一个好处。 ...
xml 配置bean方式 注解式配置bean方式 1. @Component 声明 /** * @program: springboot * @description: * @author: ggBall * @create: 2021-02-20 17:11 **/ @Component @Data public class Dog { private String name; private Integer age; } 结果 2. 在配置类中使用 @Bean注解 结果 3. 继承 FactoryBean 结果 4. 继承BeanDefinitionRegistryPostprocessor ...
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<>(); }
事务失效的原因 数据库引擎不支持事务 (这里以 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. ...