1.FactoryBean用途
FactoryBean是一个工厂bean,自定义工厂bean时需要继承FactoryBean类
FactoryBean代码如下
package org.springframework.beans.factory;
import org.springframework.lang.Nullable;
public interface FactoryBean<T> {
@Nullable
T getObject() throws Exception;
@Nullable
Class<?> getObjectType();
default boolean isSingleton() {
return true;
}
其中getObject()返回的就是bean
mybatis-spring 项目中的SqlSessionFactoryBean就是实现的FactoryBean,
public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent>
@Override
public SqlSessionFactory getObject() throws Exception {
if (this.sqlSessionFactory == null) {
afterPropertiesSet();
}
ret