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中如何使用注解实现面向切面编程,以及如何使用自定义注解. 一个场景 比如用户登录,每个请求发起之前都会判断用户是否登录,如果每个请求都去判断一次,那就重复地做了很多事情,只要是有 ...
随机推荐
- Grodno 2015 (Urozero May 2015 Day 5) D Triangles
给出$P(<=10^9)$, 求有多少个有序三元组$(a, b, c),\ gcd(a, b, c) = 1,\ a + b + c <= P$且以它们构成的三角形中存在某个角是另外一个角 ...
- 《The Swift Programming Language》的笔记-第24页
The Swift Programming Language读书笔记学习笔记 第24页 本页主要内容有两个:打印输出和怎样在swift凝视代码 1 怎样打印变量和常量的值? 使用println函数,细 ...
- db2 reorg pending
通常在数据库上线之前,我们都会对数据库做周密的规划,无论在测试阶段还是上线之初,也难免由于需求的更改会对数据中的表做一些更改.而某些alter table语句则会导致该表处于reorg-pending ...
- 蓝桥杯 第三届C/C++预赛真题(3) 比酒量(数学题)
有一群海盗(不多于20人),在船上比拼酒量.过程如下:打开一瓶酒,所有在场的人平分喝下,有几个人倒下了.再打开一瓶酒平分,又有倒下的,再次重复...... 直到开了第4瓶酒,坐着的已经所剩无几,海盗船 ...
- word中使用MathType能做什么
在Office中写论文,特别是一些比较专业的论文需要用到各种公式的.会发现有很多地方Office自带的公式编辑器都无法完成,所以要用到MathType公式编辑器这个好用的工具了.MathType是一款 ...
- java的Date类型转换为MySQL数据库的Date类型
最近遇到一个问题,需要把java中的日期类型存放为MySQL数据库的日期类型,两个日期之间需要进行转化才能进行存储,转化代码如下: package com.alphajuns.demo1; impor ...
- 【IDEA】单元测试:项目中引入JUnit测试框架+Mock简单了解
一.Junit 使用和说明: 参考:单元测试第三弹--使用JUnit进行单元测试-HollisChuang's Blog http://www.hollischuang.com/archives/17 ...
- xshell 没有反应---Xshell按ctrl+s界面无反应的解决办法
在用Xshell管理远程服务器,特别是在用vi编辑配置文件时,总是习惯的用ctrl+s想要保存文件,然后就悲剧了.xsell就再也没有返应只能关了重新打开.但原来修改的文件算是报废了. 在网上搜索了一 ...
- JAVA发送http GET/POST请求的两种方式+JAVA http 请求手动配置代理
java发送http get请求,有两种方式. 第一种用URLConnection: public static String get(String url) throws IOException { ...
- 创建使用pycharm virtualenv
创建使用pycharm virtualenv 在python的世界里,真该感谢有PyCharm,pip,virtualenv 这些好东东,为python程序员提供了极大的方便. virtualenv ...