实体方面(春季)
我在定义我的方面时遇到了一些问题。我有一堆实体,我想在其中分析 get-methods,所以我编写了以下切入点和方法,
@Pointcut("execution(* tld.myproject.data.entities.*.get*()")
public void getEntityProperty() {}
@Around("getEntityProperty()")
public Object profileGetEntityProperty(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
String name = pjp.getSignature().getName();
Object output = pjp.proceed();
long elapsedTime = System.currentTimeMillis() - start;
if(elapsedTime > 100)
System.err.println("profileGetEntityProperty: Entity method " + name + " execution time: " + elapsedTime + " ms.");
return output;
}
我在配置中打开了编织,并将方面编织到业务层工作中很好。我的切入点写得正确吗?或者实体中是否有某些东西使它们不可编织? (我的实体在类定义之前以 @Entity 为前缀)
Cheers
Nik
I'm having a bit of a problem defining my aspects. I've got a bunch of entities that I'd like to profile the get-methods in, so I've written the following pointcut and method
@Pointcut("execution(* tld.myproject.data.entities.*.get*()")
public void getEntityProperty() {}
@Around("getEntityProperty()")
public Object profileGetEntityProperty(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
String name = pjp.getSignature().getName();
Object output = pjp.proceed();
long elapsedTime = System.currentTimeMillis() - start;
if(elapsedTime > 100)
System.err.println("profileGetEntityProperty: Entity method " + name + " execution time: " + elapsedTime + " ms.");
return output;
}
I've got weaving turned on in my configuration, and aspects weaving into the business layer work just fine. Is my pointcut correctly written? Or is there something about entities that make them non-weavable? (my entity is prefixed with @Entity before the class definition)
Cheers
Nik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您只差一个括号!
如果您使用 Eclipse,我建议使用 AspectJ 编译进行开发-时间编织。这是最简单的方法。
使用 AJDT 插件,您可以获得很多帮助!我刚刚粘贴了你的切入点并收到了编译错误。加了一个括号就成功了!
AJDT 插件视觉支持的屏幕截图:
getHello() 方法左侧的橙色箭头表示它是由周围的建议建议。请参阅此处 一个更大的例子。
You're only a parenthesis away actually!
If you're using Eclipse, I will recommend developing with AspectJ compile-time weaving. It's the simplest way.
With the AJDT plugin, you get lots of help! I just pasted in your pointcut and got an compilation error. Added a parenthesis and it worked!
Screenshot of visual support with the AJDT plugin:
The orange arrow to the left of the getHello() method indicates that is's advised by an around advice. See here for a larger example.
是的,有。这些实体是由您使用 new 运算符创建的,因此它们不是 spring 上下文的一部分。
如果你想使用它,你需要启用编织(我更喜欢通过
加载时间),并用 <代码>@可配置。我个人不喜欢这种做法。唉,没有任何替代方案如此通用。如果您的持久性提供程序是 Hibernate,您可以创建实体的自定义代理 - 看到这里,不过这样就更尴尬了。
Yes, there is. The entities are created by you, using the
new
operator, and hence they are not part of the spring context.If you want to use this, you'd need to enable weaving (I prefer load-time via
<context:load-time-weaver/>
), and to annotate your entity classes with@Configurable
.I, personally, wouldn't prefer this practice. Alas, there aren't any alternatives that are so generic. If your persistence provider is Hibernate, you can create a custom proxy of your entities - see here, but this is even more awkward.
请注意,@Configurable 适用于编译时编织。通过 @Configurable 自动装配实体的缺点是,当通过 Hibernate 从数据库中检索所述实体时,它们“似乎”不起作用。通过“新”调用,是的。但就在最近,考虑到一个非常奇怪的从实体进行持久化的请求,使用“new”的单元测试工作得很好,但 hibernate(或 ehcache)加载的实体导致了自动装配属性的 NPE。已经是深夜了;所以你不妨自己做一些测试。:)只是传递我最近的经验。希望有帮助。
Just a note that @Configurable works with compile-time weaving. The drawback to autowiring entities via @Configurable is that they don't 'seem' to work when retrieving said entities from a database via Hibernate. Via a 'new' invocation, yes. But just recently, given a very weird request to do persistence from an entity, a unit test using 'new' worked perfectly, yet hibernate (or ehcache) loaded-entities resulted in NPEs for the autowired property. It was late at night; so you may wish to do some testing yourself.:) Just passing along my recent experience. Hope it helps.