是这样的 我在看 spring 代码的时候 发现在 registerlistener 方法里的注释有写到不要在这里初始化 factorybean
AbstractApplicationContext.java
protected void registerListeners() {
// Register statically specified listeners first.
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
//↓↓↓↓↓↓↓↓↓↓↓↓注释在这里↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
但是我转去看 mybatis 的 sqlfactorybean 的时候 又看到他有实现 listener 接口
SqlSessionFactoryBean.java
public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean,
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
ApplicationListener<ApplicationEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(SqlSessionFactoryBean.class);
private static final ResourcePatternResolver RESOURCE_PATTERN_RESOLVER = new PathMatchingResourcePatternResolver();
private static final MetadataReaderFactory METADATA_READER_FACTORY = new CachingMetadataReaderFactory();
...
我试着注册一个 SqlSessionFactoryBean 这确实会导致这个 factorybean 在 registerListeners()中被初始化
这是怎么一回事?难道是 mybatis 的实现不规范???还是我对那段注释的理解不对
1
yuankui 2019-05-31 13:23:56 +08:00
可能他不需要 post-processors 去处理他吧。
spring 只是建议不要这么搞,但是实际上,你仍然可以这么搞,只要你知道后果就行。何必那么墨守成规呢? spring 这么多年,修修补补,里面很多代码,概念都是矛盾的。 自己清楚他的原理就行。 |
2
airfling 2019-05-31 14:17:13 +08:00
我有自己想初始化的东西,而且优先级比较高的我也是这样搞的
|