Spring AOP使用心得

开发 后端
本文介绍Spring AOP使用心得,以及在Spring的xml配置文件中加入新的schema。

毕竟是***次使用Spring AOP,按照Reference中的介绍,准备使用Annotation来完成对AOP的配置。来看一下我做的步骤:

一、需要使用Spring2.0的jar包,现在已经发布正式版的2.0了,可以从http://www.springframework.org/上下载到***的2.0版本。加入到项目的classpath中去。

二、需要在配置文件中启用新的spring2.0的schema或者是dtd。  

1、在Spring的xml配置文件中加入新的schema:

  1. <beans xmlns="http://www.springframework.org/schema/beans" 
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.     xmlns:aop="http://www.springframework.org/schema/aop" 
  4.     xmlns:tx="http://www.springframework.org/schema/tx" 
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  6.            http://www.springframework.org/schema/aop                           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  7.            http://www.springframework.org/schema/tx                                 http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"  
  8.     default-autowire="byName" default-lazy-init="true"> 

2、“如果使用Java 5的话,推荐使用Spring提供的@AspectJ切面支持,通过这种方式声明Spring AOP中使用的切面。 "@AspectJ"使用了Java 5的注解,可以将切面声明为普通的Java类。”——Spring reference

3、为了使用Spring AOP的Annotation,在配置文件中加入<aop:aspectj-autoproxy />。

4、编写切面类:

  1. public class ArticleRemoteAccountsService {  
  2.    
  3.     /** *//**  
  4.      * 在发帖成功之后,给用户银币账户冲值  
  5.      *   
  6.      * @param arg  
  7.      * @throws AccountsException  
  8.      * @throws InstantiationException  
  9.      * @throws IllegalAccessException  
  10.      */  
  11.     @After("execution(* com.company.ArticleManager.saveArticle(..))"  
  12.             + " && args(arg)")  
  13.     public void exSilByPost(Article arg) throws AccountsException,  
  14.             InstantiationException, IllegalAccessException {  
  15.         if (arg.getLastUpdateTime() == null  
  16.                 && arg.getArticleByParentId() == null  
  17.                 && arg.getArticleByRootId() == null) {  
  18.             // TODO 主题帖  
  19.         } else if (arg.getLastUpdateTime() == null  
  20.                 && (arg.getArticleByParentId() != null || arg  
  21.                         .getArticleByRootId() != null)) {  
  22.             // TODO  回帖  
  23.         }  
  24.     }  
  25. }  

这里需要注意的是使用Annotation的Poincut语法,execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)这里就不累诉了。同时要注意的如何得到参数的问题,写法参考如上。

 

【编辑推荐】

  1. Spring Batch 2.0的新特性
  2. 简单介绍Spring osgi
  3. Spring OSGi的整合
  4. 在Spring中配置Glassfish JPA
  5. 解决Spring2.0向spring2.5迁移的问题
责任编辑:佚名 来源: blogjava
相关推荐

2011-01-19 14:57:09

Thunderbird

2011-01-07 11:14:17

Nginx负载均衡负载均衡

2011-09-01 10:16:43

JQuery Mobi

2010-02-01 10:15:07

C++ TinyXML

2009-08-28 10:08:15

C# using语句

2010-03-02 17:33:49

Android 2.0

2009-06-26 16:12:08

ThickboxjQuery

2009-08-11 11:42:50

Ruby使用心得

2009-08-31 10:00:07

C#静态变量

2010-08-06 11:12:38

FlexCairngorm框架

2009-08-26 13:31:21

JavaScript使

2010-06-01 13:42:37

TortoiseSVN

2011-04-20 13:59:28

爱普生V100扫描仪

2011-03-15 11:05:03

2016-10-07 15:45:59

2022-06-27 19:16:53

Obsidian工具

2009-07-31 18:37:45

ASP.NET MVC

2020-03-16 08:13:58

SQL性能问题

2021-03-01 23:26:41

日志Spring BootAOP

2011-04-28 16:20:18

全能一体机使用心得
点赞
收藏

51CTO技术栈公众号