日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring--@within和@target的区别

發布時間:2024/4/13 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。