【发布时间】:2013-08-30 13:21:39
【问题描述】:
我一直在编写一个方面来操作我的一些 JPA 实体 getter。它应该根据客户端区域设置重新格式化返回的文本。因为不是我所有的吸气剂都应该重新格式化,所以我引入了一个注释@ReFormat。
问题是,当我向 JPA 实体提供建议时,我的方面永远不会被拦截,但它在非 JPA 实体上工作正常(当我通过复制构造函数创建自己的实体对象时,它工作正常)。
我的注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface ReFormat {
}
我的方面:
@Aspect
public class ReFormatAspect {
@AfterReturning(pointcut = "@annotation(com.path.ReFormat)", returning = "response")
public Object formatter(JoinPoint joinPoint, Object response) {
return response;
}
}
现在这个方面在我的 MVC 控制器中被成功拦截(或在除 spring 数据之外的任何其他地方),但不适用于我的实体。
@Entity
@Table(name = "place", schema = "db")
public class TestEntity {
@Id
@Column(name = "id")
protected long id;
@Column(name = "about", columnDefinition = "TEXT DEFAULT NULL")
protected String about;
@ReFormat
public String getAbout() {
return this.about;
}
}
我希望在调用 getAbout 方法后会有一个切入点,但它不起作用。
鉴于上述事实,我认为 JPA (Hibernate) 正在覆盖任何可能由 CGLib 或 javassist 提供的拦截器。
注意:我的上下文中有这个
<context:annotation-config />
<context:spring-configured />
<aop:aspectj-autoproxy proxy-target-class="true" />
那么确切的问题是什么,我如何拦截实体内的任何方法?
我知道这应该是视图层的工作,但我仍然需要知道为什么:D
【问题讨论】:
标签: java spring aspectj spring-data spring-aop