Spring框架中的AOP技术----注解方式

Posted AlphaJunS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring框架中的AOP技术----注解方式相关的知识,希望对你有一定的参考价值。

利用AOP技术注解的方式对功能进行增强

CustomerDao接口

1 package com.alphajuns.demo1;
2 
3 public interface CustomerDao {
4 
5     public void save();
6     
7     public void update();
8     
9 }

CustomerDaoImpl实现类

 1 package com.alphajuns.demo1;
 2 
 3 public class CustomerDaoImpl implements CustomerDao {
 4 
 5     @Override
 6     public void save() {
 7         System.out.println("保存客户...");
 8     }
 9 
10     @Override
11     public void update() {
12         System.out.println("更新客户...");
13     }
14 
15 }

切面类

 1 package com.alphajuns.demo1;
 2 
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 import org.aspectj.lang.annotation.After;
 5 import org.aspectj.lang.annotation.Around;
 6 import org.aspectj.lang.annotation.Aspect;
 7 import org.aspectj.lang.annotation.Before;
 8 import org.aspectj.lang.annotation.Pointcut;
 9 
10 /*
11  * 注解方式的切面类
12  */
13 @Aspect
14 public class MyAspectAnno {
15 
16     /*
17      * 前置通知
18      */
19     @Before(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
20     public void log() {
21         System.out.println("记录日志...");
22     }
23     
24     /*
25      * 后置通知
26      */
27     /*@After(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")*/
28     @After(value="MyAspectAnno.fn()")
29     public void after() {
30         System.out.println("更新日志...");
31     }
32     
33     @Around(value="execution(public void com.alphajuns.demo1.CustomerDaoImpl.save())")
34     public void around(ProceedingJoinPoint joinPoint) {
35         System.out.println("环绕通知1...");
36         try {
37             joinPoint.proceed();
38         } catch (Throwable e) {
39             e.printStackTrace();
40         }
41         System.out.println("环绕通知2...");
42     }
43     
44     /*
45      * 自定义切入点
46      */
47     @Pointcut(value="execution(public * com.alphajuns.demo1.CustomerDaoImpl.save())")
48     public void fn(){};
49     
50 }

测试类

 1 package com.alphajuns.demo1;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 
10 @RunWith(SpringJUnit4ClassRunner.class)
11 @ContextConfiguration("classpath:applicationContext.xml")
12 public class Demo1 {
13 
14     @Resource(name="customerDao")
15     private CustomerDao customerDao;
16     
17     @Test
18     public void run1() {
19         customerDao.save();
20     }
21     
22 }

配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8     http://www.springframework.org/schema/beans/spring-beans.xsd
 9     http://www.springframework.org/schema/context
10     http://www.springframework.org/schema/context/spring-context.xsd
11     http://www.springframework.org/schema/aop
12     http://www.springframework.org/schema/aop/spring-aop.xsd
13     http://www.springframework.org/schema/tx 
14     http://www.springframework.org/schema/tx/spring-tx.xsd">
15     
16     <!-- 开启自动代理 -->
17     <aop:aspectj-autoproxy/>
18     
19     <!-- 创建目标对象 -->
20     <bean id="customerDao" class="com.alphajuns.demo1.CustomerDaoImpl"/>
21     
22     <!-- 创建切面类 -->
23     <bean id="myAspectAnno" class="com.alphajuns.demo1.MyAspectAnno"/>
24     
25 </beans>

 

以上是关于Spring框架中的AOP技术----注解方式的主要内容,如果未能解决你的问题,请参考以下文章

spring框架

基于注解的Spring AOP的配置和使用

框架 day37 Spring3,AOP,代理模式(动态/CGLIB/工厂bean),传统AOP,AspectJ框架(基于xml/注解),切入点表达式,jdbcTemplate

一起学Spring之注解和Schema方式实现AOP

[Spring框架]Spring AOP基础入门总结一.

Spring基础IOC(控制反转)AOP(面向切面编程)Log4j注解配置