三万字盘点Spring/Boot的那些常用扩展点

打印 上一主题 下一主题

主题 860|帖子 860|积分 2580

大家好,我是三友。
Spring对于每个Java后端程序员来说肯定不陌生,日常开发和面试必备的。本文就来盘点Spring/SpringBoot常见的扩展点,同时也来看看常见的开源框架是如何基于这些扩展点跟Spring/SpringBoot整合的。
话不多说,直接进入正题。
FactoryBean

提起FactoryBean,就有一道“著名”的面试题“说一说FactoryBean和BeanFactory的区别”。其实这两者除了名字有点像,没有半毛钱关系。。
BeanFactory是Bean的工厂,可以帮我们生成想要的Bean,而FactoryBean就是一种Bean的类型。当往容器中注入class类型为FactoryBean的类型的时候,最终生成的Bean是用过FactoryBean的getObject获取的。
来个FactoryBean的Demo

定义一个UserFactoryBean,实现FactoryBean接口,getObject方法返回一个User对象
  1. public class UserFactoryBean implements FactoryBean<User> {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }@Override
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public User getObject() throws Exception {
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }User user = new User();
  49. public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }System.out.println("调用 UserFactoryBean 的 getObject 方法生成 Bean:" + user);
  72. public class SqlSessionFactoryBean
  73.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  74.    
  75.     @Override
  76.     public void onApplicationEvent(ApplicationEvent event) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  81.     }
  82.    
  83. }public class SqlSessionFactoryBean
  84.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  85.    
  86.     @Override
  87.     public void onApplicationEvent(ApplicationEvent event) {
  88. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  92.     }
  93.    
  94. }return user;
  95. public class SqlSessionFactoryBean
  96.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  97.    
  98.     @Override
  99.     public void onApplicationEvent(ApplicationEvent event) {
  100. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  103. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  104.     }
  105.    
  106. }}
  107. public class SqlSessionFactoryBean
  108.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  109.    
  110.     @Override
  111.     public void onApplicationEvent(ApplicationEvent event) {
  112. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  115. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  116.     }
  117.    
  118. }@Override
  119. public class SqlSessionFactoryBean
  120.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  121.    
  122.     @Override
  123.     public void onApplicationEvent(ApplicationEvent event) {
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  126. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  127. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  128.     }
  129.    
  130. }public Class<?> getObjectType() {
  131. public class SqlSessionFactoryBean
  132.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  133.    
  134.     @Override
  135.     public void onApplicationEvent(ApplicationEvent event) {
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  138. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  139. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  140.     }
  141.    
  142. }public class SqlSessionFactoryBean
  143.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  144.    
  145.     @Override
  146.     public void onApplicationEvent(ApplicationEvent event) {
  147. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  148. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  149. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  150. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  151.     }
  152.    
  153. }// 这个 FactoryBean 返回的Bean的类型
  154. public class SqlSessionFactoryBean
  155.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  156.    
  157.     @Override
  158.     public void onApplicationEvent(ApplicationEvent event) {
  159. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  160. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  161. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  162. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  163.     }
  164.    
  165. }public class SqlSessionFactoryBean
  166.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  167.    
  168.     @Override
  169.     public void onApplicationEvent(ApplicationEvent event) {
  170. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  171. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  172. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  173. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  174.     }
  175.    
  176. }return User.class;
  177. public class SqlSessionFactoryBean
  178.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  179.    
  180.     @Override
  181.     public void onApplicationEvent(ApplicationEvent event) {
  182. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  183. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  184. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  185. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  186.     }
  187.    
  188. }}
  189. }
复制代码
  
测试类:
  1. public class Application {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }public static void main(String[] args) {
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public class SqlSessionFactoryBean
  26.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  27.    
  28.     @Override
  29.     public void onApplicationEvent(ApplicationEvent event) {
  30. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  34.     }
  35.    
  36. }AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
  37. public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }public class SqlSessionFactoryBean
  49.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  50.    
  51.     @Override
  52.     public void onApplicationEvent(ApplicationEvent event) {
  53. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  57.     }
  58.    
  59. }//将 UserFactoryBean 注册到容器中
  60. public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }public class SqlSessionFactoryBean
  72.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  73.    
  74.     @Override
  75.     public void onApplicationEvent(ApplicationEvent event) {
  76. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  80.     }
  81.    
  82. }applicationContext.register(UserFactoryBean.class);
  83. public class SqlSessionFactoryBean
  84.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  85.    
  86.     @Override
  87.     public void onApplicationEvent(ApplicationEvent event) {
  88. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  92.     }
  93.    
  94. }public class SqlSessionFactoryBean
  95.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  96.    
  97.     @Override
  98.     public void onApplicationEvent(ApplicationEvent event) {
  99. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  100. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  103.     }
  104.    
  105. }applicationContext.refresh();
  106. public class SqlSessionFactoryBean
  107.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  108.    
  109.     @Override
  110.     public void onApplicationEvent(ApplicationEvent event) {
  111. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  112. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  115.     }
  116.    
  117. }public class SqlSessionFactoryBean
  118.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  119.    
  120.     @Override
  121.     public void onApplicationEvent(ApplicationEvent event) {
  122. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  123. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  126.     }
  127.    
  128. }System.out.println("获取到的Bean为" + applicationContext.getBean(User.class));
  129. public class SqlSessionFactoryBean
  130.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  131.    
  132.     @Override
  133.     public void onApplicationEvent(ApplicationEvent event) {
  134. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  135. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  138.     }
  139.    
  140. }}
  141. }
复制代码
  
结果:
  1. 调用 UserFactoryBean 的 getObject 方法生成 Bean:com.sanyou.spring.extension.User@396e2f39
  2. 获取到的Bean为com.sanyou.spring.extension.User@396e2f39
复制代码
从结果可以看出,明明注册到Spring容器的是UserFactoryBean,但是却能从容器中获取到User类型的Bean,User这个Bean就是通过UserFactoryBean的getObject方法返回的。
FactoryBean在开源框架中的使用

1、 在Mybatis中的使用

Mybatis在整合Spring的时候,就是通过FactoryBean来实现的,这也就是为什么在Spring的Bean中可以注入Mybatis的Mapper接口的动态代理对象的原因。
代码如下,省略了不重要的代码。
  1. public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {
  2.   
  3.   // mapper的接口类型
  4.   private Class<T> mapperInterface;
  5.   @Override
  6.   public T getObject() throws Exception {
  7. public class SqlSessionFactoryBean
  8.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  9.    
  10.     @Override
  11.     public void onApplicationEvent(ApplicationEvent event) {
  12. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  13. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  14. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  15. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  16.     }
  17.    
  18. }// 通过SqlSession获取接口的动态搭理对象
  19. public class SqlSessionFactoryBean
  20.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  21.    
  22.     @Override
  23.     public void onApplicationEvent(ApplicationEvent event) {
  24. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  25. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  26. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  27. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  28.     }
  29.    
  30. }return getSqlSession().getMapper(this.mapperInterface);
  31.   }
  32.   
  33.   @Override
  34.   public Class<T> getObjectType() {
  35. public class SqlSessionFactoryBean
  36.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  37.    
  38.     @Override
  39.     public void onApplicationEvent(ApplicationEvent event) {
  40. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  41. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  44.     }
  45.    
  46. }return this.mapperInterface;
  47.   }
  48.   
  49. }
复制代码
getObject方法的实现就是返回通过SqlSession获取到的Mapper接口的动态代理对象。
而@MapperScan注解的作用就是将每个接口对应的MapperFactoryBean注册到Spring容器的。
2、在OpenFeign中的使用

FeignClient接口的动态代理也是通过FactoryBean注入到Spring中的。
  1. class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean, ApplicationContextAware {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }// FeignClient接口类型
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }private Class<?> type;
  38. public class SqlSessionFactoryBean
  39.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  40.    
  41.     @Override
  42.     public void onApplicationEvent(ApplicationEvent event) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  46. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  47.     }
  48.    
  49. }
  50. public class SqlSessionFactoryBean
  51.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  52.    
  53.     @Override
  54.     public void onApplicationEvent(ApplicationEvent event) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  58. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  59.     }
  60.    
  61. }@Override
  62.    public Object getObject() throws Exception {
  63. public class SqlSessionFactoryBean
  64.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  65.    
  66.     @Override
  67.     public void onApplicationEvent(ApplicationEvent event) {
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  69. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  70. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  71. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  72.     }
  73.    
  74. }  return getTarget();
  75.    }
  76. public class SqlSessionFactoryBean
  77.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  78.    
  79.     @Override
  80.     public void onApplicationEvent(ApplicationEvent event) {
  81. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  82. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  83. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  84. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  85.     }
  86.    
  87. }
  88. public class SqlSessionFactoryBean
  89.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  90.    
  91.     @Override
  92.     public void onApplicationEvent(ApplicationEvent event) {
  93. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  94. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  95. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  96. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  97.     }
  98.    
  99. }@Override
  100.    public Class<?> getObjectType() {
  101. public class SqlSessionFactoryBean
  102.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  103.    
  104.     @Override
  105.     public void onApplicationEvent(ApplicationEvent event) {
  106. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  107. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  108. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  109. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  110.     }
  111.    
  112. }  return type;
  113.    }
  114. }
复制代码
getObject方法是调用getTarget方法来返回的动态代理。
@EnableFeignClients注解的作用就是将每个接口对应的FeignClientFactoryBean注入到Spring容器的。
一般来说,FactoryBean 比较适合那种复杂Bean的构建,在其他框架整合Spring的时候用的比较多。
@Import注解

@Import注解在项目中可能不常见,但是下面这两个注解肯定常见。
  1. @Import({SchedulingConfiguration.class})
  2. public @interface EnableScheduling {
  3. }
  4. @Import({AsyncConfigurationSelector.class})
  5. public @interface EnableAsync {
  6. public class SqlSessionFactoryBean
  7.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  8.    
  9.     @Override
  10.     public void onApplicationEvent(ApplicationEvent event) {
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  12. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  13. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  14. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  15.     }
  16.    
  17. }//忽略
  18. }
复制代码
@EnableScheduling和@EnableAsync两个注解,一个是开启定时任务,一个是开启异步执行。通过这两个注解可以看出,他们都使用了@Import注解,所以真正起作用的是@Import注解。并且在很多情况下,@EnbaleXXX这种格式的注解,都是通过@Import注解起作用的,代表开启了某个功能。
@Import注解导入的配置类的分类

@Import注解导入的配置类可以分为三种情况:
第一种:配置类实现了 ImportSelector 接口
  1. public interface ImportSelector {
  2.    String[] selectImports(AnnotationMetadata importingClassMetadata);
  3.    @Nullable
  4.    default Predicate<String> getExclusionFilter() {
  5. public class SqlSessionFactoryBean
  6.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  7.    
  8.     @Override
  9.     public void onApplicationEvent(ApplicationEvent event) {
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  12. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  13. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  14.     }
  15.    
  16. }  return null;
  17.    }
  18. }
复制代码
  
当配置类实现了 ImportSelector 接口的时候,就会调用 selectImports 方法的实现,获取一批类的全限定名,最终这些类就会被注册到Spring容器中。
UserImportSelector实现了ImportSelector,selectImports方法返回User的全限定名,代表吧User这个类注册容器中
  1. public class UserImportSelector implements ImportSelector {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }@Override
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public String[] selectImports(AnnotationMetadata importingClassMetadata) {
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }System.out.println("调用 UserImportSelector 的 selectImports 方法获取一批类限定名");
  49. public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }return new String[]{"com.sanyou.spring.extension.User"};
  72. public class SqlSessionFactoryBean
  73.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  74.    
  75.     @Override
  76.     public void onApplicationEvent(ApplicationEvent event) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  81.     }
  82.    
  83. }}
  84. }
复制代码
  
测试:
  1. // @Import 注解导入 UserImportSelector
  2. @Import(UserImportSelector.class)
  3. public class Application {
  4. public class SqlSessionFactoryBean
  5.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  6.    
  7.     @Override
  8.     public void onApplicationEvent(ApplicationEvent event) {
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  12. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  13.     }
  14.    
  15. }public static void main(String[] args) {
  16. public class SqlSessionFactoryBean
  17.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  18.    
  19.     @Override
  20.     public void onApplicationEvent(ApplicationEvent event) {
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  23. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  24. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  25.     }
  26.    
  27. }public class SqlSessionFactoryBean
  28.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  29.    
  30.     @Override
  31.     public void onApplicationEvent(ApplicationEvent event) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  35. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  36.     }
  37.    
  38. }AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
  39. public class SqlSessionFactoryBean
  40.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  41.    
  42.     @Override
  43.     public void onApplicationEvent(ApplicationEvent event) {
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  46. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  47. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  48.     }
  49.    
  50. }public class SqlSessionFactoryBean
  51.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  52.    
  53.     @Override
  54.     public void onApplicationEvent(ApplicationEvent event) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  58. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  59.     }
  60.    
  61. }//将 Application 注册到容器中
  62. public class SqlSessionFactoryBean
  63.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  64.    
  65.     @Override
  66.     public void onApplicationEvent(ApplicationEvent event) {
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  69. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  70. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  71.     }
  72.    
  73. }public class SqlSessionFactoryBean
  74.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  75.    
  76.     @Override
  77.     public void onApplicationEvent(ApplicationEvent event) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  81. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  82.     }
  83.    
  84. }applicationContext.register(Application.class);
  85. public class SqlSessionFactoryBean
  86.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  87.    
  88.     @Override
  89.     public void onApplicationEvent(ApplicationEvent event) {
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  92. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  93. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  94.     }
  95.    
  96. }public class SqlSessionFactoryBean
  97.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  98.    
  99.     @Override
  100.     public void onApplicationEvent(ApplicationEvent event) {
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  103. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  104. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  105.     }
  106.    
  107. }applicationContext.refresh();
  108. public class SqlSessionFactoryBean
  109.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  110.    
  111.     @Override
  112.     public void onApplicationEvent(ApplicationEvent event) {
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  115. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  116. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  117.     }
  118.    
  119. }public class SqlSessionFactoryBean
  120.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  121.    
  122.     @Override
  123.     public void onApplicationEvent(ApplicationEvent event) {
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  126. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  127. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  128.     }
  129.    
  130. }System.out.println("获取到的Bean为" + applicationContext.getBean(User.class));
  131. public class SqlSessionFactoryBean
  132.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  133.    
  134.     @Override
  135.     public void onApplicationEvent(ApplicationEvent event) {
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  138. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  139. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  140.     }
  141.    
  142. }}
  143. }
复制代码
结果:
  1. 调用 UserImportSelector 的 selectImports 方法获取一批类限定名
  2. 获取到的Bean为com.sanyou.spring.extension.User@282003e1
复制代码
所以可以看出,的确成功往容器中注入了User这个Bean
第二种:配置类实现了 ImportBeanDefinitionRegistrar 接口
  1. public interface ImportBeanDefinitionRegistrar {
  2.    default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,BeanNameGenerator importBeanNameGenerator) {
  3. public class SqlSessionFactoryBean
  4.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  5.    
  6.     @Override
  7.     public void onApplicationEvent(ApplicationEvent event) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  12.     }
  13.    
  14. }   registerBeanDefinitions(importingClassMetadata, registry);
  15.    }
  16.    default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
  17.    }
  18. }
复制代码
当配置类实现了 ImportBeanDefinitionRegistrar 接口,你就可以自定义往容器中注册想注入的Bean。这个接口相比与 ImportSelector 接口的主要区别就是,ImportSelector接口是返回一个类,你不能对这个类进行任何操作,但是 ImportBeanDefinitionRegistrar 是可以自己注入 BeanDefinition,可以添加属性之类的。
来个demo:
实现ImportBeanDefinitionRegistrar接口
  1. public class UserImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }@Override
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }//构建一个 BeanDefinition , Bean的类型为 User
  49. public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class)
  72. public class SqlSessionFactoryBean
  73.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  74.    
  75.     @Override
  76.     public void onApplicationEvent(ApplicationEvent event) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  81.     }
  82.    
  83. }public class SqlSessionFactoryBean
  84.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  85.    
  86.     @Override
  87.     public void onApplicationEvent(ApplicationEvent event) {
  88. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  92.     }
  93.    
  94. }public class SqlSessionFactoryBean
  95.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  96.    
  97.     @Override
  98.     public void onApplicationEvent(ApplicationEvent event) {
  99. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  100. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  103.     }
  104.    
  105. }public class SqlSessionFactoryBean
  106.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  107.    
  108.     @Override
  109.     public void onApplicationEvent(ApplicationEvent event) {
  110. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  111. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  112. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  114.     }
  115.    
  116. }// 设置 User 这个Bean的属性username的值为三友的java日记
  117. public class SqlSessionFactoryBean
  118.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  119.    
  120.     @Override
  121.     public void onApplicationEvent(ApplicationEvent event) {
  122. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  123. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  126.     }
  127.    
  128. }public class SqlSessionFactoryBean
  129.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  130.    
  131.     @Override
  132.     public void onApplicationEvent(ApplicationEvent event) {
  133. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  134. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  135. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  137.     }
  138.    
  139. }public class SqlSessionFactoryBean
  140.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  141.    
  142.     @Override
  143.     public void onApplicationEvent(ApplicationEvent event) {
  144. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  145. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  146. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  147. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  148.     }
  149.    
  150. }public class SqlSessionFactoryBean
  151.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  152.    
  153.     @Override
  154.     public void onApplicationEvent(ApplicationEvent event) {
  155. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  156. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  157. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  158. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  159.     }
  160.    
  161. }.addPropertyValue("username", "三友的java日记")
  162. public class SqlSessionFactoryBean
  163.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  164.    
  165.     @Override
  166.     public void onApplicationEvent(ApplicationEvent event) {
  167. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  168. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  169. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  170. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  171.     }
  172.    
  173. }public class SqlSessionFactoryBean
  174.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  175.    
  176.     @Override
  177.     public void onApplicationEvent(ApplicationEvent event) {
  178. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  179. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  180. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  181. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  182.     }
  183.    
  184. }public class SqlSessionFactoryBean
  185.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  186.    
  187.     @Override
  188.     public void onApplicationEvent(ApplicationEvent event) {
  189. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  190. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  191. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  192. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  193.     }
  194.    
  195. }public class SqlSessionFactoryBean
  196.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  197.    
  198.     @Override
  199.     public void onApplicationEvent(ApplicationEvent event) {
  200. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  201. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  202. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  203. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  204.     }
  205.    
  206. }.getBeanDefinition();
  207. public class SqlSessionFactoryBean
  208.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  209.    
  210.     @Override
  211.     public void onApplicationEvent(ApplicationEvent event) {
  212. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  213. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  214. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  215. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  216.     }
  217.    
  218. }public class SqlSessionFactoryBean
  219.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  220.    
  221.     @Override
  222.     public void onApplicationEvent(ApplicationEvent event) {
  223. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  224. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  225. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  226. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  227.     }
  228.    
  229. }System.out.println("往Spring容器中注入User");
  230. public class SqlSessionFactoryBean
  231.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  232.    
  233.     @Override
  234.     public void onApplicationEvent(ApplicationEvent event) {
  235. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  236. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  237. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  238. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  239.     }
  240.    
  241. }public class SqlSessionFactoryBean
  242.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  243.    
  244.     @Override
  245.     public void onApplicationEvent(ApplicationEvent event) {
  246. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  247. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  248. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  249. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  250.     }
  251.    
  252. }//把 User 这个Bean的定义注册到容器中
  253. public class SqlSessionFactoryBean
  254.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  255.    
  256.     @Override
  257.     public void onApplicationEvent(ApplicationEvent event) {
  258. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  259. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  260. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  261. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  262.     }
  263.    
  264. }public class SqlSessionFactoryBean
  265.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  266.    
  267.     @Override
  268.     public void onApplicationEvent(ApplicationEvent event) {
  269. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  270. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  271. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  272. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  273.     }
  274.    
  275. }registry.registerBeanDefinition("user", beanDefinition);
  276. public class SqlSessionFactoryBean
  277.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  278.    
  279.     @Override
  280.     public void onApplicationEvent(ApplicationEvent event) {
  281. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  282. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  283. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  284. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  285.     }
  286.    
  287. }}
  288. }
复制代码
测试:
  1. // 导入 UserImportBeanDefinitionRegistrar
  2. @Import(UserImportBeanDefinitionRegistrar.class)
  3. public class Application {
  4. public class SqlSessionFactoryBean
  5.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  6.    
  7.     @Override
  8.     public void onApplicationEvent(ApplicationEvent event) {
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  12. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  13.     }
  14.    
  15. }public static void main(String[] args) {
  16. public class SqlSessionFactoryBean
  17.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  18.    
  19.     @Override
  20.     public void onApplicationEvent(ApplicationEvent event) {
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  23. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  24. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  25.     }
  26.    
  27. }public class SqlSessionFactoryBean
  28.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  29.    
  30.     @Override
  31.     public void onApplicationEvent(ApplicationEvent event) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  35. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  36.     }
  37.    
  38. }AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
  39. public class SqlSessionFactoryBean
  40.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  41.    
  42.     @Override
  43.     public void onApplicationEvent(ApplicationEvent event) {
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  46. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  47. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  48.     }
  49.    
  50. }public class SqlSessionFactoryBean
  51.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  52.    
  53.     @Override
  54.     public void onApplicationEvent(ApplicationEvent event) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  58. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  59.     }
  60.    
  61. }//将 Application 注册到容器中
  62. public class SqlSessionFactoryBean
  63.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  64.    
  65.     @Override
  66.     public void onApplicationEvent(ApplicationEvent event) {
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  69. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  70. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  71.     }
  72.    
  73. }public class SqlSessionFactoryBean
  74.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  75.    
  76.     @Override
  77.     public void onApplicationEvent(ApplicationEvent event) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  81. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  82.     }
  83.    
  84. }applicationContext.register(Application.class);
  85. public class SqlSessionFactoryBean
  86.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  87.    
  88.     @Override
  89.     public void onApplicationEvent(ApplicationEvent event) {
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  92. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  93. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  94.     }
  95.    
  96. }public class SqlSessionFactoryBean
  97.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  98.    
  99.     @Override
  100.     public void onApplicationEvent(ApplicationEvent event) {
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  103. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  104. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  105.     }
  106.    
  107. }applicationContext.refresh();
  108. public class SqlSessionFactoryBean
  109.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  110.    
  111.     @Override
  112.     public void onApplicationEvent(ApplicationEvent event) {
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  115. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  116. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  117.     }
  118.    
  119. }public class SqlSessionFactoryBean
  120.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  121.    
  122.     @Override
  123.     public void onApplicationEvent(ApplicationEvent event) {
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  126. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  127. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  128.     }
  129.    
  130. }User user = applicationContext.getBean(User.class);
  131. public class SqlSessionFactoryBean
  132.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  133.    
  134.     @Override
  135.     public void onApplicationEvent(ApplicationEvent event) {
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  138. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  139. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  140.     }
  141.    
  142. }public class SqlSessionFactoryBean
  143.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  144.    
  145.     @Override
  146.     public void onApplicationEvent(ApplicationEvent event) {
  147. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  148. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  149. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  150. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  151.     }
  152.    
  153. }System.out.println("获取到的Bean为" + user + ",属性username值为:" + user.getUsername());
  154. public class SqlSessionFactoryBean
  155.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  156.    
  157.     @Override
  158.     public void onApplicationEvent(ApplicationEvent event) {
  159. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  160. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  161. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  162. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  163.     }
  164.    
  165. }}
  166. }
复制代码
结果:
  1. 往Spring容器中注入User
  2. 获取到的Bean为com.sanyou.spring.extension.User@6385cb26,属性username值为:三友的java日记
复制代码
第三种:配置类什么接口都没实现

这种就不演示了,就是一个普普通通的类。
总结

@Import注解作用示意图其实不论是什么样的配置类,主要的作用就是往Spring容器中注册Bean,只不过注入的方式不同罢了。
这种方式有什么好处呢?
ImportSelector和ImportBeanDefinitionRegistrar的方法是有入参的,也就是注解的一些属性的封装,所以就可以根据注解的属性的配置,来决定应该返回样的配置类或者是应该往容器中注入什么样的类型的Bean,可以看一下 @EnableAsync 的实现,看看是如何根据@EnableAsync注解的属性来决定往容器中注入什么样的Bean。
@Import的核心作用就是导入配置类,并且还可以根据配合(比如@EnableXXX)使用的注解的属性来决定应该往Spring中注入什么样的Bean。
Bean的生命周期

第一节讲的FactoryBean是一种特殊的Bean的类型,@Import注解是往Spring容器中注册Bean。其实不论是@Import注解,还是@Component、@Bean等注解,又或是xml配置,甚至是demo中的register方法,其实主要都是做了一件事,那就是往Spring容器去注册Bean。
Bean注册示意图为什么需要去注册Bean?
当然是为了让Spring知道要为我们生成Bean,并且需要按照我的要求来生成Bean,比如说,我要@Autowired一个对象,那么你在创建Bean的过程中,就得给我@Autowired一个对象,这就是一个IOC的过程。所以这就涉及了Bean的创建,销毁的过程,也就是面试常问的Bean的生命周期。我之前写过两篇文章,来剖析Bean的生命周期的源码,有需要的小伙伴可以看一下关注微信公众号 三友的java日记 回复 Bean 即可获取。
本节来着重看一下,一个Bean在创建的过程中,有哪些常见的操作Spring在Bean的创建过程中给我们完成,并且操作的顺序是什么样的。
话不多说,直接测试,基于结果来分析。
Bean生命周期的回调

先来测试

创建LifeCycle类

创建了一个LifeCycle,实现了 InitializingBean、ApplicationContextAware、DisposableBean接口,加了@PostConstruct、@PreDestroy注解,注入了一个User对象。
  1. public class LifeCycle implements InitializingBean, ApplicationContextAware, DisposableBean {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }@Autowired
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }private User user;
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public LifeCycle() {
  38. public class SqlSessionFactoryBean
  39.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  40.    
  41.     @Override
  42.     public void onApplicationEvent(ApplicationEvent event) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  46. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  47.     }
  48.    
  49. }public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }System.out.println("LifeCycle对象被创建了");
  61. public class SqlSessionFactoryBean
  62.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  63.    
  64.     @Override
  65.     public void onApplicationEvent(ApplicationEvent event) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  69. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  70.     }
  71.    
  72. }}
  73. public class SqlSessionFactoryBean
  74.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  75.    
  76.     @Override
  77.     public void onApplicationEvent(ApplicationEvent event) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  81. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  82.     }
  83.    
  84. }/**
  85. public class SqlSessionFactoryBean
  86.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  87.    
  88.     @Override
  89.     public void onApplicationEvent(ApplicationEvent event) {
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  92. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  93. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  94.     }
  95.    
  96. } * 实现的 Aware 回调接口
  97. public class SqlSessionFactoryBean
  98.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  99.    
  100.     @Override
  101.     public void onApplicationEvent(ApplicationEvent event) {
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  103. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  104. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  105. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  106.     }
  107.    
  108. } *
  109. public class SqlSessionFactoryBean
  110.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  111.    
  112.     @Override
  113.     public void onApplicationEvent(ApplicationEvent event) {
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  115. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  116. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  117. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  118.     }
  119.    
  120. } * @param applicationContext
  121. public class SqlSessionFactoryBean
  122.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  123.    
  124.     @Override
  125.     public void onApplicationEvent(ApplicationEvent event) {
  126. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  127. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  128. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  129. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  130.     }
  131.    
  132. } * @throws BeansException
  133. public class SqlSessionFactoryBean
  134.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  135.    
  136.     @Override
  137.     public void onApplicationEvent(ApplicationEvent event) {
  138. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  139. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  140. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  141. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  142.     }
  143.    
  144. } */
  145. public class SqlSessionFactoryBean
  146.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  147.    
  148.     @Override
  149.     public void onApplicationEvent(ApplicationEvent event) {
  150. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  151. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  152. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  153. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  154.     }
  155.    
  156. }@Override
  157. public class SqlSessionFactoryBean
  158.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  159.    
  160.     @Override
  161.     public void onApplicationEvent(ApplicationEvent event) {
  162. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  163. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  164. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  165. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  166.     }
  167.    
  168. }public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  169. public class SqlSessionFactoryBean
  170.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  171.    
  172.     @Override
  173.     public void onApplicationEvent(ApplicationEvent event) {
  174. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  175. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  176. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  177. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  178.     }
  179.    
  180. }public class SqlSessionFactoryBean
  181.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  182.    
  183.     @Override
  184.     public void onApplicationEvent(ApplicationEvent event) {
  185. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  186. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  187. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  188. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  189.     }
  190.    
  191. }System.out.println("Aware接口起作用,setApplicationContext被调用了,此时user=" + user);
  192. public class SqlSessionFactoryBean
  193.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  194.    
  195.     @Override
  196.     public void onApplicationEvent(ApplicationEvent event) {
  197. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  198. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  199. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  200. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  201.     }
  202.    
  203. }}
  204. public class SqlSessionFactoryBean
  205.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  206.    
  207.     @Override
  208.     public void onApplicationEvent(ApplicationEvent event) {
  209. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  210. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  211. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  212. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  213.     }
  214.    
  215. }@PostConstruct
  216. public class SqlSessionFactoryBean
  217.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  218.    
  219.     @Override
  220.     public void onApplicationEvent(ApplicationEvent event) {
  221. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  222. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  223. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  224. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  225.     }
  226.    
  227. }public void postConstruct() {
  228. public class SqlSessionFactoryBean
  229.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  230.    
  231.     @Override
  232.     public void onApplicationEvent(ApplicationEvent event) {
  233. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  234. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  235. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  236. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  237.     }
  238.    
  239. }public class SqlSessionFactoryBean
  240.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  241.    
  242.     @Override
  243.     public void onApplicationEvent(ApplicationEvent event) {
  244. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  245. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  246. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  247. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  248.     }
  249.    
  250. }System.out.println("@PostConstruct注解起作用,postConstruct方法被调用了");
  251. public class SqlSessionFactoryBean
  252.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  253.    
  254.     @Override
  255.     public void onApplicationEvent(ApplicationEvent event) {
  256. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  257. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  258. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  259. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  260.     }
  261.    
  262. }}
  263. public class SqlSessionFactoryBean
  264.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  265.    
  266.     @Override
  267.     public void onApplicationEvent(ApplicationEvent event) {
  268. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  269. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  270. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  271. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  272.     }
  273.    
  274. }/**
  275. public class SqlSessionFactoryBean
  276.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  277.    
  278.     @Override
  279.     public void onApplicationEvent(ApplicationEvent event) {
  280. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  281. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  282. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  283. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  284.     }
  285.    
  286. } * 实现 InitializingBean 接口
  287. public class SqlSessionFactoryBean
  288.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  289.    
  290.     @Override
  291.     public void onApplicationEvent(ApplicationEvent event) {
  292. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  293. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  294. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  295. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  296.     }
  297.    
  298. } *
  299. public class SqlSessionFactoryBean
  300.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  301.    
  302.     @Override
  303.     public void onApplicationEvent(ApplicationEvent event) {
  304. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  305. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  306. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  307. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  308.     }
  309.    
  310. } * @throws Exception
  311. public class SqlSessionFactoryBean
  312.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  313.    
  314.     @Override
  315.     public void onApplicationEvent(ApplicationEvent event) {
  316. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  317. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  318. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  319. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  320.     }
  321.    
  322. } */
  323. public class SqlSessionFactoryBean
  324.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  325.    
  326.     @Override
  327.     public void onApplicationEvent(ApplicationEvent event) {
  328. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  329. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  330. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  331. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  332.     }
  333.    
  334. }@Override
  335. public class SqlSessionFactoryBean
  336.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  337.    
  338.     @Override
  339.     public void onApplicationEvent(ApplicationEvent event) {
  340. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  341. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  342. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  343. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  344.     }
  345.    
  346. }public void afterPropertiesSet() throws Exception {
  347. public class SqlSessionFactoryBean
  348.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  349.    
  350.     @Override
  351.     public void onApplicationEvent(ApplicationEvent event) {
  352. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  353. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  354. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  355. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  356.     }
  357.    
  358. }public class SqlSessionFactoryBean
  359.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  360.    
  361.     @Override
  362.     public void onApplicationEvent(ApplicationEvent event) {
  363. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  364. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  365. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  366. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  367.     }
  368.    
  369. }System.out.println("InitializingBean接口起作用,afterPropertiesSet方法被调用了");
  370. public class SqlSessionFactoryBean
  371.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  372.    
  373.     @Override
  374.     public void onApplicationEvent(ApplicationEvent event) {
  375. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  376. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  377. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  378. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  379.     }
  380.    
  381. }}
  382. public class SqlSessionFactoryBean
  383.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  384.    
  385.     @Override
  386.     public void onApplicationEvent(ApplicationEvent event) {
  387. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  388. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  389. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  390. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  391.     }
  392.    
  393. }/**
  394. public class SqlSessionFactoryBean
  395.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  396.    
  397.     @Override
  398.     public void onApplicationEvent(ApplicationEvent event) {
  399. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  400. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  401. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  402. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  403.     }
  404.    
  405. } * 通过 {@link Bean#initMethod()}来指定
  406. public class SqlSessionFactoryBean
  407.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  408.    
  409.     @Override
  410.     public void onApplicationEvent(ApplicationEvent event) {
  411. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  412. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  413. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  414. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  415.     }
  416.    
  417. } *
  418. public class SqlSessionFactoryBean
  419.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  420.    
  421.     @Override
  422.     public void onApplicationEvent(ApplicationEvent event) {
  423. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  424. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  425. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  426. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  427.     }
  428.    
  429. } * @throws Exception
  430. public class SqlSessionFactoryBean
  431.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  432.    
  433.     @Override
  434.     public void onApplicationEvent(ApplicationEvent event) {
  435. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  436. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  437. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  438. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  439.     }
  440.    
  441. } */
  442. public class SqlSessionFactoryBean
  443.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  444.    
  445.     @Override
  446.     public void onApplicationEvent(ApplicationEvent event) {
  447. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  448. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  449. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  450. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  451.     }
  452.    
  453. }public void initMethod() throws Exception {
  454. public class SqlSessionFactoryBean
  455.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  456.    
  457.     @Override
  458.     public void onApplicationEvent(ApplicationEvent event) {
  459. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  460. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  461. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  462. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  463.     }
  464.    
  465. }public class SqlSessionFactoryBean
  466.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  467.    
  468.     @Override
  469.     public void onApplicationEvent(ApplicationEvent event) {
  470. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  471. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  472. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  473. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  474.     }
  475.    
  476. }System.out.println("@Bean#initMethod()起作用,initMethod方法被调用了");
  477. public class SqlSessionFactoryBean
  478.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  479.    
  480.     @Override
  481.     public void onApplicationEvent(ApplicationEvent event) {
  482. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  483. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  484. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  485. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  486.     }
  487.    
  488. }}
  489. public class SqlSessionFactoryBean
  490.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  491.    
  492.     @Override
  493.     public void onApplicationEvent(ApplicationEvent event) {
  494. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  495. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  496. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  497. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  498.     }
  499.    
  500. }@PreDestroy
  501. public class SqlSessionFactoryBean
  502.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  503.    
  504.     @Override
  505.     public void onApplicationEvent(ApplicationEvent event) {
  506. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  507. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  508. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  509. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  510.     }
  511.    
  512. }public void preDestroy() throws Exception {
  513. public class SqlSessionFactoryBean
  514.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  515.    
  516.     @Override
  517.     public void onApplicationEvent(ApplicationEvent event) {
  518. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  519. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  520. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  521. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  522.     }
  523.    
  524. }public class SqlSessionFactoryBean
  525.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  526.    
  527.     @Override
  528.     public void onApplicationEvent(ApplicationEvent event) {
  529. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  530. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  531. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  532. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  533.     }
  534.    
  535. }System.out.println("@PreDestroy注解起作用,preDestroy方法被调用了");
  536. public class SqlSessionFactoryBean
  537.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  538.    
  539.     @Override
  540.     public void onApplicationEvent(ApplicationEvent event) {
  541. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  542. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  543. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  544. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  545.     }
  546.    
  547. }}
  548. public class SqlSessionFactoryBean
  549.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  550.    
  551.     @Override
  552.     public void onApplicationEvent(ApplicationEvent event) {
  553. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  554. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  555. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  556. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  557.     }
  558.    
  559. }/**
  560. public class SqlSessionFactoryBean
  561.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  562.    
  563.     @Override
  564.     public void onApplicationEvent(ApplicationEvent event) {
  565. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  566. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  567. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  568. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  569.     }
  570.    
  571. } * 通过 {@link Bean#destroyMethod()}来指定
  572. public class SqlSessionFactoryBean
  573.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  574.    
  575.     @Override
  576.     public void onApplicationEvent(ApplicationEvent event) {
  577. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  578. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  579. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  580. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  581.     }
  582.    
  583. } *
  584. public class SqlSessionFactoryBean
  585.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  586.    
  587.     @Override
  588.     public void onApplicationEvent(ApplicationEvent event) {
  589. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  590. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  591. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  592. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  593.     }
  594.    
  595. } * @throws Exception
  596. public class SqlSessionFactoryBean
  597.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  598.    
  599.     @Override
  600.     public void onApplicationEvent(ApplicationEvent event) {
  601. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  602. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  603. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  604. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  605.     }
  606.    
  607. } */
  608. public class SqlSessionFactoryBean
  609.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  610.    
  611.     @Override
  612.     public void onApplicationEvent(ApplicationEvent event) {
  613. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  614. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  615. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  616. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  617.     }
  618.    
  619. }public void destroyMethod() throws Exception {
  620. public class SqlSessionFactoryBean
  621.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  622.    
  623.     @Override
  624.     public void onApplicationEvent(ApplicationEvent event) {
  625. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  626. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  627. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  628. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  629.     }
  630.    
  631. }public class SqlSessionFactoryBean
  632.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  633.    
  634.     @Override
  635.     public void onApplicationEvent(ApplicationEvent event) {
  636. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  637. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  638. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  639. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  640.     }
  641.    
  642. }System.out.println("@Bean#destroyMethod()起作用,destroyMethod方法被调用了");
  643. public class SqlSessionFactoryBean
  644.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  645.    
  646.     @Override
  647.     public void onApplicationEvent(ApplicationEvent event) {
  648. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  649. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  650. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  651. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  652.     }
  653.    
  654. }}
  655. public class SqlSessionFactoryBean
  656.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  657.    
  658.     @Override
  659.     public void onApplicationEvent(ApplicationEvent event) {
  660. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  661. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  662. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  663. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  664.     }
  665.    
  666. }/**
  667. public class SqlSessionFactoryBean
  668.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  669.    
  670.     @Override
  671.     public void onApplicationEvent(ApplicationEvent event) {
  672. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  673. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  674. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  675. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  676.     }
  677.    
  678. } * 实现 DisposableBean 注解
  679. public class SqlSessionFactoryBean
  680.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  681.    
  682.     @Override
  683.     public void onApplicationEvent(ApplicationEvent event) {
  684. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  685. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  686. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  687. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  688.     }
  689.    
  690. } *
  691. public class SqlSessionFactoryBean
  692.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  693.    
  694.     @Override
  695.     public void onApplicationEvent(ApplicationEvent event) {
  696. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  697. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  698. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  699. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  700.     }
  701.    
  702. } * @throws Exception
  703. public class SqlSessionFactoryBean
  704.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  705.    
  706.     @Override
  707.     public void onApplicationEvent(ApplicationEvent event) {
  708. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  709. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  710. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  711. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  712.     }
  713.    
  714. } */
  715. public class SqlSessionFactoryBean
  716.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  717.    
  718.     @Override
  719.     public void onApplicationEvent(ApplicationEvent event) {
  720. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  721. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  722. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  723. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  724.     }
  725.    
  726. }@Override
  727. public class SqlSessionFactoryBean
  728.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  729.    
  730.     @Override
  731.     public void onApplicationEvent(ApplicationEvent event) {
  732. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  733. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  734. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  735. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  736.     }
  737.    
  738. }public void destroy() throws Exception {
  739. public class SqlSessionFactoryBean
  740.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  741.    
  742.     @Override
  743.     public void onApplicationEvent(ApplicationEvent event) {
  744. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  745. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  746. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  747. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  748.     }
  749.    
  750. }public class SqlSessionFactoryBean
  751.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  752.    
  753.     @Override
  754.     public void onApplicationEvent(ApplicationEvent event) {
  755. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  756. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  757. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  758. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  759.     }
  760.    
  761. }System.out.println("DisposableBean接口起作用,destroy方法被调用了");
  762. public class SqlSessionFactoryBean
  763.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  764.    
  765.     @Override
  766.     public void onApplicationEvent(ApplicationEvent event) {
  767. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  768. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  769. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  770. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  771.     }
  772.    
  773. }}
  774. }
复制代码
声明LifeCycle

通过@Bean声明了LifeCycle,并且initMethod和destroyMethod属性分别指定到了LifeCycle类的initMethod方法和destroyMethod方法
  1. @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
  2. public LifeCycle lifeCycle() {
  3. public class SqlSessionFactoryBean
  4.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  5.    
  6.     @Override
  7.     public void onApplicationEvent(ApplicationEvent event) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  12.     }
  13.    
  14. }return new LifeCycle();
  15. }
复制代码
  
测试
  1. public class Application {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }public static void main(String[] args) {
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public class SqlSessionFactoryBean
  26.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  27.    
  28.     @Override
  29.     public void onApplicationEvent(ApplicationEvent event) {
  30. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  34.     }
  35.    
  36. }AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
  37. public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }public class SqlSessionFactoryBean
  49.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  50.    
  51.     @Override
  52.     public void onApplicationEvent(ApplicationEvent event) {
  53. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  57.     }
  58.    
  59. }//将 LifeCycle 注册到容器中
  60. public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }public class SqlSessionFactoryBean
  72.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  73.    
  74.     @Override
  75.     public void onApplicationEvent(ApplicationEvent event) {
  76. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  80.     }
  81.    
  82. }applicationContext.register(Application.class);
  83. public class SqlSessionFactoryBean
  84.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  85.    
  86.     @Override
  87.     public void onApplicationEvent(ApplicationEvent event) {
  88. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  92.     }
  93.    
  94. }public class SqlSessionFactoryBean
  95.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  96.    
  97.     @Override
  98.     public void onApplicationEvent(ApplicationEvent event) {
  99. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  100. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  103.     }
  104.    
  105. }applicationContext.refresh();
  106. public class SqlSessionFactoryBean
  107.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  108.    
  109.     @Override
  110.     public void onApplicationEvent(ApplicationEvent event) {
  111. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  112. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  115.     }
  116.    
  117. }public class SqlSessionFactoryBean
  118.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  119.    
  120.     @Override
  121.     public void onApplicationEvent(ApplicationEvent event) {
  122. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  123. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  126.     }
  127.    
  128. }// 关闭上下文,触发销毁操作
  129. public class SqlSessionFactoryBean
  130.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  131.    
  132.     @Override
  133.     public void onApplicationEvent(ApplicationEvent event) {
  134. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  135. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  138.     }
  139.    
  140. }public class SqlSessionFactoryBean
  141.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  142.    
  143.     @Override
  144.     public void onApplicationEvent(ApplicationEvent event) {
  145. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  146. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  147. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  148. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  149.     }
  150.    
  151. }applicationContext.close();
  152. public class SqlSessionFactoryBean
  153.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  154.    
  155.     @Override
  156.     public void onApplicationEvent(ApplicationEvent event) {
  157. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  158. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  159. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  160. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  161.     }
  162.    
  163. }}
  164. public class SqlSessionFactoryBean
  165.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  166.    
  167.     @Override
  168.     public void onApplicationEvent(ApplicationEvent event) {
  169. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  170. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  171. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  172. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  173.     }
  174.    
  175. }@Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
  176. public class SqlSessionFactoryBean
  177.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  178.    
  179.     @Override
  180.     public void onApplicationEvent(ApplicationEvent event) {
  181. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  182. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  183. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  184. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  185.     }
  186.    
  187. }public LifeCycle lifeCycle() {
  188. public class SqlSessionFactoryBean
  189.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  190.    
  191.     @Override
  192.     public void onApplicationEvent(ApplicationEvent event) {
  193. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  194. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  195. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  196. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  197.     }
  198.    
  199. }public class SqlSessionFactoryBean
  200.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  201.    
  202.     @Override
  203.     public void onApplicationEvent(ApplicationEvent event) {
  204. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  205. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  206. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  207. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  208.     }
  209.    
  210. }return new LifeCycle();
  211. public class SqlSessionFactoryBean
  212.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  213.    
  214.     @Override
  215.     public void onApplicationEvent(ApplicationEvent event) {
  216. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  217. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  218. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  219. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  220.     }
  221.    
  222. }}
  223. public class SqlSessionFactoryBean
  224.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  225.    
  226.     @Override
  227.     public void onApplicationEvent(ApplicationEvent event) {
  228. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  229. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  230. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  231. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  232.     }
  233.    
  234. }@Bean
  235. public class SqlSessionFactoryBean
  236.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  237.    
  238.     @Override
  239.     public void onApplicationEvent(ApplicationEvent event) {
  240. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  241. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  242. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  243. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  244.     }
  245.    
  246. }public User user() {
  247. public class SqlSessionFactoryBean
  248.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  249.    
  250.     @Override
  251.     public void onApplicationEvent(ApplicationEvent event) {
  252. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  253. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  254. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  255. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  256.     }
  257.    
  258. }public class SqlSessionFactoryBean
  259.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  260.    
  261.     @Override
  262.     public void onApplicationEvent(ApplicationEvent event) {
  263. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  264. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  265. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  266. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  267.     }
  268.    
  269. }return new User();
  270. public class SqlSessionFactoryBean
  271.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  272.    
  273.     @Override
  274.     public void onApplicationEvent(ApplicationEvent event) {
  275. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  276. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  277. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  278. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  279.     }
  280.    
  281. }}
  282. }
复制代码
  
执行结果:
  1. LifeCycle对象被创建了
  2. Aware接口起作用,setApplicationContext被调用了,此时user=com.sanyou.spring.extension.User@57d5872c
  3. @PostConstruct注解起作用,postConstruct方法被调用了
  4. InitializingBean接口起作用,afterPropertiesSet方法被调用了
  5. @Bean#initMethod()起作用,initMethod方法被调用了
  6. @PreDestroy注解起作用,preDestroy方法被调用了
  7. DisposableBean接口起作用,destroy方法被调用了
  8. @Bean#destroyMethod()起作用,destroyMethod方法被调用了
复制代码
分析结果

通过测试的结果可以看出,Bean在创建和销毁的过程当我们实现了某些接口或者加了某些注解,Spring就会回调我们实现的接口或者执行的方法。
同时,在执行setApplicationContext的时候,能打印出User对象,说明User已经被注入了,说明注入发生在setApplicationContext之前。
这里画张图总结一下Bean创建和销毁过程中调用的顺序。
回调顺序红色部分发生在Bean的创建过程,灰色部分发生在Bean销毁的过程中,在容器关闭的时候,就会销毁Bean。
这里说一下图中的Aware接口指的是什么。其余的其实没什么好说的,就是按照这种方式配置,Spring会调用对应的方法而已。
Aware接口是指以Aware结尾的一些Spring提供的接口,当你的Bean实现了这些接口的话,在创建过程中会回调对应的set方法,并传入响应的对象。
这里列举几个Aware接口以及它们的作用
接口作用ApplicationContextAware注入ApplicationContextApplicationEventPublisherAware注入ApplicationEventPublisher事件发布器BeanFactoryAware注入BeanFactoryBeanNameAware注入Bean的名称有了这些回调,比如说我的Bean想拿到ApplicationContext,不仅可以通过@Autowired注入,还可以通过实现ApplicationContextAware接口拿到。
通过上面的例子我们知道了比如说@PostConstruct注解、@Autowired注解、@PreDestroy注解的作用,但是它们是如何在不同的阶段实现的呢?接着往下看。
BeanPostProcessor

BeanPostProcessor,中文名 Bean的后置处理器,在Bean创建的过程中起作用。
BeanPostProcessor是Bean在创建过程中一个非常重要的扩展点,因为每个Bean在创建的各个阶段,都会回调BeanPostProcessor及其子接口的方法,传入正在创建的Bean对象,这样如果想对Bean创建过程中某个阶段进行自定义扩展,那么就可以自定义BeanPostProcessor来完成。
说得简单点,BeanPostProcessor就是在Bean创建过程中留的口子,通过这个口子可以对正在创建的Bean进行扩展。只不过Bean创建的阶段比较多,然后BeanPostProcessor接口以及他的子接口InstantiationAwareBeanPostProcessor、DestructionAwareBeanPostProcessor就提供了很多方法,可以使得在不同的阶段都可以拿到正在创建的Bean进行扩展。
来个Demo

现在需要实现一个这样的需求,如果Bean的类型是User,那么就设置这个对象的username属性为 ”三友的java日记“。
那么就可以这么写:
  1. public class UserBeanPostProcessor implements BeanPostProcessor {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }@Override
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }if (bean instanceof User) {
  49. public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }public class SqlSessionFactoryBean
  72.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  73.    
  74.     @Override
  75.     public void onApplicationEvent(ApplicationEvent event) {
  76. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  80.     }
  81.    
  82. }//如果当前的Bean的类型是 User ,就把这个对象 username 的属性赋值为 三友的java日记
  83. public class SqlSessionFactoryBean
  84.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  85.    
  86.     @Override
  87.     public void onApplicationEvent(ApplicationEvent event) {
  88. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  92.     }
  93.    
  94. }public class SqlSessionFactoryBean
  95.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  96.    
  97.     @Override
  98.     public void onApplicationEvent(ApplicationEvent event) {
  99. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  100. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  103.     }
  104.    
  105. }public class SqlSessionFactoryBean
  106.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  107.    
  108.     @Override
  109.     public void onApplicationEvent(ApplicationEvent event) {
  110. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  111. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  112. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  114.     }
  115.    
  116. }((User) bean).setUsername("三友的java日记");
  117. public class SqlSessionFactoryBean
  118.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  119.    
  120.     @Override
  121.     public void onApplicationEvent(ApplicationEvent event) {
  122. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  123. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  126.     }
  127.    
  128. }public class SqlSessionFactoryBean
  129.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  130.    
  131.     @Override
  132.     public void onApplicationEvent(ApplicationEvent event) {
  133. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  134. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  135. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  137.     }
  138.    
  139. }}
  140. public class SqlSessionFactoryBean
  141.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  142.    
  143.     @Override
  144.     public void onApplicationEvent(ApplicationEvent event) {
  145. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  146. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  147. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  148. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  149.     }
  150.    
  151. }public class SqlSessionFactoryBean
  152.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  153.    
  154.     @Override
  155.     public void onApplicationEvent(ApplicationEvent event) {
  156. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  157. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  158. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  159. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  160.     }
  161.    
  162. }return bean;
  163. public class SqlSessionFactoryBean
  164.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  165.    
  166.     @Override
  167.     public void onApplicationEvent(ApplicationEvent event) {
  168. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  169. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  170. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  171. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  172.     }
  173.    
  174. }}
  175. }
复制代码
测试:
  1. public class Application {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }public static void main(String[] args) {
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public class SqlSessionFactoryBean
  26.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  27.    
  28.     @Override
  29.     public void onApplicationEvent(ApplicationEvent event) {
  30. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  34.     }
  35.    
  36. }AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
  37. public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }public class SqlSessionFactoryBean
  49.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  50.    
  51.     @Override
  52.     public void onApplicationEvent(ApplicationEvent event) {
  53. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  57.     }
  58.    
  59. }//将 UserBeanPostProcessor 和  User 注册到容器中
  60. public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }public class SqlSessionFactoryBean
  72.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  73.    
  74.     @Override
  75.     public void onApplicationEvent(ApplicationEvent event) {
  76. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  80.     }
  81.    
  82. }applicationContext.register(UserBeanPostProcessor.class);
  83. public class SqlSessionFactoryBean
  84.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  85.    
  86.     @Override
  87.     public void onApplicationEvent(ApplicationEvent event) {
  88. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  92.     }
  93.    
  94. }public class SqlSessionFactoryBean
  95.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  96.    
  97.     @Override
  98.     public void onApplicationEvent(ApplicationEvent event) {
  99. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  100. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  103.     }
  104.    
  105. }applicationContext.register(User.class);
  106. public class SqlSessionFactoryBean
  107.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  108.    
  109.     @Override
  110.     public void onApplicationEvent(ApplicationEvent event) {
  111. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  112. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  115.     }
  116.    
  117. }public class SqlSessionFactoryBean
  118.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  119.    
  120.     @Override
  121.     public void onApplicationEvent(ApplicationEvent event) {
  122. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  123. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  126.     }
  127.    
  128. }applicationContext.refresh();
  129. public class SqlSessionFactoryBean
  130.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  131.    
  132.     @Override
  133.     public void onApplicationEvent(ApplicationEvent event) {
  134. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  135. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  138.     }
  139.    
  140. }public class SqlSessionFactoryBean
  141.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  142.    
  143.     @Override
  144.     public void onApplicationEvent(ApplicationEvent event) {
  145. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  146. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  147. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  148. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  149.     }
  150.    
  151. }User user = applicationContext.getBean(User.class);
  152. public class SqlSessionFactoryBean
  153.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  154.    
  155.     @Override
  156.     public void onApplicationEvent(ApplicationEvent event) {
  157. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  158. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  159. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  160. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  161.     }
  162.    
  163. }public class SqlSessionFactoryBean
  164.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  165.    
  166.     @Override
  167.     public void onApplicationEvent(ApplicationEvent event) {
  168. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  169. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  170. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  171. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  172.     }
  173.    
  174. }System.out.println("获取到的Bean为" + user + ",属性username值为:" + user.getUsername());
  175. public class SqlSessionFactoryBean
  176.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  177.    
  178.     @Override
  179.     public void onApplicationEvent(ApplicationEvent event) {
  180. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  181. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  182. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  183. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  184.     }
  185.    
  186. }}
  187. }
复制代码
测试结果:
  1. 获取到的Bean为com.sanyou.spring.extension.User@21a947fe,属性username值为:三友的java日记
复制代码
从结果可以看出,每个生成的Bean在执行到某个阶段的时候,都会回调UserBeanPostProcessor,然后UserBeanPostProcessor就会判断当前创建的Bean的类型,如果是User类型,那么就会将username的属性设置为 ”三友的java日记“。
Spring内置的BeanPostProcessor

这里我列举了常见的一些BeanPostProcessor的实现以及它们的作用
BeanPostProcessor作用AutowiredAnnotationBeanPostProcessor处理@Autowired、@Value注解CommonAnnotationBeanPostProcessor处理@Resource、@PostConstruct、@PreDestroy注解AnnotationAwareAspectJAutoProxyCreator处理一些注解或者是AOP切面的动态代理ApplicationContextAwareProcessor处理Aware接口注入的AsyncAnnotationBeanPostProcessor处理@Async注解ScheduledAnnotationBeanPostProcessor处理@Scheduled注解通过列举的这些BeanPostProcessor的实现可以看出,Spring Bean的很多注解的处理都是依靠BeanPostProcessor及其子类的实现来完成的,这也回答了上一小节的疑问,处理@Autowired、@PostConstruct、@PreDestroy注解是如何起作用的,其实就是通过BeanPostProcessor,在Bean的不同阶段来调用对应的方法起作用的。
BeanPostProcessor在Dubbo中的使用

在Dubbo中可以通过@DubboReference(@Reference)来引用生产者提供的接口,这个注解的处理也是依靠ReferenceAnnotationBeanPostProcessor,也就是 BeanPostProcessor 的扩展来实现的。
  1. public class ReferenceAnnotationBeanPostProcessor
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }   extends AbstractAnnotationBeanPostProcessor
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }   implements ApplicationContextAware, BeanFactoryPostProcessor {
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }// 忽略
  49. }
复制代码
当Bean在创建的某一阶段,走到了ReferenceAnnotationBeanPostProcessor这个类,就会根据反射找出这个类有没有@DubboReference(@Reference)注解,有的话就构建一个动态搭理注入就可以了。
BeanPostProcessor在Spring Bean的扩展中扮演着重要的角色,是Spring Bean生命周期中很重要的一部分。正是因为有了BeanPostProcessor,你就可以在Bean创建过程中的任意一个阶段扩展自己想要的东西。
BeanFactoryPostProcessor

通过上面一节我们知道 BeanPostProcessor 是对Bean的处理,那么BeanFactoryPostProcessor很容易就猜到是对BeanFactory,也就是Spring容器的处理。
举个例子,如果我们想禁止循环依赖,那么就可以这么写。
  1. public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }@Override
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }// 禁止循环依赖
  49. public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }((DefaultListableBeanFactory) beanFactory).setAllowCircularReferences(false);
  72. public class SqlSessionFactoryBean
  73.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  74.    
  75.     @Override
  76.     public void onApplicationEvent(ApplicationEvent event) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  81.     }
  82.    
  83. }}
  84. }
复制代码
后面只需要将注入到Spring容器中就会生效。
BeanFactoryPostProcessor是可以对Spring容器做处理的,方法的入参就是Spring的容器,通过这个接口,就对容器进行为所欲为的操作。
Spring SPI机制

SPI全称为 (Service Provider Interface),是一种动态替换发现的机制,一种解耦非常优秀的思想,SPI可以很灵活的让接口和实现分离, 让api提供者只提供接口,第三方来实现,然后可以使用配置文件的方式来实现替换或者扩展,在框架中比较常见,提高框架的可扩展性。
JDK有内置的SPI机制的实现ServiceLoader,Dubbo也有自己的SPI机制的实现ExtensionLoader,但是这里我们都不讲。。
 但是,我之前写过相关的文章,文章的前半部分有对比三者的区别,有需要的小伙伴可以关注微信公众号 三友的java日记 回复 dubbo spi 即可获得。
这里我们着重讲一下Spring的SPI机制的实现SpringFactoriesLoader。
SpringFactoriesLoader

Spring的SPI机制规定,配置文件必须在classpath路径下的META-INF文件夹内,文件名必须为spring.factories,文件内容为键值对,一个键可以有多个值,只需要用逗号分割就行,同时键值都需要是类的全限定名。但是键和值可以没有任何关系,当然想有也可以有。
show me the code

这里我自定义一个类,MyEnableAutoConfiguration作为键,值就是User
  1. public class MyEnableAutoConfiguration {
  2. }
复制代码
  1. spring.factories文件
复制代码
  1. com.sanyou.spring.extension.spi.MyEnableAutoConfiguration=com.sanyou.spring.extension.User
复制代码
然后放在META-INF底下
测试:
  1. public class Application {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }public static void main(String[] args) {
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public class SqlSessionFactoryBean
  26.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  27.    
  28.     @Override
  29.     public void onApplicationEvent(ApplicationEvent event) {
  30. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  34.     }
  35.    
  36. }List<String> classNames = SpringFactoriesLoader.loadFactoryNames(MyEnableAutoConfiguration.class, MyEnableAutoConfiguration.class.getClassLoader());
  37. public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }public class SqlSessionFactoryBean
  49.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  50.    
  51.     @Override
  52.     public void onApplicationEvent(ApplicationEvent event) {
  53. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  57.     }
  58.    
  59. }classNames.forEach(System.out::println);
  60. public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }}
  72. }
复制代码
结果:
  1. com.sanyou.spring.extension.User
复制代码
可以看出,通过SpringFactoriesLoader的确可以从spring.factories文件中拿到MyEnableAutoConfiguration键对应的值。
到这你可能说会,这SPI机制也没啥用啊。的确,我这个例子比较简单,拿到就是遍历,但是在Spring中,如果Spring在加载类的话使用SPI机制,那我们就可以扩展,接着往下看。
SpringBoot启动扩展点

SpringBoot项目在启动的过程中有很多扩展点,这里就来盘点一下几个常见的扩展点。
1、自动装配

说到SpringBoot的扩展点,第一时间肯定想到的就是自动装配机制,面试贼喜欢问,但是其实就是一个很简单的东西。当项目启动的时候,会去从所有的spring.factories文件中读取@EnableAutoConfiguration键对应的值,拿到配置类,然后根据一些条件判断,决定哪些配置可以使用,哪些不能使用。
spring.factories文件?键值?不错,自动装配说白了就是SPI机制的一种运用场景。
@EnableAutoConfiguration注解:
  1. @Import(AutoConfigurationImportSelector.class)
  2. public @interface EnableAutoConfiguration {
  3. public class SqlSessionFactoryBean
  4.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  5.    
  6.     @Override
  7.     public void onApplicationEvent(ApplicationEvent event) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  12.     }
  13.    
  14. }//忽略
  15. }
复制代码
我擦,这个注解也是使用@Import注解,而且配置类还实现了ImportSelector接口,跟前面也都对上了。在SpringBoot中,@EnableAutoConfiguration是通过@SpringBootApplication来使用的。
在AutoConfigurationImportSelector中还有这样一段代码
 
所以,这段代码也明显地可以看出,自动装配也是基于SPI机制实现的。
那么我想实现自动装配怎么办呢?很简单,只需两步。
第一步,写个配置类:
  1. @Configuration
  2. public class UserAutoConfiguration {
  3. public class SqlSessionFactoryBean
  4.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  5.    
  6.     @Override
  7.     public void onApplicationEvent(ApplicationEvent event) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  12.     }
  13.    
  14. }@Bean
  15. public class SqlSessionFactoryBean
  16.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  17.    
  18.     @Override
  19.     public void onApplicationEvent(ApplicationEvent event) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  23. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  24.     }
  25.    
  26. }public UserFactoryBean userFactoryBean() {
  27. public class SqlSessionFactoryBean
  28.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  29.    
  30.     @Override
  31.     public void onApplicationEvent(ApplicationEvent event) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  35. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  36.     }
  37.    
  38. }public class SqlSessionFactoryBean
  39.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  40.    
  41.     @Override
  42.     public void onApplicationEvent(ApplicationEvent event) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  46. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  47.     }
  48.    
  49. }return new UserFactoryBean();
  50. public class SqlSessionFactoryBean
  51.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  52.    
  53.     @Override
  54.     public void onApplicationEvent(ApplicationEvent event) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  58. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  59.     }
  60.    
  61. }}
  62. }
复制代码
这里我为了跟前面的知识有关联,配置了一个UserFactoryBean。
第二步,往spring.factories文件配置一下
  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. com.sanyou.spring.extension.springbootextension.UserAutoConfiguration
复制代码
到这就已经实现了自动装配的扩展。
接下来进行测试:
  1. @SpringBootApplication
  2. public class Application {
  3. public class SqlSessionFactoryBean
  4.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  5.    
  6.     @Override
  7.     public void onApplicationEvent(ApplicationEvent event) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  12.     }
  13.    
  14. }public static void main(String[] args) {
  15. public class SqlSessionFactoryBean
  16.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  17.    
  18.     @Override
  19.     public void onApplicationEvent(ApplicationEvent event) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  23. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  24.     }
  25.    
  26. }public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class);
  38. public class SqlSessionFactoryBean
  39.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  40.    
  41.     @Override
  42.     public void onApplicationEvent(ApplicationEvent event) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  46. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  47.     }
  48.    
  49. }public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }User user = applicationContext.getBean(User.class);
  61. public class SqlSessionFactoryBean
  62.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  63.    
  64.     @Override
  65.     public void onApplicationEvent(ApplicationEvent event) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  69. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  70.     }
  71.    
  72. }public class SqlSessionFactoryBean
  73.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  74.    
  75.     @Override
  76.     public void onApplicationEvent(ApplicationEvent event) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  81.     }
  82.    
  83. }System.out.println("获取到的Bean为" + user);
  84. public class SqlSessionFactoryBean
  85.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  86.    
  87.     @Override
  88.     public void onApplicationEvent(ApplicationEvent event) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  92. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  93.     }
  94.    
  95. }}
  96. }
复制代码
运行结果:
  1. 调用 UserFactoryBean 的 getObject 方法生成 Bean:com.sanyou.spring.extension.User@3406472c
  2. 获取到的Bean为com.sanyou.spring.extension.User@3406472c
复制代码
从运行结果可以看出,自动装配起了作用,并且虽然往容器中注入的Bean的class类型为UserFactoryBean,但是最终会调用UserFactoryBean的getObject的实现获取到User对象。
自动装配机制是SpringBoot的一个很重要的扩展点,很多框架在整合SpringBoot的时候,也都通过自动装配来的,实现项目启动,框架就自动启动的,这里我举个Mybatis整合SpringBoot。
Mybatis整合SpringBoot的spring.factories文件2、PropertySourceLoader

PropertySourceLoader,这是干啥的呢?

我们都知道,在SpringBoot环境下,外部化的配置文件支持properties和yaml两种格式。但是,现在不想使用properties和yaml格式的文件,想使用json格式的配置文件,怎么办?
当然是基于该小节讲的PropertySourceLoader来实现的。
  1. public interface PropertySourceLoader {
  2.    //可以支持哪种文件格式的解析
  3.    String[] getFileExtensions();
  4.    // 解析配置文件,读出内容,封装成一个PropertySource<?>结合返回回去
  5.    List<PropertySource<?>> load(String name, Resource resource) throws IOException;
  6. }
复制代码
  
第二步:配置PropertySourceLoader

JsonPropertySourceLoader 已经有了,那么怎么用呢?当然是SPI机制了,SpringBoot对于配置文件的处理,就是依靠SPI机制,这也是能扩展的重要原因。
SPI机制加载PropertySourceLoader实现spring.factories文件配置PropertySourceLoaderSpringBoot会先通过SPI机制加载所有PropertySourceLoader,然后遍历每个PropertySourceLoader,判断当前遍历的PropertySourceLoader,通过getFileExtensions获取到当前PropertySourceLoader能够支持哪些配置文件格式的解析,让后跟当前需要解析的文件格式进行匹配,如果能匹配上,那么就会使用当前遍历的PropertySourceLoader来解析配置文件。
PropertySourceLoader其实就属于策略接口,配置文件的解析就是策略模式的运用。
所以,只需要按照这种格式,在spring.factories文件中配置一下就行了。
  1. public class JsonPropertySourceLoader implements PropertySourceLoader {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }@Override
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public String[] getFileExtensions() {
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }//这个方法表明这个类支持解析以json结尾的配置文件
  49. public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }return new String[]{"json"};
  72. public class SqlSessionFactoryBean
  73.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  74.    
  75.     @Override
  76.     public void onApplicationEvent(ApplicationEvent event) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  81.     }
  82.    
  83. }}
  84. public class SqlSessionFactoryBean
  85.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  86.    
  87.     @Override
  88.     public void onApplicationEvent(ApplicationEvent event) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  92. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  93.     }
  94.    
  95. }@Override
  96. public class SqlSessionFactoryBean
  97.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  98.    
  99.     @Override
  100.     public void onApplicationEvent(ApplicationEvent event) {
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  103. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  104. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  105.     }
  106.    
  107. }public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
  108. public class SqlSessionFactoryBean
  109.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  110.    
  111.     @Override
  112.     public void onApplicationEvent(ApplicationEvent event) {
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  115. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  116. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  117.     }
  118.    
  119. }public class SqlSessionFactoryBean
  120.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  121.    
  122.     @Override
  123.     public void onApplicationEvent(ApplicationEvent event) {
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  126. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  127. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  128.     }
  129.    
  130. }ReadableByteChannel readableByteChannel = resource.readableChannel();
  131. public class SqlSessionFactoryBean
  132.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  133.    
  134.     @Override
  135.     public void onApplicationEvent(ApplicationEvent event) {
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  138. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  139. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  140.     }
  141.    
  142. }public class SqlSessionFactoryBean
  143.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  144.    
  145.     @Override
  146.     public void onApplicationEvent(ApplicationEvent event) {
  147. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  148. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  149. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  150. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  151.     }
  152.    
  153. }ByteBuffer byteBuffer = ByteBuffer.allocate((int) resource.contentLength());
  154. public class SqlSessionFactoryBean
  155.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  156.    
  157.     @Override
  158.     public void onApplicationEvent(ApplicationEvent event) {
  159. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  160. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  161. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  162. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  163.     }
  164.    
  165. }public class SqlSessionFactoryBean
  166.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  167.    
  168.     @Override
  169.     public void onApplicationEvent(ApplicationEvent event) {
  170. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  171. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  172. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  173. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  174.     }
  175.    
  176. }//将文件内容读到 ByteBuffer 中
  177. public class SqlSessionFactoryBean
  178.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  179.    
  180.     @Override
  181.     public void onApplicationEvent(ApplicationEvent event) {
  182. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  183. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  184. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  185. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  186.     }
  187.    
  188. }public class SqlSessionFactoryBean
  189.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  190.    
  191.     @Override
  192.     public void onApplicationEvent(ApplicationEvent event) {
  193. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  194. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  195. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  196. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  197.     }
  198.    
  199. }readableByteChannel.read(byteBuffer);
  200. public class SqlSessionFactoryBean
  201.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  202.    
  203.     @Override
  204.     public void onApplicationEvent(ApplicationEvent event) {
  205. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  206. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  207. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  208. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  209.     }
  210.    
  211. }public class SqlSessionFactoryBean
  212.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  213.    
  214.     @Override
  215.     public void onApplicationEvent(ApplicationEvent event) {
  216. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  217. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  218. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  219. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  220.     }
  221.    
  222. }//将读出来的字节转换成字符串
  223. public class SqlSessionFactoryBean
  224.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  225.    
  226.     @Override
  227.     public void onApplicationEvent(ApplicationEvent event) {
  228. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  229. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  230. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  231. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  232.     }
  233.    
  234. }public class SqlSessionFactoryBean
  235.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  236.    
  237.     @Override
  238.     public void onApplicationEvent(ApplicationEvent event) {
  239. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  240. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  241. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  242. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  243.     }
  244.    
  245. }String content = new String(byteBuffer.array());
  246. public class SqlSessionFactoryBean
  247.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  248.    
  249.     @Override
  250.     public void onApplicationEvent(ApplicationEvent event) {
  251. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  252. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  253. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  254. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  255.     }
  256.    
  257. }public class SqlSessionFactoryBean
  258.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  259.    
  260.     @Override
  261.     public void onApplicationEvent(ApplicationEvent event) {
  262. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  263. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  264. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  265. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  266.     }
  267.    
  268. }// 将字符串转换成 JSONObject
  269. public class SqlSessionFactoryBean
  270.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  271.    
  272.     @Override
  273.     public void onApplicationEvent(ApplicationEvent event) {
  274. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  275. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  276. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  277. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  278.     }
  279.    
  280. }public class SqlSessionFactoryBean
  281.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  282.    
  283.     @Override
  284.     public void onApplicationEvent(ApplicationEvent event) {
  285. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  286. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  287. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  288. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  289.     }
  290.    
  291. }JSONObject jsonObject = JSON.parseObject(content);
  292. public class SqlSessionFactoryBean
  293.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  294.    
  295.     @Override
  296.     public void onApplicationEvent(ApplicationEvent event) {
  297. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  298. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  299. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  300. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  301.     }
  302.    
  303. }public class SqlSessionFactoryBean
  304.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  305.    
  306.     @Override
  307.     public void onApplicationEvent(ApplicationEvent event) {
  308. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  309. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  310. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  311. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  312.     }
  313.    
  314. }Map<String, Object> map = new HashMap<>(jsonObject.size());
  315. public class SqlSessionFactoryBean
  316.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  317.    
  318.     @Override
  319.     public void onApplicationEvent(ApplicationEvent event) {
  320. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  321. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  322. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  323. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  324.     }
  325.    
  326. }public class SqlSessionFactoryBean
  327.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  328.    
  329.     @Override
  330.     public void onApplicationEvent(ApplicationEvent event) {
  331. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  332. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  333. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  334. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  335.     }
  336.    
  337. }//将 json 的键值对读出来,放入到 map 中
  338. public class SqlSessionFactoryBean
  339.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  340.    
  341.     @Override
  342.     public void onApplicationEvent(ApplicationEvent event) {
  343. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  344. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  345. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  346. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  347.     }
  348.    
  349. }public class SqlSessionFactoryBean
  350.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  351.    
  352.     @Override
  353.     public void onApplicationEvent(ApplicationEvent event) {
  354. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  355. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  356. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  357. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  358.     }
  359.    
  360. }for (String key : jsonObject.keySet()) {
  361. public class SqlSessionFactoryBean
  362.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  363.    
  364.     @Override
  365.     public void onApplicationEvent(ApplicationEvent event) {
  366. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  367. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  368. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  369. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  370.     }
  371.    
  372. }public class SqlSessionFactoryBean
  373.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  374.    
  375.     @Override
  376.     public void onApplicationEvent(ApplicationEvent event) {
  377. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  378. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  379. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  380. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  381.     }
  382.    
  383. }public class SqlSessionFactoryBean
  384.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  385.    
  386.     @Override
  387.     public void onApplicationEvent(ApplicationEvent event) {
  388. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  389. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  390. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  391. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  392.     }
  393.    
  394. }map.put(key, jsonObject.getString(key));
  395. public class SqlSessionFactoryBean
  396.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  397.    
  398.     @Override
  399.     public void onApplicationEvent(ApplicationEvent event) {
  400. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  401. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  402. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  403. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  404.     }
  405.    
  406. }public class SqlSessionFactoryBean
  407.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  408.    
  409.     @Override
  410.     public void onApplicationEvent(ApplicationEvent event) {
  411. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  412. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  413. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  414. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  415.     }
  416.    
  417. }}
  418. public class SqlSessionFactoryBean
  419.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  420.    
  421.     @Override
  422.     public void onApplicationEvent(ApplicationEvent event) {
  423. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  424. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  425. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  426. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  427.     }
  428.    
  429. }public class SqlSessionFactoryBean
  430.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  431.    
  432.     @Override
  433.     public void onApplicationEvent(ApplicationEvent event) {
  434. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  435. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  436. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  437. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  438.     }
  439.    
  440. }return Collections.singletonList(new MapPropertySource("jsonPropertySource", map));
  441. public class SqlSessionFactoryBean
  442.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  443.    
  444.     @Override
  445.     public void onApplicationEvent(ApplicationEvent event) {
  446. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  447. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  448. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  449. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  450.     }
  451.    
  452. }}
  453. }
复制代码
到此,其实就扩展完了,接下来就来测试一下。
测试

先创建一个application.json的配置文件
application.json配置文件改造User
  1. org.springframework.boot.env.PropertySourceLoader=\
  2. com.sanyou.spring.extension.springbootextension.propertysourceloader.JsonPropertySourceLoader
复制代码
  
启动项目
  1. public class User {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }// 注入配置文件的属性
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }@Value("${sanyou.username:}")
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }private String username;
  38. }
复制代码
  
运行结果:
  1. @SpringBootApplication
  2. public class Application {
  3. public class SqlSessionFactoryBean
  4.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  5.    
  6.     @Override
  7.     public void onApplicationEvent(ApplicationEvent event) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  12.     }
  13.    
  14. }public static void main(String[] args) {
  15. public class SqlSessionFactoryBean
  16.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  17.    
  18.     @Override
  19.     public void onApplicationEvent(ApplicationEvent event) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  23. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  24.     }
  25.    
  26. }public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class);
  38. public class SqlSessionFactoryBean
  39.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  40.    
  41.     @Override
  42.     public void onApplicationEvent(ApplicationEvent event) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  46. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  47.     }
  48.    
  49. }public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }User user = applicationContext.getBean(User.class);
  61. public class SqlSessionFactoryBean
  62.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  63.    
  64.     @Override
  65.     public void onApplicationEvent(ApplicationEvent event) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  69. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  70.     }
  71.    
  72. }public class SqlSessionFactoryBean
  73.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  74.    
  75.     @Override
  76.     public void onApplicationEvent(ApplicationEvent event) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  80. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  81.     }
  82.    
  83. }System.out.println("获取到的Bean为" + user + ",属性username值为:" + user.getUsername());
  84. public class SqlSessionFactoryBean
  85.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  86.    
  87.     @Override
  88.     public void onApplicationEvent(ApplicationEvent event) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  92. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  93.     }
  94.    
  95. }}
  96. public class SqlSessionFactoryBean
  97.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  98.    
  99.     @Override
  100.     public void onApplicationEvent(ApplicationEvent event) {
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  103. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  104. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  105.     }
  106.    
  107. }@Bean
  108. public class SqlSessionFactoryBean
  109.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  110.    
  111.     @Override
  112.     public void onApplicationEvent(ApplicationEvent event) {
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  115. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  116. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  117.     }
  118.    
  119. }public User user() {
  120. public class SqlSessionFactoryBean
  121.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  122.    
  123.     @Override
  124.     public void onApplicationEvent(ApplicationEvent event) {
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  126. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  127. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  128. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  129.     }
  130.    
  131. }public class SqlSessionFactoryBean
  132.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  133.    
  134.     @Override
  135.     public void onApplicationEvent(ApplicationEvent event) {
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  138. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  139. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  140.     }
  141.    
  142. }return new User();
  143. public class SqlSessionFactoryBean
  144.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  145.    
  146.     @Override
  147.     public void onApplicationEvent(ApplicationEvent event) {
  148. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  149. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  150. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  151. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  152.     }
  153.    
  154. }}
  155. }
复制代码
成功将json配置文件的属性注入到User对象中。
至此,SpringBoot就支持了以json为结尾的配置文件格式。
Nacos对于PropertySourceLoader的实现

如果你的项目正在用Nacos作为配置中心,那么刚刚好,Nacos已经实现json配置文件格式的解析。
Nacos对于PropertySourceLoader的实现Nacos不仅实现了json格式的解析,也实现了关于xml格式的配置文件的解析,并且优先级会比SpringBoot默认的xml格式文件解析的优先级高。至于Nacos为啥需要实现PropertySourceLoader?其实很简单,因为Nacos作为配置中心,不仅支持properties和yaml格式的文件,还支持json格式的配置文件,那么客户端拿到这些配置就需要解析,SpringBoot已经支持了properties和yaml格式的文件的解析,那么Nacos只需要实现SpringBoot不支持的就可以了。
3、ApplicationContextInitializer

ApplicationContextInitializer也是SpringBoot启动过程的一个扩展点。
ApplicationContextInitializer在SpringBoot启动过程,会回调这个类的实现initialize方法,传入ConfigurableApplicationContext。
那怎么用呢?
依然是SPI。
SPI加载ApplicationContextInitializer然后遍历所有的实现,依次调用
调用initialize这里就不演示了,实现接口,按照如下这种配置就行了
但是这里需要注意的是,此时传入的ConfigurableApplicationContext并没有调用过refresh方法,也就是里面是没有Bean对象的,一般这个接口是用来配置ConfigurableApplicationContext,而不是用来获取Bean的。
4、EnvironmentPostProcessor

EnvironmentPostProcessor在SpringBoot启动过程中,也会调用,也是通过SPI机制来加载扩展的。
EnvironmentPostProcessorEnvironmentPostProcessor是用来处理ConfigurableEnvironment的,也就是一些配置信息,SpringBoot所有的配置都是存在这个对象的。
说这个类的主要原因,主要不是说扩展,而是他的一个实现类很关键。
ConfigFileApplicationListener这个类的作用就是用来处理外部化配置文件的,也就是这个类是用来处理配置文件的,通过前面提到的PropertySourceLoader解析配置文件,放到ConfigurableEnvironment里面。
5、ApplicationRunner和CommandLineRunner

ApplicationRunner和CommandLineRunner都是在SpringBoot成功启动之后会调用,可以拿到启动时的参数。
那怎么扩展呢?
当然又是SPI了。
 这两个其实不是通过SPI机制来扩展,而是直接从容器中获取的,这又是为啥呢?
因为调用ApplicationRunner和CommandLineRunner时,SpringBoot已经启动成功了,Spring容器都准备好了,需要什么Bean直接从容器中查找多方便。
而前面说的几个需要SPI机制的扩展点,是因为在SpringBoot启动的时候,Spring容器还没有启动好,也就是无法从Spring容器获取到这些扩展的对象,为了兼顾扩展性,所以就通过SPI机制来实现获取到实现类。
刷新上下文和调用Runner加载和调用Runner所以要想扩展这个点,只需要实现接口,添加到Spring容器就可以了。
Spring Event 事件

Event 事件可以说是一种观察者模式的实现,主要是用来解耦合的。当发生了某件事,只要发布一个事件,对这个事件的监听者(观察者)就可以对事件进行响应或者处理。
举个例子来说,假设发生了火灾,可能需要打119、救人,那么就可以基于事件的模型来实现,只需要打119、救人监听火灾的发生就行了,当发生了火灾,通知这些打119、救人去触发相应的逻辑操作。
什么是Spring Event 事件

那么是什么是Spring Event 事件,就是Spring实现了这种事件模型,你只需要基于Spring提供的API进行扩展,就可以完成事件的发布订阅
Spring提供的事件api:
ApplicationEvent

ApplicationEvent事件的父类,所有具体的事件都得继承这个类,构造方法的参数是这个事件携带的参数,监听器就可以通过这个参数来进行一些业务操作。
ApplicationListener

ApplicationListener事件监听的接口,泛型是子类需要监听的事件类型,子类需要实现onApplicationEvent,参数就是事件类型,onApplicationEvent方法的实现就代表了对事件的处理,当事件发生时,Spring会回调onApplicationEvent方法的实现,传入发布的事件。
ApplicationEventPublisher

ApplicationEventPublisher事件发布器,通过publishEvent方法就可以发布一个事件,然后就可以触发监听这个事件的监听器的回调。
ApplicationContext实现了ApplicationEventPublisher接口,所以通过ApplicationContext就可以发布事件。
那怎么才能拿到ApplicationContext呢?
前面Bean生命周期那节说过,可以通过ApplicationContextAware接口拿到,甚至你可以通过实现ApplicationEventPublisherAware直接获取到ApplicationEventPublisher,其实获取到的ApplicationEventPublisher也就是ApplicationContext,因为是ApplicationContext实现了ApplicationEventPublisher。
话不多说,上代码

就以上面的火灾为例
第一步:创建一个火灾事件类

火灾事件类继承ApplicationEvent
  1. 获取到的Bean为com.sanyou.spring.extension.User@481ba2cf,属性username值为:三友的java日记
复制代码
  
第二步:创建火灾事件的监听器

打119的火灾事件的监听器:
  1. // 火灾事件
  2. public class FireEvent extends ApplicationEvent {
  3. public class SqlSessionFactoryBean
  4.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  5.    
  6.     @Override
  7.     public void onApplicationEvent(ApplicationEvent event) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  12.     }
  13.    
  14. }public FireEvent(String source) {
  15. public class SqlSessionFactoryBean
  16.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  17.    
  18.     @Override
  19.     public void onApplicationEvent(ApplicationEvent event) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  23. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  24.     }
  25.    
  26. }public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }super(source);
  38. public class SqlSessionFactoryBean
  39.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  40.    
  41.     @Override
  42.     public void onApplicationEvent(ApplicationEvent event) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  46. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  47.     }
  48.    
  49. }}
  50. }
复制代码
 
救人的火灾事件的监听器:
  1. public class Call119FireEventListener implements ApplicationListener<FireEvent> {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }@Override
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public void onApplicationEvent(FireEvent event) {
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }System.out.println("打119");
  49. public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }}
  61. }
复制代码
  
事件和对应的监听都有了,接下来进行测试:
  1. public class SavePersonFireEventListener implements ApplicationListener<FireEvent> {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }@Override
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public void onApplicationEvent(FireEvent event) {
  26. public class SqlSessionFactoryBean
  27.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  28.    
  29.     @Override
  30.     public void onApplicationEvent(ApplicationEvent event) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  34. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  35.     }
  36.    
  37. }public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }System.out.println("救人");
  49. public class SqlSessionFactoryBean
  50.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  51.    
  52.     @Override
  53.     public void onApplicationEvent(ApplicationEvent event) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  57. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  58.     }
  59.    
  60. }}
  61. }
复制代码
将两个事件注册到Spring容器中,然后发布FireEvent事件
运行结果:
  1. public class Application {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }public static void main(String[] args) {
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public class SqlSessionFactoryBean
  26.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  27.    
  28.     @Override
  29.     public void onApplicationEvent(ApplicationEvent event) {
  30. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  34.     }
  35.    
  36. }AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
  37. public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }public class SqlSessionFactoryBean
  49.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  50.    
  51.     @Override
  52.     public void onApplicationEvent(ApplicationEvent event) {
  53. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  57.     }
  58.    
  59. }//将 事件监听器 注册到容器中
  60. public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }public class SqlSessionFactoryBean
  72.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  73.    
  74.     @Override
  75.     public void onApplicationEvent(ApplicationEvent event) {
  76. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  80.     }
  81.    
  82. }applicationContext.register(Call119FireEventListener.class);
  83. public class SqlSessionFactoryBean
  84.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  85.    
  86.     @Override
  87.     public void onApplicationEvent(ApplicationEvent event) {
  88. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  92.     }
  93.    
  94. }public class SqlSessionFactoryBean
  95.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  96.    
  97.     @Override
  98.     public void onApplicationEvent(ApplicationEvent event) {
  99. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  100. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  103.     }
  104.    
  105. }applicationContext.register(SavePersonFireEventListener.class);
  106. public class SqlSessionFactoryBean
  107.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  108.    
  109.     @Override
  110.     public void onApplicationEvent(ApplicationEvent event) {
  111. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  112. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  115.     }
  116.    
  117. }public class SqlSessionFactoryBean
  118.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  119.    
  120.     @Override
  121.     public void onApplicationEvent(ApplicationEvent event) {
  122. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  123. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  126.     }
  127.    
  128. }applicationContext.refresh();
  129. public class SqlSessionFactoryBean
  130.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  131.    
  132.     @Override
  133.     public void onApplicationEvent(ApplicationEvent event) {
  134. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  135. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  138.     }
  139.    
  140. }public class SqlSessionFactoryBean
  141.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  142.    
  143.     @Override
  144.     public void onApplicationEvent(ApplicationEvent event) {
  145. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  146. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  147. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  148. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  149.     }
  150.    
  151. }// 发布着火的事件,触发监听
  152. public class SqlSessionFactoryBean
  153.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  154.    
  155.     @Override
  156.     public void onApplicationEvent(ApplicationEvent event) {
  157. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  158. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  159. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  160. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  161.     }
  162.    
  163. }public class SqlSessionFactoryBean
  164.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  165.    
  166.     @Override
  167.     public void onApplicationEvent(ApplicationEvent event) {
  168. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  169. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  170. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  171. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  172.     }
  173.    
  174. }applicationContext.publishEvent(new FireEvent("着火了"));
  175. public class SqlSessionFactoryBean
  176.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  177.    
  178.     @Override
  179.     public void onApplicationEvent(ApplicationEvent event) {
  180. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  181. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  182. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  183. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  184.     }
  185.    
  186. }}
  187. }
复制代码
控制台打印出了结果,触发了监听。
如果现在需要对火灾进行救火,那么只需要去监听FireEvent,实现救火的逻辑,注入到Spring容器中,就可以了,其余的代码根本不用动。
Spring内置的事件

Spring内置的事件很多,这里我罗列几个
事件类型触发时机ContextRefreshedEvent在调用ConfigurableApplicationContext 接口中的refresh()方法时触发ContextStartedEvent在调用ConfigurableApplicationContext的start()方法时触发ContextStoppedEvent在调用ConfigurableApplicationContext的stop()方法时触发ContextClosedEvent当ApplicationContext被关闭时触发该事件,也就是调用close()方法触发在Spring容器启动的过程中,Spring会发布这些事件,如果你需要这Spring容器启动的某个时刻进行什么操作,只需要监听对应的事件即可。
Spring事件的传播

Spring事件的传播是什么意思呢?
我们都知道,在Spring中有子父容器的概念,而Spring事件的传播就是指当通过子容器发布一个事件之后,不仅可以触发在这个子容器的事件监听器,还可以触发在父容器的这个事件的监听器。
上代码
  1. 打119
  2. 救人
复制代码
创建了两个容器,父容器注册了打119的监听器,子容器注册了救人的监听器,然后将子父容器通过setParent关联起来,最后通过子容器,发布了着火的事件。
运行结果:
  1. public class EventPropagateApplication {
  2. public class SqlSessionFactoryBean
  3.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  4.    
  5.     @Override
  6.     public void onApplicationEvent(ApplicationEvent event) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  11.     }
  12.    
  13. }public static void main(String[] args) {
  14. public class SqlSessionFactoryBean
  15.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  16.    
  17.     @Override
  18.     public void onApplicationEvent(ApplicationEvent event) {
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  21. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  22. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  23.     }
  24.    
  25. }public class SqlSessionFactoryBean
  26.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  27.    
  28.     @Override
  29.     public void onApplicationEvent(ApplicationEvent event) {
  30. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  32. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  33. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  34.     }
  35.    
  36. }// 创建一个父容器
  37. public class SqlSessionFactoryBean
  38.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  39.    
  40.     @Override
  41.     public void onApplicationEvent(ApplicationEvent event) {
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  43. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  44. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  45. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  46.     }
  47.    
  48. }public class SqlSessionFactoryBean
  49.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  50.    
  51.     @Override
  52.     public void onApplicationEvent(ApplicationEvent event) {
  53. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  54. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  55. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  56. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  57.     }
  58.    
  59. }AnnotationConfigApplicationContext parentApplicationContext = new AnnotationConfigApplicationContext();
  60. public class SqlSessionFactoryBean
  61.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  62.    
  63.     @Override
  64.     public void onApplicationEvent(ApplicationEvent event) {
  65. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  66. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  67. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  68. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  69.     }
  70.    
  71. }public class SqlSessionFactoryBean
  72.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  73.    
  74.     @Override
  75.     public void onApplicationEvent(ApplicationEvent event) {
  76. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  77. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  78. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  79. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  80.     }
  81.    
  82. }//将 打119监听器 注册到父容器中
  83. public class SqlSessionFactoryBean
  84.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  85.    
  86.     @Override
  87.     public void onApplicationEvent(ApplicationEvent event) {
  88. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  89. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  90. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  91. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  92.     }
  93.    
  94. }public class SqlSessionFactoryBean
  95.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  96.    
  97.     @Override
  98.     public void onApplicationEvent(ApplicationEvent event) {
  99. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  100. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  101. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  102. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  103.     }
  104.    
  105. }parentApplicationContext.register(Call119FireEventListener.class);
  106. public class SqlSessionFactoryBean
  107.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  108.    
  109.     @Override
  110.     public void onApplicationEvent(ApplicationEvent event) {
  111. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  112. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  113. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  114. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  115.     }
  116.    
  117. }public class SqlSessionFactoryBean
  118.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  119.    
  120.     @Override
  121.     public void onApplicationEvent(ApplicationEvent event) {
  122. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  123. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  124. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  125. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  126.     }
  127.    
  128. }parentApplicationContext.refresh();
  129. public class SqlSessionFactoryBean
  130.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  131.    
  132.     @Override
  133.     public void onApplicationEvent(ApplicationEvent event) {
  134. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  135. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  136. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  137. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  138.     }
  139.    
  140. }public class SqlSessionFactoryBean
  141.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  142.    
  143.     @Override
  144.     public void onApplicationEvent(ApplicationEvent event) {
  145. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  146. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  147. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  148. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  149.     }
  150.    
  151. }// 创建一个子容器
  152. public class SqlSessionFactoryBean
  153.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  154.    
  155.     @Override
  156.     public void onApplicationEvent(ApplicationEvent event) {
  157. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  158. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  159. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  160. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  161.     }
  162.    
  163. }public class SqlSessionFactoryBean
  164.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  165.    
  166.     @Override
  167.     public void onApplicationEvent(ApplicationEvent event) {
  168. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  169. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  170. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  171. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  172.     }
  173.    
  174. }AnnotationConfigApplicationContext childApplicationContext = new AnnotationConfigApplicationContext();
  175. public class SqlSessionFactoryBean
  176.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  177.    
  178.     @Override
  179.     public void onApplicationEvent(ApplicationEvent event) {
  180. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  181. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  182. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  183. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  184.     }
  185.    
  186. }public class SqlSessionFactoryBean
  187.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  188.    
  189.     @Override
  190.     public void onApplicationEvent(ApplicationEvent event) {
  191. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  192. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  193. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  194. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  195.     }
  196.    
  197. }//将 救人监听器 注册到子容器中
  198. public class SqlSessionFactoryBean
  199.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  200.    
  201.     @Override
  202.     public void onApplicationEvent(ApplicationEvent event) {
  203. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  204. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  205. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  206. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  207.     }
  208.    
  209. }public class SqlSessionFactoryBean
  210.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  211.    
  212.     @Override
  213.     public void onApplicationEvent(ApplicationEvent event) {
  214. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  215. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  216. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  217. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  218.     }
  219.    
  220. }childApplicationContext.register(SavePersonFireEventListener.class);
  221. public class SqlSessionFactoryBean
  222.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  223.    
  224.     @Override
  225.     public void onApplicationEvent(ApplicationEvent event) {
  226. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  227. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  228. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  229. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  230.     }
  231.    
  232. }public class SqlSessionFactoryBean
  233.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  234.    
  235.     @Override
  236.     public void onApplicationEvent(ApplicationEvent event) {
  237. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  238. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  239. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  240. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  241.     }
  242.    
  243. }childApplicationContext.refresh();
  244. public class SqlSessionFactoryBean
  245.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  246.    
  247.     @Override
  248.     public void onApplicationEvent(ApplicationEvent event) {
  249. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  250. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  251. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  252. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  253.     }
  254.    
  255. }public class SqlSessionFactoryBean
  256.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  257.    
  258.     @Override
  259.     public void onApplicationEvent(ApplicationEvent event) {
  260. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  261. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  262. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  263. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  264.     }
  265.    
  266. }// 设置一下父容器
  267. public class SqlSessionFactoryBean
  268.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  269.    
  270.     @Override
  271.     public void onApplicationEvent(ApplicationEvent event) {
  272. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  273. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  274. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  275. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  276.     }
  277.    
  278. }public class SqlSessionFactoryBean
  279.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  280.    
  281.     @Override
  282.     public void onApplicationEvent(ApplicationEvent event) {
  283. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  284. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  285. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  286. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  287.     }
  288.    
  289. }childApplicationContext.setParent(parentApplicationContext);
  290. public class SqlSessionFactoryBean
  291.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  292.    
  293.     @Override
  294.     public void onApplicationEvent(ApplicationEvent event) {
  295. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  296. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  297. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  298. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  299.     }
  300.    
  301. }public class SqlSessionFactoryBean
  302.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  303.    
  304.     @Override
  305.     public void onApplicationEvent(ApplicationEvent event) {
  306. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  307. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  308. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  309. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  310.     }
  311.    
  312. }// 通过子容器发布着火的事件,触发监听
  313. public class SqlSessionFactoryBean
  314.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  315.    
  316.     @Override
  317.     public void onApplicationEvent(ApplicationEvent event) {
  318. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  319. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  320. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  321. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  322.     }
  323.    
  324. }public class SqlSessionFactoryBean
  325.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  326.    
  327.     @Override
  328.     public void onApplicationEvent(ApplicationEvent event) {
  329. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  330. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  331. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  332. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  333.     }
  334.    
  335. }childApplicationContext.publishEvent(new FireEvent("着火了"));
  336. public class SqlSessionFactoryBean
  337.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  338.    
  339.     @Override
  340.     public void onApplicationEvent(ApplicationEvent event) {
  341. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  342. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  343. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  344. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  345.     }
  346.    
  347. }}
  348. }
复制代码
从打印的日志,的确可以看出,虽然是子容器发布了着火的事件,但是父容器的监听器也成功监听了着火事件。
源码验证

事件传播源码从这段源码可以看出,如果父容器不为空,就会通过父容器再发布一次事件。
传播特性的一个坑

前面说过,在Spring容器启动的过程,会发布很多事件,如果你需要有相应的扩展,可以监听这些事件。但是,在SpringCloud环境下,你的这些Spring发布的事件的监听器可能会执行很多次。为什么会执行很多次呢?其实就是跟传播特性有关。
在SpringCloud的环境下,为了使像FeignClient和RibbonClient这些不同的服务的配置相互隔离,会创建很多的子容器,而这些子容器都有一个公共的父容器,那就是SpringBoot项目启动时创建的容器,事件的监听器都在这个容器中。而这些为了配置隔离创建的子容器,在容器启动的过程中,也会发布诸如ContextRefreshedEvent等这样的事件,如果你监听了这些事件,那么由于传播特性的关系,你的这个事件的监听器就会触发多次。
如何解决这个坑呢?
你可以进行判断这些监听器有没有执行过,比如加一个判断的标志;或者是监听类似的事件,比如ApplicationStartedEvent事件,这种事件是在SpringBoot启动中发布的事件,而子容器不是SpringBoot,所以不会多次发这种事件,也就会只执行一次。
Spring事件的运用举例

1、在Mybatis中的使用

又来以Mybatis举例了。。Mybatis的SqlSessionFactoryBean监听了ApplicationEvent,然后判断如果是ContextRefreshedEvent就进行相应的处理,这个类还实现了FactoryBean接口。。
  1. 救人
  2. 打119
复制代码
说实话,这监听代码写的不太好,监听了ApplicationEvent,那么所有的事件都会回调这个类的onApplicationEvent方法,但是onApplicationEvent方法实现又是当ApplicationEvent是ContextRefreshedEvent类型才会往下走,那为什么不直接监听ContextRefreshedEvent呢?
可以给个差评。
  
膨胀了膨胀了。。
2、在SpringCloud的运用

在SpringCloud的中,当项目启动的时候,会自动往注册中心进行注册,那么是如何实现的呢?当然也是基于事件来的。当web服务器启动完成之后,就发布ServletWebServerInitializedEvent事件。
然后不同的注册中心的实现都只需要监听这个事件,就知道web服务器已经创建好了,那么就可以往注册中心注册服务实例了。如果你的服务没往注册中心,看看是不是web环境,因为只有web环境才会发这个事件。
SpringCloud提供了一个抽象类 AbstractAutoServiceRegistration,实现了对WebServerInitializedEvent(ServletWebServerInitializedEvent的父类)事件的监听
AbstractAutoServiceRegistration一般不同的注册中心都会去继承这个类,监听项目启动,实现往注册中心服务端进行注册。
Nacos对于AbstractAutoServiceRegistration的继承
Spring Event事件在Spring内部中运用很多,是解耦合的利器。在实际项目中,你既可以监听Spring/Boot内置的一些事件,进行相应的扩展,也可以基于这套模型在业务中自定义事件和相应的监听器,减少业务代码的耦合。
命名空间

最后来讲一个可能没有留意,但是很神奇的扩展点--命名空间。起初我知道这个扩展点的时候,我都惊呆了,这玩意也能扩展?真的不得不佩服Spring设计的可扩展性。
回忆一下啥是命名空间?
先看一段配置
  1. public class SqlSessionFactoryBean
  2.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  3.    
  4.     @Override
  5.     public void onApplicationEvent(ApplicationEvent event) {
  6. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  10.     }
  11.    
  12. }
复制代码
这一段xml配置想必都很熟悉,其中, context 标签就代表了一个命名空间。
也就说,这个标签是可以扩展的。
话不多说,来个扩展

接下来自定义命名空间 sanyou,总共分为3步。
第一步:定义一个xsd文件

如下:
  1. public class SqlSessionFactoryBean
  2.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  3.    
  4.     @Override
  5.     public void onApplicationEvent(ApplicationEvent event) {
  6. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  10.     }
  11.    
  12. }public class SqlSessionFactoryBean
  13.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  14.    
  15.     @Override
  16.     public void onApplicationEvent(ApplicationEvent event) {
  17. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  18. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  21.     }
  22.    
  23. }public class SqlSessionFactoryBean
  24.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  25.    
  26.     @Override
  27.     public void onApplicationEvent(ApplicationEvent event) {
  28. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  29. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  30. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  32.     }
  33.    
  34. }public class SqlSessionFactoryBean
  35.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  36.    
  37.     @Override
  38.     public void onApplicationEvent(ApplicationEvent event) {
  39. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  40. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  41. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  43.     }
  44.    
  45. }public class SqlSessionFactoryBean
  46.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  47.    
  48.     @Override
  49.     public void onApplicationEvent(ApplicationEvent event) {
  50. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  51. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  52. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  53. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  54.     }
  55.    
  56. }public class SqlSessionFactoryBean
  57.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  58.    
  59.     @Override
  60.     public void onApplicationEvent(ApplicationEvent event) {
  61. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  62. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  63. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  64. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  65.     }
  66.    
  67. }public class SqlSessionFactoryBean
  68.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  69.    
  70.     @Override
  71.     public void onApplicationEvent(ApplicationEvent event) {
  72. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  73. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  74. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  75. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  76.     }
  77.    
  78. }
复制代码
  
这个xsd文件来指明sanyou这个命名空间下有哪些标签和属性。这里我只指定了一个标签 mybean,mybean标签里面有个class的属性,然后这个标签的目的就是将class属性指定的Bean的类型,注入到Spring容器中,作用跟spring的 标签的作用是一样的。
xsd文件没有需要放的固定的位置,这里我放到 META-INF 目录下
第二步:解析这个命名空间

解析命名空间很简单,Spring都有配套的东西--NamespaceHandler接口,只要实现这个接口就行了。但一般我们不直接实现 NamespaceHandler 接口,我们可以继承 NamespaceHandlerSupport 类,这个类实现了 NamespaceHandler 接口。
  1. public class SanYouNameSpaceHandler extends NamespaceHandlerSupport {public class SqlSessionFactoryBean
  2.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  3.    
  4.     @Override
  5.     public void onApplicationEvent(ApplicationEvent event) {
  6. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  10.     }
  11.    
  12. }@Overridepublic class SqlSessionFactoryBean
  13.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  14.    
  15.     @Override
  16.     public void onApplicationEvent(ApplicationEvent event) {
  17. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  18. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  21.     }
  22.    
  23. }public void init() {public class SqlSessionFactoryBean
  24.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  25.    
  26.     @Override
  27.     public void onApplicationEvent(ApplicationEvent event) {
  28. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  29. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  30. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  32.     }
  33.    
  34. }public class SqlSessionFactoryBean
  35.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  36.    
  37.     @Override
  38.     public void onApplicationEvent(ApplicationEvent event) {
  39. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  40. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  41. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  43.     }
  44.    
  45. }//注册解析 mybean 标签的解析器public class SqlSessionFactoryBean
  46.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  47.    
  48.     @Override
  49.     public void onApplicationEvent(ApplicationEvent event) {
  50. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  51. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  52. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  53. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  54.     }
  55.    
  56. }public class SqlSessionFactoryBean
  57.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  58.    
  59.     @Override
  60.     public void onApplicationEvent(ApplicationEvent event) {
  61. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  62. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  63. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  64. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  65.     }
  66.    
  67. }registerBeanDefinitionParser("mybean", new SanYouBeanDefinitionParser());public class SqlSessionFactoryBean
  68.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  69.    
  70.     @Override
  71.     public void onApplicationEvent(ApplicationEvent event) {
  72. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  73. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  74. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  75. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  76.     }
  77.    
  78. }}public class SqlSessionFactoryBean
  79.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  80.    
  81.     @Override
  82.     public void onApplicationEvent(ApplicationEvent event) {
  83. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  84. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  85. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  86. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  87.     }
  88.    
  89. }private static class SanYouBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {public class SqlSessionFactoryBean
  90.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  91.    
  92.     @Override
  93.     public void onApplicationEvent(ApplicationEvent event) {
  94. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  95. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  96. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  97. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  98.     }
  99.    
  100. }public class SqlSessionFactoryBean
  101.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  102.    
  103.     @Override
  104.     public void onApplicationEvent(ApplicationEvent event) {
  105. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  106. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  107. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  108. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  109.     }
  110.    
  111. }@Overridepublic class SqlSessionFactoryBean
  112.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  113.    
  114.     @Override
  115.     public void onApplicationEvent(ApplicationEvent event) {
  116. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  117. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  118. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  119. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  120.     }
  121.    
  122. }public class SqlSessionFactoryBean
  123.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  124.    
  125.     @Override
  126.     public void onApplicationEvent(ApplicationEvent event) {
  127. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  128. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  129. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  130. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  131.     }
  132.    
  133. }protected boolean shouldGenerateId() {public class SqlSessionFactoryBean
  134.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  135.    
  136.     @Override
  137.     public void onApplicationEvent(ApplicationEvent event) {
  138. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  139. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  140. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  141. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  142.     }
  143.    
  144. }public class SqlSessionFactoryBean
  145.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  146.    
  147.     @Override
  148.     public void onApplicationEvent(ApplicationEvent event) {
  149. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  150. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  151. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  152. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  153.     }
  154.    
  155. }public class SqlSessionFactoryBean
  156.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  157.    
  158.     @Override
  159.     public void onApplicationEvent(ApplicationEvent event) {
  160. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  161. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  162. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  163. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  164.     }
  165.    
  166. }return true;public class SqlSessionFactoryBean
  167.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  168.    
  169.     @Override
  170.     public void onApplicationEvent(ApplicationEvent event) {
  171. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  172. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  173. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  174. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  175.     }
  176.    
  177. }public class SqlSessionFactoryBean
  178.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  179.    
  180.     @Override
  181.     public void onApplicationEvent(ApplicationEvent event) {
  182. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  183. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  184. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  185. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  186.     }
  187.    
  188. }}public class SqlSessionFactoryBean
  189.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  190.    
  191.     @Override
  192.     public void onApplicationEvent(ApplicationEvent event) {
  193. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  194. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  195. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  196. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  197.     }
  198.    
  199. }public class SqlSessionFactoryBean
  200.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  201.    
  202.     @Override
  203.     public void onApplicationEvent(ApplicationEvent event) {
  204. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  205. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  206. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  207. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  208.     }
  209.    
  210. }@Overridepublic class SqlSessionFactoryBean
  211.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  212.    
  213.     @Override
  214.     public void onApplicationEvent(ApplicationEvent event) {
  215. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  216. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  217. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  218. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  219.     }
  220.    
  221. }public class SqlSessionFactoryBean
  222.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  223.    
  224.     @Override
  225.     public void onApplicationEvent(ApplicationEvent event) {
  226. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  227. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  228. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  229. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  230.     }
  231.    
  232. }protected String getBeanClassName(Element element) {public class SqlSessionFactoryBean
  233.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  234.    
  235.     @Override
  236.     public void onApplicationEvent(ApplicationEvent event) {
  237. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  238. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  239. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  240. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  241.     }
  242.    
  243. }public class SqlSessionFactoryBean
  244.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  245.    
  246.     @Override
  247.     public void onApplicationEvent(ApplicationEvent event) {
  248. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  249. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  250. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  251. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  252.     }
  253.    
  254. }public class SqlSessionFactoryBean
  255.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  256.    
  257.     @Override
  258.     public void onApplicationEvent(ApplicationEvent event) {
  259. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  260. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  261. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  262. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  263.     }
  264.    
  265. }return element.getAttribute("class");public class SqlSessionFactoryBean
  266.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  267.    
  268.     @Override
  269.     public void onApplicationEvent(ApplicationEvent event) {
  270. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  271. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  272. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  273. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  274.     }
  275.    
  276. }public class SqlSessionFactoryBean
  277.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  278.    
  279.     @Override
  280.     public void onApplicationEvent(ApplicationEvent event) {
  281. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  282. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  283. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  284. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  285.     }
  286.    
  287. }}public class SqlSessionFactoryBean
  288.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  289.    
  290.     @Override
  291.     public void onApplicationEvent(ApplicationEvent event) {
  292. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  293. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  294. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  295. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  296.     }
  297.    
  298. }}}
复制代码
SanYouNameSpaceHandler的作用就是将sanyou命名空间中的mybean这个标签读出来,拿到class的属性,然后将这个class属性指定的class类型注入到Spring容器中,至于注册这个环节的代码,都交给了SanYouBeanDefinitionParser的父类来做了。
第三步:创建并配置spring.handlers和spring.schemas文件

先创建spring.handlers和spring.schemas文件
spring.handlers文件内容
  1. public class SanYouNameSpaceHandler extends NamespaceHandlerSupport {
  2.     @Override
  3.     public void init() {
  4. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd//注册解析 mybean 标签的解析器
  5. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdregisterBeanDefinitionParser("mybean", new SanYouBeanDefinitionParser());
  6.     }
  7.     private static class SanYouBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd@Override
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdprotected boolean shouldGenerateId() {
  10. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd    return true;
  11. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  12. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd@Override
  13. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdprotected String getBeanClassName(Element element) {
  14. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd    return element.getAttribute("class");
  15. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  16.     }
  17. }
复制代码
通过spring.handlers配置文件,就知道sanyou命名空间应该找SanYouNameSpaceHandler进行解析
spring.schemas文内容
  1. http\://sanyou.com/schema/sanyou=com.sanyou.spring.extension.namespace.SanYouNameSpaceHandler
复制代码
spring.schemas配置xsd文件的路径
文件都有了,只需要放到classpath下的META-INF文件夹就行了。
xsd、spring.handlers、spring.schema文件到这里,就完成了扩展,接下来进行测试
测试

先构建一个applicationContext.xml文件,放到resources目录下
  1. public class SqlSessionFactoryBean
  2.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  3.    
  4.     @Override
  5.     public void onApplicationEvent(ApplicationEvent event) {
  6. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  10.     }
  11.    
  12. }public class SqlSessionFactoryBean
  13.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  14.    
  15.     @Override
  16.     public void onApplicationEvent(ApplicationEvent event) {
  17. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  18. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  21.     }
  22.    
  23. }
复制代码
  
再写个测试类
  1. public class Application {public class SqlSessionFactoryBean
  2.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  3.    
  4.     @Override
  5.     public void onApplicationEvent(ApplicationEvent event) {
  6. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  7. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  8. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  9. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  10.     }
  11.    
  12. }public static void main(String[] args) {public class SqlSessionFactoryBean
  13.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  14.    
  15.     @Override
  16.     public void onApplicationEvent(ApplicationEvent event) {
  17. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  18. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  19. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  20. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  21.     }
  22.    
  23. }public class SqlSessionFactoryBean
  24.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  25.    
  26.     @Override
  27.     public void onApplicationEvent(ApplicationEvent event) {
  28. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  29. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  30. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  31. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  32.     }
  33.    
  34. }ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");public class SqlSessionFactoryBean
  35.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  36.    
  37.     @Override
  38.     public void onApplicationEvent(ApplicationEvent event) {
  39. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  40. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  41. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  42. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  43.     }
  44.    
  45. }public class SqlSessionFactoryBean
  46.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  47.    
  48.     @Override
  49.     public void onApplicationEvent(ApplicationEvent event) {
  50. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  51. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  52. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  53. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  54.     }
  55.    
  56. }applicationContext.refresh();public class SqlSessionFactoryBean
  57.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  58.    
  59.     @Override
  60.     public void onApplicationEvent(ApplicationEvent event) {
  61. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  62. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  63. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  64. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  65.     }
  66.    
  67. }public class SqlSessionFactoryBean
  68.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  69.    
  70.     @Override
  71.     public void onApplicationEvent(ApplicationEvent event) {
  72. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  73. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  74. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  75. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  76.     }
  77.    
  78. }User user = applicationContext.getBean(User.class);public class SqlSessionFactoryBean
  79.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  80.    
  81.     @Override
  82.     public void onApplicationEvent(ApplicationEvent event) {
  83. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  84. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  85. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  86. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  87.     }
  88.    
  89. }public class SqlSessionFactoryBean
  90.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  91.    
  92.     @Override
  93.     public void onApplicationEvent(ApplicationEvent event) {
  94. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  95. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  96. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  97. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  98.     }
  99.    
  100. }System.out.println(user);public class SqlSessionFactoryBean
  101.     implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
  102.    
  103.     @Override
  104.     public void onApplicationEvent(ApplicationEvent event) {
  105. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdif (failFast && event instanceof ContextRefreshedEvent) {
  106. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd// fail-fast -> check all statements are completed
  107. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsdthis.sqlSessionFactory.getConfiguration().getMappedStatementNames();
  108. http\://sanyou.com/schema/sanyou.xsd=META-INF/sanyou.xsd}
  109.     }
  110.    
  111. }}}
复制代码
运行结果:
  1. public class Application {
  2.     public static void main(String[] args) {
  3.         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  4.         applicationContext.refresh();
  5.         User user = applicationContext.getBean(User.class);
  6.         System.out.println(user);
  7.     }
  8. }
复制代码
成功获取到User这个对象,说明自定义标签生效了。
Spring内置命名空间的扩展

NameSpaceHandler的spring实现通过NameSpaceHandler接口的这些实现类的命名就可以看出来有哪些扩展和这些扩展的作用,比如有处理aop的,有处理mvc的等等之类的。
开源框架对命名空间的扩展

1、Mybatis的扩展

Mybatis的NameSpaceHandler实现这个就是来扫描指定路径的mapper接口的,处理 scan 标签,跟@MapperScan注解的作用是一样的。
2、dubbo的扩展

使用dubbo可能写过如下的配置
[code][/code]这个dubbo命名空间肯定就是扩展的Spring的,也有对应的dubbo实现的NameSpaceHandler。
DubboNamespaceHandler不得不说,dubbo解析的标签可真的多啊,不过功能也是真的多。
总结

到这,本文就接近尾声了,这里画两张图来总结一下本文讲了Spring的哪些扩展点。
整体SpringBoot启动扩展点通过学习Spring的这些扩展点,既可以帮助我们应对日常的开发,还可以帮助我们更好地看懂Spring的源码。
最后,本文前前后后花了一周多的时间完成,如果对你有点帮助,还请帮忙点赞、在看、转发、非常感谢。
扫码或者搜索关注公众号三友的java日记,及时干货不错过,公众号致力于通过画图加上通俗易懂的语言讲解技术,让技术更加容易学习,回复 面试 即可获得一套面试题。
哦,差点忘了,本文所有demo代码,可以扫码或者搜索关注微信公众号 三友的java日记 ,回复 spring扩展点 即可获取。
 
往期热门文章推荐
 
扫码或者搜索关注公众号 三友的java日记 ,及时干货不错过,公众号致力于通过画图加上通俗易懂的语言讲解技术,让技术更加容易学习,回复 面试 即可获得一套面试真题。

 
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

忿忿的泥巴坨

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表