问题描述

在使用Mybatis开发中,或者和Spring整合中,在Dao层中的Mapper接口与xml中的sql对应着,在service中直接调用Dao中的方法就可以直接访问sql。如下所示:

  1. /**
  2. * interface 层的代码
  3. */
  4. public interface ArticleMapper {
  5. Article selectByPrimaryKey(Integer id);
  6. }

在xml中,我们的sql语句这样定义:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="cn.com.gkmeteor.article.provider.mapper.ArticleMapper"> <!-- interface 与 sql文件 关联语句 -->
  4. ...
  5.  
  6. <!-- xml中对应的sql -->
  7. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  8. select
  9. <include refid="Base_Column_List" />
  10. from tb_article
  11. where id = #{id,jdbcType=INTEGER}
  12. </select>
  13.  
  14. ...
  15. </mapper>

我们在业务代码中,如下调用的方式就可以从数据库中获取到数据:

  1. @Resource
  2. private ArticleMapper articleMapper;
  3.  
  4. ...
  5.  
  6. public Article getArticleById(Integer id) {
  7. return articleMapper.selectByPrimaryKey(id);
  8. }

如果仔细研究就会发现,ArticleMapper 接口并没有实现类,我们看到xml代码中的关联语句,知道知道mapper和sql是有关联的,但是也只知道这么多了。问题是,执行mapper中的方法后为什么就可以执行xml中定义的sql呢?本文对这个问题进行回答。

mybatis执行sql的完整流程

在MyBatis框架下中调用一个sql时候,会经历下面这个完整的流程:

  1. // 解析配置文件,生成配置
  2. String resource = "mybatis-config.xml";
  3. InputStream inputStream = Resources.getResourceAsStream(resource);
  4.  
  5. // 根据配置,构建一个SqlSessionFactory
  6. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  7.  
  8. // 得到一个真正可用的SqlSession
  9. SqlSession sqlSession = sqlSessionFactory.openSession();
  10.  
  11. // 从SqlSession获取interface的代理
  12. ArticleMapper articleMapperProxy = sqlSession.getMapper(ArticleMapper.class);
  13.  
  14. // 执行代理类中的方法
  15. Article article = articleMapperProxy.selectByPrimaryKey(123);
  16.  
  17. // 以下省略对 article 的操作

我们这里不讨论mybatis解析配置文件和加载configuration的过程,也就是上面的 build 方法。这里面同样有些学问,在这里我们简单认为通过 build,我们得到了 sqlSessionFactory 对象,里面包含 configuration 对象。

在这篇文章中,我们只关注mybatis初始化完成之后,mybatis框架到底做了哪些事情来让我们自如地执行sql。我们先来了解上面代码中重要的三个概念。

SqlSessionFactoryBuilder

这是SqlSessionFactory的构造器,其中最重要的是build方法,通过build方法可以构建出 SqlSessionFactory的实例。

SqlSessionFactory

SqlSessionFactory实例被 build 方法创建出来以后,在应用的运行期间一直存在。它包含了很多重要信息,这些信息在之后的调用过程中会反复使用。让我们来看一下:

  1. # sqlSessionFactory 中的重要信息
  2.  
  3. sqlSessionFactory
  4. configuration
  5. environment # 里面有 dataSource 信息
  6. mapperRegistry
  7. config # 里面有配置信息
  8. knownMappers # 里面有所有的 mapper,让我们记住它,它将是后面篇章中的主角
  9. mappedStatements # 里面有所有 mapper 的所有方法
  10. resultMaps # 里面有所有 xml 中的所有 resultMap
  11. sqlFragments # 里面有所有的 sql 片段

sqlSessionFactory最重要的是 openSession 方法,返回了一个 SqlSession 实例。

SqlSession

一个请求线程会有一个SqlSession实例。该实例贯穿了数据库连接的生命周期,是访问数据库的唯一渠道。sqlSession 中有 selectList,selectOne 等所有的sql增删改查数据库的方法,最终对数据库的操作都落在 sqlSession 上。

sqlSession的getMapper流程

有了上面三个重点类的基本知识以后,我们知道对数据库的操作都落在 sqlSession 上。可以先把结论告诉你,sqlSession.getMapper之后,获取到的其实是 configuration 对象中的 mapper的代理类。那么,具体的过程是怎样的?

详细流程

我们跟踪进去看sqlSession.getMapper以后到底发生了什么。

  1. public class DefaultSqlSession implements SqlSession {
  2.  
  3. private final Configuration configuration;
  4. private final Executor executor;
  5.  
  6. private final boolean autoCommit;
  7. private boolean dirty;
  8. private List<Cursor<?>> cursorList;
  9.  
  10. public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
  11. this.configuration = configuration;
  12. this.executor = executor;
  13. this.dirty = false;
  14. this.autoCommit = autoCommit;
  15. }
  16.  
  17. public DefaultSqlSession(Configuration configuration, Executor executor) {
  18. this(configuration, executor, false);
  19. }
  20.  
  21. ...
  22.  
  23. // 重点方法
  24. @Override
  25. public <T> T getMapper(Class<T> type) {
  26. return configuration.<T>getMapper(type, this);
  27. }
  28.  
  29. }

通过调用DefaultSqlSession的getMapper方法并且传入一个类型对象获取,底层调用的是配置对象configuration的getMapper方法,configuration对象是我们在加载DefaultSqlSessionFactory时传入的。

然后我们再来看下这个配置对象的getMapper,传入的是【类型对象】和【自身 (DefaultSqlSession) 】。(这里说的类型对象,就是我们平时写的DAO层接口,里面是一些数据库操作的接口方法。)

  1. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
  2. return this.mapperRegistry.getMapper(type, sqlSession);
  3. }

我们看到这个configuration的getMapper方法里调用的是mapperRegistry的getMapper方法,参数依然是类型对象和sqlSession。我们要先来看下这个 MapperRegistry 是什么。

  1. public class MapperRegistry {
  2. private final Configuration config;
  3. private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap();
  4.  
  5. public MapperRegistry(Configuration config) {
  6. this.config = config;
  7. }
  8. ....
  9. }

从这里我们可以知道其实这个MapperRegistry就是保持了一个Configuration对象和一个HashMap,而这个HashMap的key是类型对象,value是MapperProxyFactory。我们这里先不管MapperProxyFactory是什么东西,我们现在只需要知道MapperRegistry是这么一个东西就可以了。这里有人会问MapperRegistry对象是怎么来的,这里是在初始化Configuration对象时初始化了这个MapperRegistry对象的,具体代码可以看configuration对象的初始化。

接下来我们继续看下这个MapperRegistry的getMapper方法。

  1. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
  2. MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory)this.knownMappers.get(type);
  3. if (mapperProxyFactory == null) {
  4. throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
  5. } else {
  6. try {
  7. return mapperProxyFactory.newInstance(sqlSession);
  8. } catch (Exception var5) {
  9. throw new BindingException("Error getting mapper instance. Cause: " + var5, var5);
  10. }
  11. }
  12. }

这里我们可以看到从knownMappers中获取key为类型对象的MapperProxyFactory对象。然后调用MapperProxyFactory对象的newInstance方法返回,newInstance方法传入sqlSession对象。到这里我们可能看不出什么端倪,那我们就继续往下看这个newInstance方法做的什么事情吧。

  1. public class MapperProxyFactory<T> {
  2. private final Class<T> mapperInterface;
  3. private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap();
  4.  
  5. public MapperProxyFactory(Class<T> mapperInterface) {
  6. this.mapperInterface = mapperInterface;
  7. }
  8.  
  9. public Class<T> getMapperInterface() {
  10. return this.mapperInterface;
  11. }
  12.  
  13. public Map<Method, MapperMethod> getMethodCache() {
  14. return this.methodCache;
  15. }
  16.  
  17. protected T newInstance(MapperProxy<T> mapperProxy) {
  18. // 新建一个mapperProxy的代理类
  19. return Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{this.mapperInterface}, mapperProxy);
  20. }
  21.  
  22. public T newInstance(SqlSession sqlSession) {
  23. MapperProxy<T> mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);
  24. return this.newInstance(mapperProxy);
  25. }
  26. }

这里我们可以看到MapperProxyFactory直接new了一个MapperProxy对象,然后调用另外一重载的newInstance方法传入MapperProxy对象。这里我们可以看出一些东西了,通过调用Proxy.newProxyInstance动态代理了我们的mapperProxy对象!这里的mapperInterface即我们的dao层(持久层)接口的类型对象。

所以得出总结:我们通过sqlSesssion.getMapper(clazz)得到的Mapper对象是一个mapperProxy的代理类!这点印证了结论中说的getMapper其实得到的是mapper的方法的代理。

由于代理,我每次在自己的程序里执行 mapper 中的方法,都会走 MapperProxy中的invoke方法。我们来看看 MapperProxy 的 invoke方法中。

  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.  
  7. public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
  8. this.sqlSession = sqlSession;
  9. this.mapperInterface = mapperInterface;
  10. this.methodCache = methodCache;
  11. }
  12. ....
  13. }

MapperProxy必然实现了InvocationHandler接口。所以当我们调用我们的持久层接口的方法时,必然就会调用到这个MapperProxy对象的invoke方法。这属于JDK的代理的知识。所以接下来我们进入这个方法看看具体mybatis为我们做了什么。

  1. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  2. if (Object.class.equals(method.getDeclaringClass())) {
  3. // 如果是toString, equals之类的方法,直接执行
  4. try {
  5. return method.invoke(this, args);
  6. } catch (Throwable var5) {
  7. throw ExceptionUtil.unwrapThrowable(var5);
  8. }
  9. } else {
  10. // 从缓存的 MapperMethod 中拿,如果没有,就新建一个,并且放入缓存对象
  11. MapperMethod mapperMethod = this.cachedMapperMethod(method);
  12. // mapperMethod 执行
  13. return mapperMethod.execute(this.sqlSession, args);
  14. }
  15. }

看看 cachedMapperMethod 方法:

  1. private MapperMethod cachedMapperMethod(Method method) {
  2. MapperMethod mapperMethod = (MapperMethod)this.methodCache.get(method);
  3. if (mapperMethod == null) {
  4. mapperMethod = new MapperMethod(
  5. this.mapperInterface, method, this.sqlSession.getConfiguration());
  6. this.methodCache.put(method, mapperMethod);
  7. }
  8.  
  9. return mapperMethod;
  10. }

从代码中我们可以看到前面做了一个判断,这个判断主要是遇到像toString方法或者equals方法时,可以正常调用。然后我们可以看到它调用cachedMapperMethod返回MapperMethod对象,接着就执行这个MapperMethod对象的execute方法。这个cachedMapperMethod方法主要是能缓存我们使用过的一些mapperMethod对象,方便下次使用。

MapperMethod

mybatis的动态代理机制的重点来了!像其他所有的类一样,MapperMethod也有实例化的过程和重要的execute方法。

MapperMethod实例化过程

MapperMethod对象主要是获取mapper中的某个方法对应的sql命令和执行相应SQL操作等的处理,非常重要。通过 mapperMethod,mapper中的方法就与 xml 中定义的sql语句结合起来了。

  1. public class MapperMethod {
  2. private final MapperMethod.SqlCommand command;
  3. private final MapperMethod.MethodSignature method;
  4.  
  5. public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) {
  6. // 获取 SqlCommand 类
  7. // 其中主要记录了mapper中的method方法的名称与sql执行类型(INSERT还是SELECT)的映射
  8. // 方便之后在 execute 的时候根据sql执行类型来执行对应的 switch case 逻辑
  9. this.command = new MapperMethod.SqlCommand(config, mapperInterface, method);
  10.  
  11. // 获取 MethodSignature 类,其中记录了method方法的返回类型,以及其他一些必要的、和sql执行相关的信息
  12. // 这些信息有很多,大部分是从configuration中处理得到的
  13. // 如何处理得到这些信息,以及如何使用这些信息,属于另一个知识范畴了
  14. // 在这里我们可以简单地理解为获取了method方法的返回类型等信息,在之后 execute的时候要使用的
  15. this.method = new MapperMethod.MethodSignature(config, mapperInterface, method);
  16. }
  17. ....
  18. }

MapperMethod execute方法

来看一下 mapperMethod 对象的 execute方法:

  1. public Object execute(SqlSession sqlSession, Object[] args) {
  2. Object param;
  3. Object result;
  4. switch(this.command.getType()) {
  5. case INSERT:
  6. param = this.method.convertArgsToSqlCommandParam(args);
  7. result = this.rowCountResult(sqlSession.insert(this.command.getName(), param));
  8. break;
  9. case UPDATE:
  10. param = this.method.convertArgsToSqlCommandParam(args);
  11. result = this.rowCountResult(sqlSession.update(this.command.getName(), param));
  12. break;
  13. case DELETE:
  14. param = this.method.convertArgsToSqlCommandParam(args);
  15. result = this.rowCountResult(sqlSession.delete(this.command.getName(), param));
  16. break;
  17. case SELECT:
  18. if (this.method.returnsVoid() && this.method.hasResultHandler()) {
  19. this.executeWithResultHandler(sqlSession, args);
  20. result = null;
  21. } else if (this.method.returnsMany()) {
  22. result = this.executeForMany(sqlSession, args);
  23. } else if (this.method.returnsMap()) {
  24. result = this.executeForMap(sqlSession, args);
  25. } else if (this.method.returnsCursor()) {
  26. result = this.executeForCursor(sqlSession, args);
  27. } else {
  28. param = this.method.convertArgsToSqlCommandParam(args);
  29. result = sqlSession.selectOne(this.command.getName(), param);
  30. }
  31. break;
  32. case FLUSH:
  33. result = sqlSession.flushStatements();
  34. break;
  35. default:
  36. throw new BindingException("Unknown execution method for: " + this.command.getName());
  37. }
  38.  
  39. if (result == null && this.method.getReturnType().isPrimitive() && !this.method.returnsVoid()) {
  40. throw new BindingException("Mapper method '" + this.command.getName() + " attempted to return null from a method with a primitive return type (" + this.method.getReturnType() + ").");
  41. } else {
  42. return result;
  43. }
  44. }

我们可以清晰的看到,这里针对数据库的增删改查做了对应的操作,这里我们可以看下查询操作。我们可以看到这里针对方法的不同返回值作了不同的处理,我们以其中一种情况来举例:

  1. // SELECT语句,当所有情况都不满足时,执行下面的逻辑
  2. param = this.method.convertArgsToSqlCommandParam(args);
  3. result = sqlSession.selectOne(this.command.getName(), param);

这里我们可以看到它将方法参数类型转换成数据库层面上的参数类型,最后调用sqlSession对象的selectOne方法执行。所以我们看到最后还是回到sqlSession对象上来,符合前面所说的sqlSession是mybatis提供的与数据库交互的唯一对象。

接下来我们看下这个selectOne方法做了什么事,这里我们看的是defaultSqlSession的selectOne方法。

  1. public <T> T selectOne(String statement, Object parameter) {
  2. List<T> list = this.selectList(statement, parameter);
  3. if (list.size() == 1) {
  4. return list.get(0);
  5. } else if (list.size() > 1) {
  6. throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
  7. } else {
  8. return null;
  9. }
  10. }

我们看到它调用selectList方法,通过取返回值的第一个值作为结果返回。那么我们来看下这个selectList方法。

  1. public <E> List<E> selectList(String statement, Object parameter) {
  2. return this.selectList(statement, parameter, RowBounds.DEFAULT);
  3. }
  4.  
  5. public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
  6. List var5;
  7. try {
  8. MappedStatement ms = this.configuration.getMappedStatement(statement);
  9. var5 = this.executor.query(ms, this.wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
  10. } catch (Exception var9) {
  11. throw ExceptionFactory.wrapException("Error querying database. Cause: " + var9, var9);
  12. } finally {
  13. ErrorContext.instance().reset();
  14. }
  15.  
  16. return var5;
  17. }

我们可以看到这里调用了executor的query方法,我们再进入到query里看看。这里我们看的是BaseExecutor的query方法,先不看缓存的Executor中的query方法。注意看getBoundSql这个方法:

  1. public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
  2. // getBoundSql方法非常重要
  3. // 作用是根据传入的sql参数,加上configuration和mappedStatement中的信息,组装一个 BoundSql 给你
  4. // 具体怎么组装的,又有一段复杂的逻辑,其中也牵涉到了缓存技术,在这里不展开研究
  5. // 在这里只要简单的认为我们得到了 mappedStatement 对应的 BoundSql
  6. // 里面有sql语句和sql参数,是一个完整的sql了,终于马上可以去数据库执行一把了
  7. BoundSql boundSql = ms.getBoundSql(parameter);
  8.  
  9. CacheKey key = this.createCacheKey(ms, parameter, rowBounds, boundSql);
  10. return this.query(ms, parameter, rowBounds, resultHandler, key, boundSql);
  11. }
  12.  
  13. ...
  14.  
  15. public <E> List<E> query( MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
  16. ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
  17. if (this.closed) {
  18. throw new ExecutorException("Executor was closed.");
  19. } else {
  20. if (this.queryStack == 0 && ms.isFlushCacheRequired()) {
  21. this.clearLocalCache();
  22. }
  23.  
  24. List list;
  25. try {
  26. ++this.queryStack;
  27. list = resultHandler == null ? (List)this.localCache.getObject(key) : null;
  28. if (list != null) {
  29. this.handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
  30. } else {
  31. list = this.queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql); // 重点方法
  32. }
  33. } finally {
  34. --this.queryStack;
  35. }
  36.  
  37. if (this.queryStack == 0) {
  38. Iterator i$ = this.deferredLoads.iterator();
  39.  
  40. while(i$.hasNext()) {
  41. BaseExecutor.DeferredLoad deferredLoad = (BaseExecutor.DeferredLoad)i$.next();
  42. deferredLoad.load();
  43. }
  44.  
  45. this.deferredLoads.clear();
  46. if (this.configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
  47. this.clearLocalCache();
  48. }
  49. }
  50.  
  51. return list;
  52. }
  53. }

boundSql中到底有些什么,我们来看看:

  1. -- boundSql 中的sql语句如下
  2. SELECT id, job_name, job_group_name, trigger_name, trigger_group_name, cron, status, create_time, update_time, operate_id, operate_name FROM cron_quartz_record WHERE job_group_name = ?

回到代码跟踪,进入queryFromDatabase这个重点方法:

  1. private <E> List<E> queryFromDatabase(
  2. MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler,
  3. CacheKey key, BoundSql boundSql) throws SQLException {
  4. this.localCache.putObject(key, ExecutionPlaceholder.EXECUTION_PLACEHOLDER);
  5.  
  6. List list;
  7. try {
  8. list = this.doQuery(ms, parameter, rowBounds, resultHandler, boundSql); // 重点方法
  9. } finally {
  10. this.localCache.removeObject(key);
  11. }
  12.  
  13. this.localCache.putObject(key, list);
  14. if (ms.getStatementType() == StatementType.CALLABLE) {
  15. this.localOutputParameterCache.putObject(key, parameter);
  16. }
  17.  
  18. return list;
  19. }

我们看到有个一个方法doQuery,进入方法看看做了什么。点进去后我们发现是抽象方法,我们选择simpleExecutor子类查看实现。

  1. public <E> List<E> doQuery( MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  2. Statement stmt = null;
  3.  
  4. List var9;
  5. try {
  6. Configuration configuration = ms.getConfiguration();
  7. StatementHandler handler = configuration.newStatementHandler(this.wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
  8. stmt = this.prepareStatement(handler, ms.getStatementLog());
  9. var9 = handler.query(stmt, resultHandler);
  10. } finally {
  11. this.closeStatement(stmt);
  12. }
  13.  
  14. return var9;
  15. }
  16.  
  17. private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
  18. Connection connection = this.getConnection(statementLog);
  19. Statement stmt = handler.prepare(connection, this.transaction.getTimeout());
  20. handler.parameterize(stmt);
  21. return stmt;
  22. }

我们看到了熟悉的 statement 和 prepareStatement 方法,这很像 JDBC 中的 preparedStatement。我们也看到了 query 方法,这很像 JDBC 中的 executeQuery。我们知道,代码跟踪的旅程快要结束了。

我们可以看到通过configuration对象的newStatementHandler方法构建了一个StatementHandler,然后在调用prepareStatement方法中获取连接对象,通过StatementHandler得到Statement对象。另外我们注意到在获取了Statement对象后调用了parameterize方法。如果继续跟踪下去,我们可以发现会调用ParameterHandler对象的setParameters方法去处理我们的参数。所以这里的prepareStatement方法主要使用了StatementHandler和ParameterHandler对象帮助我们处理语句集和参数的处理。最后还调用了StatementHandler的query方法,我们继续跟踪下去。

这里我们进入到PreparedStatementHandler这个handler查看代码。

  1. public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
  2. c ps = (PreparedStatement)statement;
  3. ps.execute(); // 操作数据库
  4. return this.resultSetHandler.handleResultSets(ps);
  5. }

看到这里,我们终于找到了操作数据库的地方了,就是ps.execute()这句代码。底层我们可以发现就是原始的JDBC!然后将这个执行后的PreparedStatement交给resultSetHandler处理结果集,最后返回我们需要的结果集。

总结

我们总结一下mybatis的动态代理机制:

sqlSession是唯一访问数据库的渠道,通过sqlSession.getMapper方法,得到到是mapper的代理类,已经不是我们在预先写好的mapper接口了。当我们执行mapper中的方法,在代理类中的执行逻辑如下:

  1. if 判断
  2. else if 判断
  3. cachedMapperMethod 重点方法
  4. mapperMethod.execute 重点方法,实际执行的方法

在mapperMethod对象中,通过getBoundSql方法,早早的为你准备好了mapper中的方法实际对应的sql语句,而且是可以被执行的preparedStatement。这样一来,我们在业务代码中执行mapper中的方法,实际上就是去数据库中执行对应的sql语句了。

参考资料

  • https://segmentfault.com/a/1190000015117926
  • http://www.mybatis.org/mybatis-3/zh/getting-started.html

创作时间:05/22/2019 21:23

mybatis代理机制讲解的更多相关文章

  1. 从头捋了一遍 Java 代理机制,收获颇丰

    尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 「CS-Wiki」Gitee ...

  2. Java 动态代理机制详解

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  3. java的动态代理机制详解

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  4. java Proxy(代理机制)

    我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的我们的功能,我们更需要学习 ...

  5. mybatis源代码分析:mybatis延迟加载机制改进

    在上一篇博客<mybatis源代码分析:深入了解mybatis延迟加载机制>讲诉了mybatis延迟加载的具体机制及实现原理. 可以看出,如果查询结果对象中有一个属性是需要延迟加载的,那整 ...

  6. Java的动态代理机制详解(转)

    在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...

  7. 详解Java动态代理机制

    之前介绍的反射和注解都是Java中的动态特性,还有即将介绍的动态代理也是Java中的一个动态特性.这些动态特性使得我们的程序很灵活.动态代理是面向AOP编程的基础.通过动态代理,我们可以在运行时动态创 ...

  8. (转)java的动态代理机制详解

    原文出自:http://www.cnblogs.com/xiaoluo501395377/p/3383130.html 在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一 ...

  9. [转载] java的动态代理机制详解

    转载自http://www.cnblogs.com/xiaoluo501395377/p/3383130.html 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代 ...

随机推荐

  1. Merge into 语句实例

    /*Merge into 详细介绍MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配 ...

  2. 在网页中动态地给表格添加一行内容--HTML+CSS+JavaScript

    需求描述: 用户在页面上点击按钮,可以把文本框中的数据在表格的新的一行中显示,具体表现如下图: 如果如果输入框内容有一项为空,弹出对话框‘请将数据填入完全 步骤: 1.按钮注册单击事件 2.获取并判断 ...

  3. 微信公众平台开发(57)Emoji表情符号 【转发】

    微信公众平台开发(57)Emoji表情符号   微信公众平台开发 微信公众平台开发模式 企业微信公众平台 Emoji表情符号 作者:方倍工作室 地址:http://www.cnblogs.com/tx ...

  4. centos 7 常用yum源配置

    使用centos系统最熟悉的莫过于yum命令,yum命令可以让安装软件变得那么简单,编译安装的依赖关系大部分都会解决. 工具/原料   centos 7 wget yum 方法/步骤     什么是y ...

  5. 解决MybatisGenerator多次运行mapper生成重复内容

    MybatisGenerator插件是Mybatis官方提供的,这个插件存在一个固有的Bug,即当第一次生成了Mapper.xml之后,再次运行会导致Mapper.xml生成重复内容,而影响正常的运行 ...

  6. [Python] Python 学习记录(2)

    1.range(x,y) [x,y) >>> range(0,4) #0,1,2,3 >>> range(1,4) #1,2,3 2.dics dics.get(k ...

  7. 快学Scala 第九课 (伴生对象和枚举)

    Scala没有静态方法和静态字段, 你可以用object这个语法结构来达到同样的目的. 对象的构造器只有在第一次被使用时才调用. 伴生对象apply方法: 类和它的伴生对象可以互相访问私有特性,他们必 ...

  8. iOS性能优化-异步绘制

    参考地址:https://blog.ibireme.com/2015/11/12/smooth_user_interfaces_for_ios/ 很久以前就看过这篇文章,但是也只是看过就过了,没有去整 ...

  9. 如何正确遍历删除List中的元素(普通for循环、增强for循环、迭代器iterator、removeIf+方法引用)

    遍历删除List中符合条件的元素主要有以下几种方法: 普通for循环 增强for循环 foreach 迭代器iterator removeIf 和 方法引用 其中使用普通for循环容易造成遗漏元素的问 ...

  10. netty源码解解析(4.0)-24 ByteBuf基于内存池的内存管理

    io.netty.buffer.PooledByteBuf<T>使用内存池中的一块内存作为自己的数据内存,这个块内存是PoolChunk<T>的一部分.PooledByteBu ...