【发布时间】:2015-07-20 13:26:12
【问题描述】:
我正在尝试使用 AspectJ 和 cflow 开发一个简单的虫洞。
我有两个值对象 Person 和 Account 如下所示
帐户
public class Account {
private final String sortCode;
private final String accountNumber;
private final int balance;
public Account(String sortCode, String accountNumber, int balance) {
this.sortCode = sortCode;
this.accountNumber = accountNumber;
this.balance = balance;
}
public String getSortCode() {
return sortCode;
}
public String getAccountNumber() {
return accountNumber;
}
public int getBalance() {
return balance;
}
@Override
public String toString() {
return "Account{" +
"sortCode='" + sortCode + '\'' +
", accountNumber='" + accountNumber + '\'' +
", balance=" + balance +
'}';
}
}
人
public class Person {
private final String name;
private final String address;
private final int age;
public Person(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", age=" + age +
'}';
}
}
还有三个“层”
A 层
public class Layer_A {
public void doSomeLevelAprocess(Person person){
System.out.println("doSomeLevelAprocess person " + person);
new Layer_B().doSomeLevelBprocess();
}
}
B 层
public class Layer_B {
public void doSomeLevelBprocess(){
System.out.println("doSomeLevelBprocess");
new Layer_C().doSomeLevelCprocess(new Account("123456", "87654321", 1001));
}
}
C层
public class Layer_C {
public void doSomeLevelCprocess(Account account){
System.out.println("doSomeLevelCprocess " );
}
}
我希望将 Person 对象传递给 Layer_A 方法
doSomeLevelAprocess 提供于 Layer_C 方法 doSomeLevelCprocess
这可能吗?
我的方面看起来像这样一个
public aspect WormWhole {
pointcut callerSpace(Person person): execution(* Layer_A.*(..)) && this(person);
pointcut calleeSpace(Account account): this(account) && execution(public * Layer_C.*(..));
pointcut wormhole(Person person, Account account):
cflow(callerSpace(person)) && calleeSpace(account);
before(Person person, Account account):
wormhole(person, account){ **//WHAT DO I DO HERE?????** }
}
我是否正确选择了切入点 callerSpace 和 calleeSpace?
需要什么逻辑
before(Person person, Account account):
wormhole(person, account){ **//WHAT DO I DO HERE?????** }
更新
“可能重复”问题实际上并未说明如何实现虫洞,它只是显示了 AspectJ 虫洞的“模板”。此外,提到的问题答案不会尝试通过多层传递上下文,这是虫洞的全部点。
更新
当我将建议更改为:-
public aspect WormWhole {
pointcut callerSpace(Person person): execution(* Layer_A.*(..)) && this(person);
pointcut calleeSpace(Account account): this(account) && execution(public * Layer_C.*(..));
pointcut wormhole(Person person, Account account):
cflow(callerSpace(person)) && calleeSpace(account);
before(Person person, Account account):
wormhole(person, account){
System.out.println("Wormhole - " + person);
System.out.println("Wormhole - " + account);
}
}
我收到这个输出:-
doSomeLevelAprocess person Person{name='aaa', address='aaa', age=19}
doSomeLevelBprocess
doSomeLevelCprocess
Process finished with exit code 0
这证明我的切入点不正确(我相信),但为什么????
更新
我添加了一个简单的切入点,效果很好
public aspect WormWhole {
pointcut callSayHello(): call(* Layer_A.*(..)) || call(* Layer_B.*(..)) || call(* Layer_C.*(..)) ;
pointcut callerSpace(Person person): execution(* Layer_A.*(..)) && this(person);
pointcut calleeSpace(Account account): this(account) && execution(public * Layer_C.*(..));
pointcut wormhole(Person person, Account account):
cflow(callerSpace(person)) && calleeSpace(account);
before(Person person, Account account):
wormhole(person, account){
System.out.println("Wormhole - " + person);
System.out.println("Wormhole - " + account);
}
after() : callSayHello() {
System.out.println("After call sayHello" + thisJoinPoint);
}
}
测试执行的输出
doSomeLevelAprocess person aaa
doSomeLevelBprocess
doSomeLevelCprocess account 87654321
After call sayHellocall(void Layer_C.doSomeLevelCprocess(Account))
After call sayHellocall(void Layer_B.doSomeLevelBprocess())
After call sayHellocall(void Layer_A.doSomeLevelAprocess(Person))
Process finished with exit code 0
我在IntelliJ 14.1.14 中使用CTW,aspectjrt-1.8.6.jar
【问题讨论】:
-
那个问题/答案没有解释如何达到预期的结果。此外,问题示例代码没有要通过的多层
-
我是回答另一个问题的人。我实际上并没有尝试运行您的代码,但乍一看,切入点看起来不错。至于您对级别数量的关注,这无关紧要——这就是虫洞模式的全部意义所在。至于你的问题“我在这里做什么?” - 好吧,这取决于你。例如,尝试打印这两个对象。如果你可以打印它们,你也可以用它们做任何其他事情。这仅取决于您想要实现的目标。不管你做什么,基本上你在
Account对象的执行上下文中使用Person对象——很酷,不是吗? -
@kriegaex 谢谢你的时间,这很酷,我希望我能让它工作!!! :(
-
我最早今晚(中欧时间)回家时可以运行你的代码。同时,一个快速的问题,只是为了仔细检查:你的项目中是否有其他(更简单的)方面?我只想排除编译、部署或加载时编织问题。您使用 CTW 还是 LTW?