写几个类加以验证
没有使用这三个关键字
Person.java
package aspectj;
public interface Person {
void eat();
}
Student.java
package aspectj;
public class Student implements Person{
public void print()
{
System.out.println("student");
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("student-----eat");
}
}
Teacher.java
package aspectj;
public class Teacher implements Person{
public void eat() {
// TODO Auto-generated method stub
System.out.println("teacher----eat");
}
}
HelloAspect.aj
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut() : call(* eat(..));
before() : EatPointCut() {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}
HelloWorld.java
package aspectj;
import java.util.ArrayList;
import java.util.List;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorld helloWorld=new HelloWorld();
List<Person> lists=new ArrayList();
lists.add(new Teacher());
lists.add(new Student());
helloWorld.eat(lists);
}
public void eat(List<Person> lists)
{
for(Person p:lists)
{
p.eat();
}
}
}
输出:
HelloAspect.aj
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut() : call(* eat(..))&&target(Person);
before() : EatPointCut() {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}
输出:
修改HelloAspect.aj
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut() : call(* eat(..))&&target(Person);
before() : EatPointCut() {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}
输出:
为了效果明显,我们修改下Teacher类,分别测试target和this
Teacher.java
package aspectj;
public class Teacher implements Person{
public void eat() {
// TODO Auto-generated method stub
System.out.println("teacher----eat");
eat("草");
}
private void eat(String food)
{
System.out.println("food----"+food);
}
}
HelloAspect.aj
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut() : call(* eat(..))&&target(Person);
before() : EatPointCut() {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}
输出:
修改HelloAspect.aj
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut() : call(* eat(..))&&this(Person);
before() : EatPointCut() {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}
所以说,target(Person)是说拦截的方法是属于Person的,而this(Person)是说拦截的方法是在Person类中被调用的。
修改HelloAspect.aj将call改为execution
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut() : execution(* eat(..))&&this(Person);
before() : EatPointCut() {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}
输出:
within
HelloAspect.aj
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut() : execution(* eat(..))&&within(Person);
before() : EatPointCut() {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}
输出结果:
修改HelloAspect.aj
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut() : call(* eat(..))&&target(Person)&&this(HelloWorld);
before() : EatPointCut() {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}
HelloAspect.aj
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut() : call(* eat(..))&&target(Person)&&within(HelloWorld);
before() : EatPointCut() {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}
输出:
修改HelloAspect.aj
package aspectj;
public aspect HelloAspect {
pointcut EatPointCut(Person p,HelloWorld h) : call(* eat(..))&&target(p)&&this(h);
before(Person p,HelloWorld h) : EatPointCut(p,h) {
System.out.println("Entering " + thisJoinPoint.getSourceLocation());
}
}