Spring CGLIB增强bean中的Constructor-Injected字段为null

时间:2017-08-30 08:38:24

标签: java spring

我有这个存储库:

@Repository
public class MyRepository {

  private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

  public MyRepository(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
    this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
  }

  void doSomething() {
    namedParameterJdbcTemplate.update(...);
  }
}

Spring Boot 1.4.5(Spring Core& JDBC 4.3.7)正如预期的那样。调试doSomething()时,我可以看到MyRepository 的实例是普通对象,并设置了namedParameterJdbcTemplate

但是,只要我更新到Spring Boot 1.4.6(Spring Core& JDBC 4.3.8)或更高版本,MyRepository 的实例就是MyRepository$$EnhancerBySpringCGLIB namedParameterJdbcTemplatenull。我看到它被注入构造函数中,但是,此时我的存储库还没有CGLIB增强。

为什么?我无法在release 4.3.8 release notes中找到提示,所以我希望有人可以帮助我。

@Welever downvoting 请在评论中告诉我为什么你会贬低这个问题

4 个答案:

答案 0 :(得分:1)

即使我在最小的例子中无法复制它,我的bug report也被接受了。

答案 1 :(得分:1)

我有一些相关的东西。我的方法是 final ,因此,Spring使用的是CGLIB代理类,而不是默认行为(Spring AOP代理类)。因此,要访问类属性,我必须调用getXXX()方法,否则,每次尝试访问该属性时,该属性都将返回 null

答案 2 :(得分:0)

我有一个类似的问题...我通过将方法更改为public而不是protected来解决问题。

您尝试过

@Repository
public class MyRepository {

  private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

  public MyRepository(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
    this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
  }

  public void doSomething() {
    namedParameterJdbcTemplate.update(...);
  }
}

答案 3 :(得分:0)

// Check whether we only have one InvokerInterceptor: that is,
// no real advice, but just reflective invocation of the target.

if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {

  /* 
    We can skip creating a MethodInvocation: just invoke the target directly.
    Note that the final invoker must be an InvokerInterceptor, so we know
    it does nothing but a reflective operation on the target and no hot
    swapping or fancy proxying.
  */
  Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
  retVal = methodProxy.invoke(target, argsToUse);

} else {

  // We need to create a method invocation.
  retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
}
相关问题