javascript
Spring--@within和@target的区别
關于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 the declared type 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;@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 demon.study.spring5.spring_aop.sample;@A2 public 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.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration @ComponentScan @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();} }Aspect:
package demon.study.spring5.spring_aop.sample;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;@Aspect @Component public 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。
?
不同點:
- @target要求對象的運行時類型與被注解的類型是同一個類型
- @within要求對象的運行時類型是被注解的類型的子類
?
?
?
總結
以上是生活随笔為你收集整理的Spring--@within和@target的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis设计与实现笔记
- 下一篇: Spring--Resource