关于Spring中@within和@target注解的区别,很多书籍中,把这2个注解的作用翻译成一样的了,或者是总结的不清晰。官方文档中原文为:
Any join point (method execution only in Spring AOP) where the target object has a
@Transactional
annotation:
@target(org.springframework.transaction.annotation.Transactional)
Any join point (method execution only in Spring AOP) where thedeclaredtype of the target object has an
@Transactional
annotation:
@within(org.springframework.transaction.annotation.Transactional)
重点是declared type,因此写了以下示例:
示例
注解:
@Retention(RetentionPolicy.RUNTIME)@Target( ElementType.TYPE)public @interface A1 {}@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface A2 {}
测试类型:
package demon.study.spring5.spring_aop.sample;@A1public 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 demon.study.spring5.spring_aop.sample;@A2public class Man extends Human{@Overridepublic void run(){System.out.println("Man runs." );}}
package demon.study.spring5.spring_aop.sample;public class Boy extends Man{@Overridepublic void jump(){System.out.println("Boy jump." );}}
package demon.study.spring5.spring_aop.sample;import org.springframework.context.annotation.Bean;import org.springframework.ponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration@ComponentScan@EnableAspectJAutoProxypublic 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();}}
Aspect:
package demon.study.spring5.spring_aop.sample;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.ponent;@Aspect@Componentpublic class HumanAspect {@Before("@within(demon.study.spring5.spring_aop.sample.A1)")public void execute1(){System.out.println("@within(demon.study.spring5.spring_aop.sample.A1)");}@Before("@target(demon.study.spring5.spring_aop.sample.A1)")public void execute2(){System.out.println("@target(demon.study.spring5.spring_aop.sample.A1)");}@Before("@within(demon.study.spring5.spring_aop.sample.A2)")public void execute3(){System.out.println("@within(demon.study.spring5.spring_aop.sample.A2)");}@Before("@target(demon.study.spring5.spring_aop.sample.A2)")public void execute4(){System.out.println("@target(demon.study.spring5.spring_aop.sample.A2)");}}
测试入口:
package demon.study.spring5.spring_aop.sample;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Configuration;public class Checker {public static void main(String[] args) {// TODO Auto-generated method stubtest1();}public static void test1(){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(demon.study.spring5.spring_aop.sample.A1)
@target(demon.study.spring5.spring_aop.sample.A1)
Human says:hello!
@within(demon.study.spring5.spring_aop.sample.A1)
@target(demon.study.spring5.spring_aop.sample.A1)
Human jump.
@within(demon.study.spring5.spring_aop.sample.A1)
@target(demon.study.spring5.spring_aop.sample.A1)
Human runs.
---------------------This is a Man.
@within(demon.study.spring5.spring_aop.sample.A1)
@target(demon.study.spring5.spring_aop.sample.A2)
Human says:hello!
@within(demon.study.spring5.spring_aop.sample.A1) #没有对应的target
@target(demon.study.spring5.spring_aop.sample.A2)
Human jump.
@within(demon.study.spring5.spring_aop.sample.A2) #A1注解未拦截不存在,因为runs方法属于Man类型,A1应用于Human。
@target(demon.study.spring5.spring_aop.sample.A2) #A1注解未拦截不存在,因为runs方法属于Man类型,A1应用于Human。
Man runs.
---------------------This is a Boy.
@within(demon.study.spring5.spring_aop.sample.A1) #没有对应的target
Human says:hello!
Boy jump.
@within(demon.study.spring5.spring_aop.sample.A2) #没有对应的target
Man runs.
总结:
相同点:
对象的运行时绑定的方法所属的类必须与被@within或@target中的注解类型所注解的类是同一个类,方法拦截才生效。
运行时绑定的方法是指运行时对象动态绑定的方法,一般指override方法。
@within,@target中的注解类型,本示例中指A1,A2
被A1注解的类有Human
被A2注解的类有Man。