【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 之定义切面尝试(基于注解)的更多相关文章

  1. Spring 之定义切面尝试(基于 XML)

    有些场景下只能基于 XML 来定义切面. [Spring 之定义切面尝试] 1.XML 下定义切面(首先是要有一个对应的类...显然要比基于注解的麻烦) <?xml version=" ...

  2. Spring IoC 源码分析 (基于注解) 之 包扫描

    在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...

  3. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  4. 使用Spring框架入门四:基于注解的方式的AOP的使用

    一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...

  5. spring(四)之基于注解(Annotation-based)的配置.md

    注解 这里讲的注解有下面几个 @Autowired @Qualifier(" ") @Genre(" ") @Offline @Resource(name=&q ...

  6. Spring IOC容器装配Bean_基于注解配置方式

    bean的实例化 1.导入jar包(必不可少的) 2.实例化bean applicationContext.xml(xml的写法) <bean id="userDao" cl ...

  7. 使用Spring框架入门二:基于注解+XML配置的IOC/DI的使用

    一.简述 本文主要讲使用注解+xml配合使用的几种使用方式.基础课程请看前一节. 二.步骤 1.为Pom.xml中引入依赖:本例中使用的是spring-context包,引入此包时系统会自动导入它的依 ...

  8. Spring AOP:面向切面编程,AspectJ,是基于注解的方法

    面向切面编程的术语: 切面(Aspect): 横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象 通知(Advice): 切面必须要完成的工作 目标(Target): 被通知的对象 代理(Pr ...

  9. spring中基于注解使用AOP

    本文内容:spring中如何使用注解实现面向切面编程,以及如何使用自定义注解. 一个场景 比如用户登录,每个请求发起之前都会判断用户是否登录,如果每个请求都去判断一次,那就重复地做了很多事情,只要是有 ...

随机推荐

  1. Androidz之字符串国际化问题

    (1)字符串的国际化 在res下写两个values 注意:命名方式都是固定的 一个中文版:values-zh 一个英文版:values-en 可是<string name  要写成一样的,这样会 ...

  2. ASP.NET动态增加HTML元素的方法实例小结

    本文实例讲述了ASP.NET动态增加HTML元素的方法.分享给大家供大家参考,具体如下: 在使用asp.net进行web开发的时候页面中的<head></head>中的信息可以 ...

  3. 学习DBCC CHECKIDENT

    检查指定表的当前标识值,如有必要,还对标识值进行更正. 语法DBCC CHECKIDENT    ( 'table_name'        [ , { NORESEED                ...

  4. Eclipse设置护眼背景

    Window-->Preferences-->General-->Editors-->Text Editors-->Background color 自定义颜色:色调:8 ...

  5. JavaScript------如何查看var变量是否是指定类型

    function isArray(a) { //Date,Array,String,Object,Function,Boolean,Number return a.constructor.toStri ...

  6. poj3372

    Candy Distribution Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5868   Accepted: 327 ...

  7. jQuery 对象访问 index([selector|element])

    搜索匹配的元素,并返回相应元素的索引值,从0开始计数. 如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的位置. 如果参数是一组DOM元素或 ...

  8. jQuery与Zepto

    jQuery和Zepto是我比较常用的插件.其实用法差不太多,可以说Zepto是jQuery的轻量级替代品,但是不要认为Zepto就没有jQuery好用,因为Zepto有jQuery没有的功能,就是移 ...

  9. VB 十六进制转汉字的函数

    Public Function HexToStr(ByVal strs As String) As String Dim i As Integer, tmp As String, n If Len(s ...

  10. 解决:IDEA unable to import maven project see logs for details问题+java http请求报java.net.SocketException: Permission denied:connect 问题

    背景:用IDEA写了一个java发送http请求的maven项目. 运行时,项目报java.net.SocketException: Permission denied:connect问题: 修改po ...