![](/img/trans.png)
[英]AspectJ module dependency with Maven - How get working the Inter-type declarations methods of a dependency module
[英]What is a short example of inter-type declarations in AspectJ which demonstrates the usefulness of the approach?
我首先考虑使用ITD来定义private static final Logger logger = ...
对于一些不相关的情况,但它看起来不像使用它作为演示示例的明显改进。
是否有一些标准/建议的ITD使用示例,人们应该将其用于教学目的?
我想在Adam的回答中再添一个例子。
使用方面是一种改进软件模块化的方法,以及OOP,除了OOP“垂直”应用,而AOP“水平”应用。
因此,虽然可以使用建议来“水平”修改响应某些标准的所有方法的行为,但建议中的代码也需要一些数据,而ITD可以在“水平”的同时添加这些字段,同时包含在单个编译单元(方面)中的相关数据和代码。
由于您是通过ITD字段添加数据,因此您可能还希望提供一些方法来操作这些字段,例如setter / getter。
我试着将它改写几次,我希望它很容易理解。
举个例子,假设你有一个图形编辑程序,用户可以在其中创建,删除和显示多个形状(非常规范的呃?:D)。
形状有很多数据,例如它们有颜色,坐标等......它们正确地属于类本身。 但是,由于这是一个Web 3.0应用程序,您希望用户能够在单个绘图上进行协作,这意味着每个形状必须知道它何时被更改,如果已经与其他用户通知形状已更改,那么它被一些实际拖动它的用户锁定等等。
你可以在简单的OOP中实现它,在Shape中实现相关的部分或任何你的根类,但是你将使用像“this.setDirty(true)”之类的调用或类似的东西来污染所有的setter。
相反,你决定去AOP并在一个方面实现它。 在这种情况下,您可以在所有setter之后轻松添加建议,但是如果要优化网络协议仅发送更改的增量,则还需要存储“脏”布尔值,甚至是更改的属性列表。
你可以实现它
public aspect ShapesAreDirtyThings {
private boolean Shape.dirty;
after(Shape s) : execution(* Shape+.set*(..)) {
s.dirty = true;
// Set some other global field, start a thread to communicate
// to other users about the change, whatever ..
}
public boolean Shape.isDirty() {
return s.dirty;
}
public boolean Shape.findDirtyChildren() { // whatever
}
对于常见的AOP来说,这是你无法做到的,只需要封装在单个编译单元的建议,该建议所需的数据以及最终将对该数据进行操作的方法。
Java不支持多重继承或mixin。 但是对于ITD,你可以像Scins 一样使Scala像Traits或Ruby。
这是一个利用JPA和Spring的Configurable的例子:
接口:
package com.evocatus.aop;
public interface DomainNotify {
public void postPersist();
public void postUpdate();
public void postRemove();
}
AspectJ ITD:
package com.evocatus.aop;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import javax.persistence.Transient;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import com.evocatus.service.DomainNotifyService;
privileged aspect DomainNotifyAdvice {
@JsonIgnore
@Transient
@Autowired
transient DomainNotifyService DomainNotify.domainNotifyService;
@PostPersist
public void DomainNotify.postPersist() {
this.domainNotifyService.publishSave(this);
}
@PostUpdate
public void DomainNotify.postUpdate() {
this.domainNotifyService.publishUpdate(this);
}
@PostRemove
public void DomainNotify.postRemove() {
this.domainNotifyService.publishRemove(this);
}
}
现在任何实现 DomainNotify的类都将获得编织的ITD方法。
@Entity
@Configurable
public class MyJPAObject implements DomainNotify {
}
看看Spring ROO吧。 这是一个快速的开发。 使用Spring的env。 我把它看作Ruby on Rails但是在带有Spring的java中;-)
它使用ITD来生成getter,setter,toString等...基于您在创建类时定义的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.