7、target、this、within

写几个类加以验证


image.png
没有使用这三个关键字

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();
        }
        
    }

}

输出:


image.png

image.png

HelloAspect.aj

package aspectj;

public aspect HelloAspect {
    

    pointcut EatPointCut() : call(* eat(..))&&target(Person);
    before() : EatPointCut() {
        System.out.println("Entering " + thisJoinPoint.getSourceLocation());
    }
    
}



输出:


image.png

修改HelloAspect.aj

package aspectj;

public aspect HelloAspect {
    

    pointcut EatPointCut() : call(* eat(..))&&target(Person);
    before() : EatPointCut() {
        System.out.println("Entering " + thisJoinPoint.getSourceLocation());
    }
    
}



输出:


image.png

为了效果明显,我们修改下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());
    }
    
}


输出:


image.png

修改HelloAspect.aj

package aspectj;

public aspect HelloAspect {
    

    pointcut EatPointCut() : call(* eat(..))&&this(Person);
    before() : EatPointCut() {
        System.out.println("Entering " + thisJoinPoint.getSourceLocation());
    }
    
}



image.png
所以说,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());
    }
    
}



输出:


image.png

image.png

image.png
within

HelloAspect.aj

package aspectj;

public aspect HelloAspect {
    

    pointcut EatPointCut() : execution(* eat(..))&&within(Person);
    before() : EatPointCut() {
        System.out.println("Entering " + thisJoinPoint.getSourceLocation());
    }
    
}



输出结果:


image.png

修改HelloAspect.aj


package aspectj;

public aspect HelloAspect {
    

    pointcut EatPointCut() : call(* eat(..))&&target(Person)&&this(HelloWorld);
    before() : EatPointCut() {
        System.out.println("Entering " + thisJoinPoint.getSourceLocation());
    }
    
}





image.png

HelloAspect.aj

package aspectj;

public aspect HelloAspect {
    

    pointcut EatPointCut() : call(* eat(..))&&target(Person)&&within(HelloWorld);
    before() : EatPointCut() {
        System.out.println("Entering " + thisJoinPoint.getSourceLocation());
    }
    
}



输出:


image.png

修改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());
    }
    
}



image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容