spring生命周期回调

结合生命周期机制(官网提供)

  1、实现InitializingBean接口重写void afterPropertiesSet() throws Exception;方法

    使用场景:再bean构造方法不方便处理场景下可以使用以上方式进行处理。

  2、自定义init方法(xml)

    <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>

    

public class ExampleBean {

    public void init() {
// do some initialization work
}
}

  

  3、注解方式@PostConstruct

public class ExampleBean {
@PostConstruct
public void init() {
// 此方法名随便定义,最好使用此种方式,非侵入式
}
}

  

销毁回调就不在重复记录,官网很明确。

 从Spring 2.5开始,您有三个控制bean生命周期行为的选项:

includeFiltersexcludeFilters

  使用场景:对某一bean进行排除过滤或者添加过滤使用

  

@Configuration
@ComponentScan(basePackages = "org.example",
includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
excludeFilters = @Filter(Repository.class))
public class AppConfig {
...
}

  

  

@Primary注释使用

  使用场景,当两个数据源或者bean为了告诉系统那个是主要的则使用@primary

  由于按类型自动装配可能会导致多个候选人,因此通常需要对选择过程进行更多控制。实现这一目标的一种方法是使用Spring的@Primary注释。@Primary表示当多个bean可以自动装配到单值依赖项时,应该优先选择特定的bean。如果候选者中只存在一个主bean,则它将成为自动装配的值

  

@Configuration
public class MovieConfiguration { @Bean
@Primary
public MovieCatalog firstMovieCatalog() { ... } @Bean
public MovieCatalog secondMovieCatalog() { ... } // ...
}

  

@Primary注释使用

   @Primary当可以确定一个主要候选者时,是通过具有多个实例的类型使用自动装配的有效方式。当您需要更多控制选择过程时,可以使用Spring的@Qualifier注释。您可以将限定符值与特定参数相关联,缩小类型匹配集,以便为每个参数选择特定的bean。在最简单的情况下,这可以是一个简单的描述性值

生成候选组件索引

  使用场景,大型应用启动性能较慢的情况

  虽然类路径扫描速度非常快,但可以通过在编译时创建候选的静态列表来提高大型应用程序的启动性能。在此模式下,所有作为组件扫描目标的模块都必须使用此机制。

  要生成索引,请为包含组件扫描指令目标的组件的每个模块添加其他依赖项。以下示例显示了如何使用Maven执行此操作:

  

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<version>5.1.8.RELEASE</version>
<optional>true</optional>
</dependency>
</dependencies>

  

  对于Gradle 4.5及更早版本,应在compileOnly 配置中声明依赖项,如以下示例所示

dependencies {
compileOnly "org.springframework:spring-context-indexer:5.1.8.RELEASE"
}

  

  使用Gradle 4.6及更高版本时,应在annotationProcessor 配置中声明依赖项,如以下示例所示:

dependencies {
annotationProcessor "org.springframework:spring-context-indexer:5.1.8.RELEASE"
}

  

最新文章

  1. The Engine Document of JustWeEngine
  2. nginx实现本地图片生成缩略图
  3. .htaccess重命名时提示必须键入文件名怎么解决
  4. HTTP接口功能自动化测试入门
  5. verilog 数组参数
  6. forward和redirect 的区别
  7. sizeof(long)
  8. 【SQL】T-SQL基本语法复习
  9. WARN Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect (org.apache.zookeeper.ClientCnxn)
  10. C6678的PLL模块设置
  11. java Thread 类的源码阅读(oracle jdk1.8)
  12. ldd源码编译出现的问题
  13. 移动端开发demo—移动端web相册(一)
  14. 【转】Jira插件安装
  15. Python 多线程、进程
  16. nginx环境搭建
  17. $(QTDIR);$(QTDIR)\include\QtCore;$(QTDIR)\include;
  18. zoj 2876 Phone List
  19. 解决方案:android monkeyrunner:Timeout while trying to create chimp mananger(device = MonkeyRunner.waitForConnection()一直报错的问题)
  20. mysql分表分库选型

热门文章

  1. Codeforces764C【DFS】
  2. ugui mask失效
  3. Unity Prefabs
  4. Some JPR highlights (JPR 2019 March)
  5. 商务系统的构造思路(无源码!)+如何用jsp实现点击单选框内容显示在另一个jsp页面
  6. 多线程Demo1 了解
  7. 普通组件定义渲染和render渲染组件的区别(三)
  8. hibernate错误总结1
  9. E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))
  10. JavaScript 原生提供两个 Base64 相关的方法