本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP

下面是使用AspectJ注解实现AOP的Java Project
首先是位于classpath下的applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="
    
  <!-- 启用AspectJ对Annotation的支持 -->    
  <aop:aspectj-autoproxy/>
        
  <bean id="userManager" class="com.jadyer.annotation.UserManagerImpl"/>
    
  <bean id="securityHandler" class="com.jadyer.annotation.SecurityHandler"/>
</beans>
 
 
然后是服务层接口以及实现类
package com.jadyer.annotation;
public interface UserManager {
  public void addUser(String username, String password);
  public void delUser(int userId);
  public String findUserById(int userId);
  public void modifyUser(int userId, String username, String password);
}
  
/**
 * 上面的UserManager是服务层的接口
 * 下面的UserManagerImpl是服务层接口的实现类
 */
  
package com.jadyer.annotation;
  
public class UserManagerImpl implements UserManager {
  public void addUser(String username, String password) {
    System.out.println("------UserManagerImpl.addUser() is invoked------");
  }
  
  public void delUser(int userId) {
    System.out.println("------UserManagerImpl.delUser() is invoked------");
  }
  
  public String findUserById(int userId) {
    System.out.println("------UserManagerImpl.findUserById() is invoked------");
    return "铁面生";
  }
  
  public void modifyUser(int userId, String username, String password) {
    System.out.println("------UserManagerImpl.modifyUser() is invoked------");
  }
}
 
接下来是使用AspectJ注解标注的切入类
package com.jadyer.annotation;
  
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
  
@Aspect
public class SecurityHandler {
  /**
   * 定义Pointcut
   * @see Pointcut的名称为addAddMethod(),此方法没有返回值和参数
   * @see 该方法就是一个标识,不进行调用
   */
  @Pointcut("execution(* add*(..))") //匹配所有以add开头的方法
  private void addAddMethod(){};
    
  /**
   * 定义Advice
   * @see 表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上
   */
  //@Before("addAddMethod()")
  @After("addAddMethod()")
  private void checkSecurity() {
    System.out.println("------【checkSecurity is invoked】------");
  }    
}
 
最后是客户端测试类
package com.jadyer.annotation;
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
/**
 * Spring对AOP的支持:采用Annotation方式
 * @see -------------------------------------------------------------------------------------
 * @see Spring提供的AOP功能还是很强大的,支持可配置,它的默认实现使用的就是JDK动态代理
 * @see 使用Spring的AOP不需要继承相关的东西,也不需要实现接口
 * @see 但有个前提条件:由于是JDK动态代理,所以若想生成代理,该类就必须得实现一个接口才行
 * @see 如果该类没有implements接口的话,仍去使用Spring的默认AOP实现时,那么就会出错
 * @see 通常需要生成代理的类都是服务层的类,所以通常都会抽一个接口出来。即养成面向接口编程的习惯
 * @see -------------------------------------------------------------------------------------
 * @see 采用Annotation方式完成AOP示例的基本步骤,如下
 * @see 1、Spring2.0的依赖包配置。新增Annotation支持
 * @see   * SPRING_HOME//dist//spring.jar
 * @see   * SPRING_HOME//lib//log4j//log4j-1.2.14.jar
 * @see   * SPRING_HOME//lib//jakarta-commons//commons-logging.jar
 * @see   * SPRING_HOME//lib//aspectj//*.jar
 * @see 2、将横切性关注点模块化,建立SecurityHandler.java
 * @see 3、采用注解指定SecurityHandler为Aspect
 * @see 4、采用注解定义Advice和Pointcut
 * @see 5、启用AspectJ对Annotation的支持,并且将目标类和Aspect类配置到IoC容器中
 * @see 6、开发客户端
 * @see -------------------------------------------------------------------------------------
 */
public class Client {
  public static void main(String[] args) {
    ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserManager userManager = (UserManager)factory.getBean("userManager");
    userManager.addUser("张起灵", "02200059");
  }
}
 
下面是使用XML配置文件实现AOP的Java Project
首先是位于src根目录中的applicationContext-cglib.xml文件
.....
轉自:http://www.jb51.net/article/95161.htm

Spring使用AspectJ注解和XML配置实现AOP的更多相关文章

  1. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  2. [spring]Bean注入——使用注解代替xml配置

    使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...

  3. spring启动,spring mvc ,要不要xml配置,基于注解配置

    老项目是09-11年搞的,用的是spring+struts2,没有用注解,全xml配置.web.xml中也配置了一大堆. 现在启动新项目,在项目中用spring+springmvc ,主要用注解,也用 ...

  4. MongoDB和Java(4):Spring Data整合MongoDB(XML配置)

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  5. @ComponentScan注解及其XML配置

    开发中会经常使用包扫描,只要标注了@Controller.@Service.@Repository,@Component 注解的类会自动加入到容器中,ComponentScan有注解和xml配置两种方 ...

  6. Spring声明式事务(xml配置事务方式)

    Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...

  7. spring与hibernate注解及XML方式集成

    spring与hibernate注解及XML方式集成 Hibernate Xml方式 该种方式需要在sessionFactory中引入对应的hbm.xml文件,样例如下: <!-- spring ...

  8. mybatis使用注解替代xml配置,动态生成Sql

    mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...

  9. 基于XML配置的AOP实现日志打印

    Spring中可以使用注解或XML文件配置的方式实现AOP.1.导入jar包 com.springsource.net.sf.cglib -2.2.0.jar com.springsource.org ...

随机推荐

  1. ClamAV病毒软件的安装和使用

    ClamAV 杀毒是Linux平台最受欢迎的杀毒软件,ClamAV属于免费开源产品,支持多种平台,如:Linux/Unix.MAC OS X.Windows.OpenVMS.ClamAV是基于病毒扫描 ...

  2. python对oracle数据库的操作

    1          Oracle数据库 1.1       Oracle环境配置&客户端连接 1.1.1          下载安装Oracle绿色版客户端instantclient: 到o ...

  3. 敏感词过滤和XML的创建

    今天我慢下来啦,因为这三天没有新的课程学习内容,自己仅仅看啦一些,这让我停下来栖息片刻:说说现在的生活,简单的进行着,每天要奔波着去上课,然后回来,每天都在想怎样学习这个小知识点,大脑也在想怎样解决程 ...

  4. 【大数据系列】Hadoop DataNode读写流程

    DataNode的写操作流程 DataNode的写操作流程可以分为两部分,第一部分是写操作之前的准备工作,包括与NameNode的通信等:第二部分是真正的写操作. 一.准备工作 1.首先,HDFS c ...

  5. css布局 - 常规上中下分左右布局的一百种实现方法(更新中...)

    一. 上中下左固定 - fixed+margin 概括:如图,此种布局就是顶部.底部和左侧固定不动,只有中间右侧超出可滚动. html: <header>我是头部position: fix ...

  6. Android studio 怎么使用已经下载好的Android SDK ?

    AS:3.1.2 ---> android-studio-ide-173.4720617-windows32.zip sdK: gradle:4.4 1. 2. 3. 4. 5. 下面重要一步, ...

  7. laravel blade模板里调用路由方法重定向

    @if (Session::get('user') == NULL) {!!Redirect::to('login')!!} @endif or @if (Session::get('user') = ...

  8. How to Use Postman to Manage and Execute Your APIs

    How to Use Postman to Manage and Execute Your APIs Postman is convenient for executing APIs because ...

  9. 提交代码到自己的github仓库

    说起来尴尬,好几年前就搞了github建了仓库,当时玩得还有点6,后来一直的公司都是svn,自己业务项目也没玩,都忘了要怎么提交代码到自己的仓库了. 这边再来一波记录吧. 一.配置用户名 git co ...

  10. getconf

    用途 将系统配置变量值写入标准输出. 语法 getconf [ -v specification ] [ SystemwideConfiguration | PathConfiguration Pat ...