我有一个注释。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PartnerProxy {
}
并提供一些建议
@Component
@Aspect
public class AnnotationAdvice {
@Around("@annotation(PartnerProxy)")
public Object pc(ProceedingJoinPoint joinPoint) throws Throwable {
return joinPoint.proceed();
}
}
我想要代理的Bean
public class OuterServiceProxy {
private IRoomStatusService service;
... another properties
String getRemoteHourRoomStatus(){
return service.echo();
}
@PartnerProxy
public void hello() {
}
...getters & setters
有一个属性IRoomStatusService service
,这是关键点。
首先如果我在Spring XML文件中声明了OuterServiceProxy
<bean id="outerServiceProxy" class="aop.jg.OuterServiceProxy">
<property name="service" ref="service"/>
</bean>
当调用outerServiceProxy.getRemoteHourRoomStatus()
方法时,会抛出NPE异常。我调试到了该行代码[1]。
String getRemoteHourRoomStatus(){
return service.echo(); [1]
}
service
为空,但是 outerServiceProxy
实际上是由 Cglib 增强的 OuterServiceProxy$$EnhancerByCGLIB$$b0b63bb6
,但是似乎 outerServiceProxy
直接调用了 String getRemoteHourRoomStatus()
而不是通过回调和调用 TargetSource,因此 service
为空。但这没有意义!当我添加 public 修饰符 public String getRemoteHourRoomStatus()
时,一切都很好。
更奇怪的是,没有 public
修饰符,在我的个人电脑上同样的代码运行良好,但在公司的测试环境中却抛出 NPE。