当前位置:   article > 正文

spring-mybatis核心笔记_mybatis spring beanfactory

mybatis spring beanfactory

mybatis通过代理模式产生mapper的代理类,jdk的动态代理

spring怎么把mybatis产生的代理对象注入到容器中?

  1. @MapperScan通过@Import引入MapperScannerRegistrar,这里采用@Import注解的第三种使用方式
  2. MapperScannerRegistrar类通过实现ImportBeanDefinitionRegistrar接口的registerBeanDefinitions方法把scan扫描到的类信息解析成beanDefinition放到beanDefinitionMap中,其中包括MapperFactoryBean,此类就是mybatis用来创建bean的factoryBean
  3. getSqlSseion通过jdk的动态代理返回mapper的代理对象,MethodProxy就是InvocationHandler接口的实现类,里面实现mapper的方法的具体逻辑
  4. mybatis插件原理
  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target(ElementType.TYPE)
  3. @Documented
  4. @Import(MapperScannerRegistrar.class)
  5. public @interface MapperScan {
  6. }

  1. public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T> {
  2. /**
  3. * {@inheritDoc}
  4. */
  5. @Override
  6. public T getObject() throws Exception {
  7. return getSqlSession().getMapper(this.mapperInterface);
  8. }
  9. }
  1. public class MapperProxyFactory<T> {
  2. private final Class<T> mapperInterface;
  3. private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();
  4. public MapperProxyFactory(Class<T> mapperInterface) {
  5. this.mapperInterface = mapperInterface;
  6. }
  7. public Class<T> getMapperInterface() {
  8. return mapperInterface;
  9. }
  10. public Map<Method, MapperMethod> getMethodCache() {
  11. return methodCache;
  12. }
  13. @SuppressWarnings("unchecked")
  14. protected T newInstance(MapperProxy<T> mapperProxy) {
  15. //这里即jdk动态代理的方法
  16. return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  17. }
  1. public class MapperProxy<T> implements InvocationHandler, Serializable {
  2. private static final long serialVersionUID = -6424540398559729838L;
  3. private final SqlSession sqlSession;
  4. private final Class<T> mapperInterface;
  5. private final Map<Method, MapperMethod> methodCache;
  6. public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
  7. this.sqlSession = sqlSession;
  8. this.mapperInterface = mapperInterface;
  9. this.methodCache = methodCache;
  10. }
  11. @Override
  12. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  13. try {
  14. if (Object.class.equals(method.getDeclaringClass())) {
  15. return method.invoke(this, args);
  16. } else if (isDefaultMethod(method)) {
  17. return invokeDefaultMethod(proxy, method, args);
  18. }
  19. } catch (Throwable t) {
  20. throw ExceptionUtil.unwrapThrowable(t);
  21. }
  22. final MapperMethod mapperMethod = cachedMapperMethod(method);
  23. return mapperMethod.execute(sqlSession, args);
  24. }

  • spring注入对象的方式
  1. @Bean
  2. FactoryBean spring提供的一个接口,用来手动创建bean,特点是可以自定义bean的创建过程
  3. api registrySingleton --beanFactory.registerSingleton()
  4. FactoryMethod
  • beanfactory和factorybean

beanfactory  spring的bean工厂,产生bean;

factorybean是个特殊的bean,需要实现FactoryBean接口的三个方法,getObject方法返回一个bean

  • @Import注解的三种使用方式总结

第一种用法:@Import({ 要导入的容器中的组件 } ):容器会自动注册这个组件,id默认是全类名

 

第二种用法:ImportSelector:返回需要导入的组件的全类名数组,springboot底层用的特别多【重点 

 

第三种用法:ImportBeanDefinitionRegistrar:手动注册bean到容器

  • spring加载类的大概过程:

jvm加载类信息===========>beandefinition========>put到 beandefinitionMap ==========通过beanDefinition的信息创建bean

参考Factorybean,BeanFactory,Spring扩展点和生命周期

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/1018074
推荐阅读
相关标签
  

闽ICP备14008679号