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的区别相关推荐
- Spring AOP / AspectJ AOP 的区别?
Spring AOP / AspectJ AOP 的区别? Spring AOP属于运行时增强,而AspectJ是编译时增强. Spring AOP基于代理(Proxying),而AspectJ基于字 ...
- Spring AOP 和 AspectJ的区别
Spring AOP 和 AspectJ的区别 springAOP 是spring支持的面向切面AOP 编程. AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法 ...
- Spring中default-autowire与autowire区别
学习笔记:Spring中default-autowire与autowire区别 default-autowire与autowire主要用于Spring的IOC的注解注入,明白两者的区别和用法将使你的开 ...
- spring aop的@target与@within的真正区别到底是什么?
文档里面是这么说的: @target: Limits matching to join points (the execution of methods when using Spring AOP) ...
- Spring中@within与@target的区别
区别 @within:定义在哪里,就作用在哪里.(有点静态编译的味道) @target:运行在哪里,就作用在哪里.(类比动态执行的感觉)@within,如果子类没有重写,在某个类上加一个注解,就会默认 ...
- Spring @Configuration和@Component的区别(enhancer的原因)
Spring @Configuration 和 @Component 区别 一句话概括就是 @Configuration 中所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个 ...
- Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法
1. BeanFactory BeanFactory,以Factory结尾,表示它是一个工厂类(接口),用于管理Bean的一个工厂.在Spring中,BeanFactory是IOC容器的核心接口,它的 ...
- Spring中ApplicationContext和beanfactory区别
BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...
- spring拦截器-过滤器的区别
1. 理解 拦截器 :是在面向切面编程的时候,在你的 service 或者一个方法前调用一个方法,或者在方法后调用一个方法:比如动态代理就是拦截器的简单实现,在你调用方法前打印出字符串(或者做其它业 ...
最新文章
- 在哪里学python比较好-学Python从哪里开始?
- python计算两个数乘积_如何用PYTHON使两个数字总和变成乘积
- 大讲台大数据特训学习笔记
- jupyter notebook的链接密码 token查询 以及 pycharm 如何使用 jupyter notebook
- Objective-C 高性能的循环遍历 forin - NSEnumerator - 枚举 优化
- 多个数求平均数java_[分享]求任意个数的平均数!
- 详谈P(查准率),R(查全率),F1值
- macOS Docker 上安装、启动 MySQL
- 人民网舆情:公众对网约车或存偏见
- 游戏筑基开发之广度优先搜索算法(C语言)
- 滤波器原理及其作用计算机网络,数字滤波器
- 嵌入式中的 C 语言
- 姑娘留步,容我劫个色
- 织梦DedeCms网站更换域名后文章图片路径批量修改
- 对数型函数的值域与定义域为R(手机横屏交互操作)
- Android设备双屏显示
- 超级计算机性能测试,高性能计算机评测框架及其在神威蓝光上的实践
- TP-Link WR841N无线桥接(WDS)教程
- 【02】Java进阶:13-IO资源的处理、属性集、缓冲流、转换流、序列化、打印流、装饰设计模式、commons-io工具包
- 爬取笔趣阁小说,并可以搜索爬取你喜欢的小说