Spring 之定义切面尝试(基于注解)
【Spring 之定义切面尝试】
1、标记为深红色的依赖包是必须的
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
2、相对于表演定义一个观众切面。
package concert; import org.springframework.stereotype.Component; @Component
public class TheShow implements Performance {
public void perform() {
System.out.println("Doing performance!");
}
}
package concert; import org.aspectj.lang.annotation.*; @Aspect
public class Audience { @Pointcut("execution(* concert.Performance.perform(..))")
public void performance() {}
@Before("performance()")
public void silenceCellPhones() {
System.out.println("silencing cell phones");
} @Before("performance()")
public void takeSeats() {
System.out.println("Taking seats");
} @AfterReturning("performance()")
public void applause() {
System.out.println("CLAP CLAP CLAP!!!");
} @AfterThrowing("performance()")
public void demandRefund() {
System.out.println("Demanding a refund");
}
}
等价的环绕通知:
package concert; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; @Aspect
public class Audience { @Pointcut("execution(* concert.Performance.perform(..))")
public void performance() {} @Around("performance()")
public void watchPerformance(ProceedingJoinPoint jp) {
try {
System.out.println("Silencing cell phones");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP!!!");
} catch (Throwable e) {
System.out.println("Demanding a refund");
}
}
}
3、启用 AspectJ 注解的自动代理有两种方式:JavaConfig 和 XML
package concert; 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
@EnableAspectJAutoProxy
@ComponentScan
public class ConcertConfig { @Bean
public Audience audience() {
return new Audience();
} }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="concert" /> <aop:aspectj-autoproxy /> <bean class="concert.Audience" /> </beans>
启用 AspectJ 注解的自动代理的结果是:这个代理会围绕着所有该切面切点所匹配的 bean (关于代理书上有个特别形象的图片)
4、测试切面
package concert; import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ConcertConfig.class)
public class TheShowTest { @Autowired
Performance theShow; @Test
public void theShowShouldNotBeNull() {
assertNotNull(theShow);
} @Test
public void test() {
theShow.perform();
}
}

Spring 之定义切面尝试(基于注解)的更多相关文章
- Spring 之定义切面尝试(基于 XML)
		
有些场景下只能基于 XML 来定义切面. [Spring 之定义切面尝试] 1.XML 下定义切面(首先是要有一个对应的类...显然要比基于注解的麻烦) <?xml version=" ...
 - Spring IoC 源码分析 (基于注解) 之 包扫描
		
在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...
 - Spring AOP 面向切面编程相关注解
		
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
 - 使用Spring框架入门四:基于注解的方式的AOP的使用
		
一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...
 - spring(四)之基于注解(Annotation-based)的配置.md
		
注解 这里讲的注解有下面几个 @Autowired @Qualifier(" ") @Genre(" ") @Offline @Resource(name=&q ...
 - Spring IOC容器装配Bean_基于注解配置方式
		
bean的实例化 1.导入jar包(必不可少的) 2.实例化bean applicationContext.xml(xml的写法) <bean id="userDao" cl ...
 - 使用Spring框架入门二:基于注解+XML配置的IOC/DI的使用
		
一.简述 本文主要讲使用注解+xml配合使用的几种使用方式.基础课程请看前一节. 二.步骤 1.为Pom.xml中引入依赖:本例中使用的是spring-context包,引入此包时系统会自动导入它的依 ...
 - Spring  AOP:面向切面编程,AspectJ,是基于注解的方法
		
面向切面编程的术语: 切面(Aspect): 横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象 通知(Advice): 切面必须要完成的工作 目标(Target): 被通知的对象 代理(Pr ...
 - spring中基于注解使用AOP
		
本文内容:spring中如何使用注解实现面向切面编程,以及如何使用自定义注解. 一个场景 比如用户登录,每个请求发起之前都会判断用户是否登录,如果每个请求都去判断一次,那就重复地做了很多事情,只要是有 ...
 
随机推荐
- 【BZOJ】2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(dp)
			
http://www.lydsy.com/JudgeOnline/problem.php?id=2101 这个dp真是神思想orz 设状态f[i, j]表示i-j先手所拿最大值,注意,是先手 所以转移 ...
 - Dynamics CRM  系统自己定义部分的语言翻译
			
Dynamics CRM 自带语言切换功能,在官网下载所需语言包安装后,在设置语言中就能看到你所加入的语言.勾选要启用的语言应用就可以.再打开系统设置--语言就能看到可更改用户界面语言的显示了. wa ...
 - 图表 Chart
			
工作中,需要实现如下的图表,查阅了不少的资料,问了不少的人,下面对下图表的实现代码做下讲解. 实现代码: chart1.Series.Clear();//清空图表中的序列,图表中有默认的序列 //ch ...
 - sqlmap注入教程
			
1.SQLMAP用于Access数据库注入 (1)猜解是否能注入win: python sqlmap.py -u "http://www.stronkin.com/en/CompHonorB ...
 - 让TextView的drawableLeft与文本一起居中显示
			
TextView的drawableLeft.drawableRight和drawableTop是一个常用.好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用R ...
 - hdu4691(后缀数组)
			
算是后缀数组的入门题吧. 思路无比简单,要是直接套模板的话应该很容易秒掉. 关于后缀数组看高中神犇的论文就可以学会了 算法合集之<后缀数组——处理字符串的有力工具> 话说这题暴力是可以过了 ...
 - PMPBOK 进度管理
			
项目进度计划提供详尽的计划,说明项目如何以及何时交付项目范围中定义的产品.服务和成功,是一种用于沟通和管理相关方期望的工具,为绩效报告提供了依据. 进度计划方法:关键路径发或敏捷方法.创建项目经度模型 ...
 - input输入框输入小写字母自动转换成大写字母
			
input输入框输入小写字母自动转换成大写字母有两种方法 1.用js onkeyup事件,即时把字母转换为大写字母: html里input加上 <input type="text&qu ...
 - :nth- 从1开始计数,其他如:eq()、 index()从0开始计数
			
因为jQuery的实现:nth-是严格来自CSS规范,n值是“1-indexed”,也就是说,从1开始计数. 对于所有其他选择器表达式比如:eq() 或 :even ,jQuery遵循JavaScri ...
 - Virtual Private Cloud  专有网络  软件定义网络的方式   私有网络  大流量视频、直播类业务
			
私有网络 VPC_云上网络空间_自定义网络 - 腾讯云 https://cloud.tencent.com/product/vpc 私有网络 VPC 简介 私有网络(Virtual Private C ...