如果多个类使用了多个注解,而且类之间有继承关系,那AOP效果就热闹了!以下图为例,Human类使用A1注解,Man类使用A2注解,Boy类没有显式使用注解。
形象比喻
@within() 和 @target的“作用域”有点像清朝的爵位和官位的“有效期”:
- @within() ,类似于爵位,是可以世袭的,老子跟着皇上出生入死,被分封的爵位儿子可以世袭,这叫封妻荫子。
- @target(),类似于官位,是不能世袭的,纨绔子弟,是不能让他做官的。
@Before("@target(com.javatpoint.jjk.A1)")
public void execute2() {
System.out.println("@target --- A1");
}
上述代码可以形象地理解为,谁的官位是A1,就在他的所有方法执行前进行增强,即打印@target — A1,显然,只有Human被封了官。这种官位是不能世袭的,所以,对Man和Boy无效。
@Before("@within(com.javatpoint.jjk.A2)")
public void execute3() {
System.out.println("@within --- A2");
}
上述代码可以形象地理解为,谁是A2爵位的受益者,就在他自己的方法,或者继承的方法执行前增强,即打印@within — A2,显然,Man、Boy都收益。Boy重写的方法不会增强,这不是世袭的,也好理解!
完整代码
package com.javatpoint.jjk;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface A1 {
}
package com.javatpoint.jjk;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface A2 {
}
package com.javatpoint.jjk;
@A1
public class Human {
public void say(String sentence) {
System.out.println("Human says:" + sentence);
}
public void run() {
System.out.println("Human runs." );
}
public void jump() {
System.out.println("Human jump." );
}
}
package com.javatpoint.jjk;
@A2
public class Man extends Human{
@Override
public void run() {
System.out.println("Man runs." );
}
public void testMan() {
System.out.println("Man testMan." );
}
}
package com.javatpoint.jjk;
public class Boy extends Man {
@Override
public void jump() {
System.out.println("Boy jump." );
}
public void test() {
System.out.println("Bot test...");
}
}
package com.javatpoint.jjk;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class HumanAspect
@Before("@within(com.javatpoint.jjk.A1)")
public void execute1() {
System.out.println("@within --- A1");
}
@Before("@target(com.javatpoint.jjk.A1)")
public void execute2() {
System.out.println("@target --- A1");
}
@Before("@within(com.javatpoint.jjk.A2)")
public void execute3() {
System.out.println("@within --- A2");
}
@Before("@target(com.javatpoint.jjk.A2)")
public void execute4() {
System.out.println("@target --- A2");
}
}
package com.javatpoint.jjk;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.javatpoint.jjk")
@EnableAspectJAutoProxy
public class HumanManager {
@Bean(name = "human")
public Human getHuman() {
return new Human();
}
@Bean(name = "man")
public Man getMan() {
return new Man();
}
@Bean(name = "boy")
public Boy getBoy() {
return new Boy();
}
}
package com.javatpoint;
import com.javatpoint.jjk.Boy;
import com.javatpoint.jjk.Human;
import com.javatpoint.jjk.HumanManager;
import com.javatpoint.jjk.Man;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class AopBeforeAdviceExampleApplication {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(HumanManager.class);
Human human = context.getBean("human", Human.class);
System.out.println("---------------------This is a Human.");
human.say("hello!");
human.jump();
human.run();
Human man = context.getBean("man", Man.class);
System.out.println("---------------------This is a Man.");
man.say("hello!");
man.jump();
man.run();
Human boy = context.getBean("boy", Boy.class);
System.out.println("---------------------This is a Boy.");
boy.say("hello!");
boy.jump();
boy.run();
}
}
---------------------This is a Human.
@within --- A1
@target --- A1
Human says:hello!
@within --- A1
@target --- A1
Human jump.
@within --- A1
@target --- A1
Human runs.
---------------------This is a Man.
@within --- A1
@target --- A2
Human says:hello!
@within --- A1
@target --- A2
Human jump.
@within --- A2
@target --- A2
Man runs.
---------------------This is a Boy.
@within --- A1
Human says:hello!
Boy jump.
@within --- A2
Man runs.
资料
- https://blog.youkuaiyun.com/qq_36917119/article/details/108123440
- https://blog.youkuaiyun.com/demon7552003/article/details/97601209
- https://blog.youkuaiyun.com/yangshangwei/article/details/77846974