使用注解进行实现:减少xml文件的配置。

1 建立切面类

不需要实现任何特定接口,按照需要自己定义通知。

 package org.guangsoft.utils;
import java.util.Date;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/***
* 定义用户的切面类
* ***/
@Component
@Aspect
public class UsersAdvice
{
// 前置的通知
@Before("execution(* com.bjsxt.service.impl.*.*(..))")
public void before()
{
System.out.println("before-------------" + new Date());
}
// 后置通知
// @After("execution(* com.bjsxt.service.impl.*.*(..))")
@AfterReturning(pointcut = "execution(* com.bjsxt.service.impl.*.*(..))", returning = "rv")
public void after(Object rv)
{
System.out.println("after-------------" + new Date() + "--------------"
+ rv);
}
/****
* 环绕通知
* **/
@Around("execution(* com.bjsxt.service.impl.*.*(..))")
public void around(ProceedingJoinPoint jp)
{
System.out.println("aroun.---------before-------------" + new Date());
try
{
jp.proceed();// 调用目标方法
}
catch (Throwable e)
{
e.printStackTrace();
} // 调用目标方法
System.out.println("around---------after--------------" + new Date());
}
/***
* 异常通知
* **/
@AfterThrowing(pointcut = "execution(* com.bjsxt.service.impl.*.*(..))", throwing = "ex")
public void exception(Throwable ex)
{
System.out.println("exception-------------------" + ex.getMessage());
}
}

2xml文件配置

 <?xml version="1.0" encoding="UTF-8"?>
<!-- 到入xml文件的约束 -->
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
">
<!-- 开启扫描注解 -->
<context:component-scan
base-package="org.guangsoft.service.impl,org.guangsoft.utils">
</context:component-scan>
<!-- 开启切面的自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

在开发中是方式二。减少xml配置,实现灵活

Spring面向切面编程(AOP)方式二的更多相关文章

  1. Spring面向切面编程(AOP)

    1 spring容器中bean特性 Spring容器的javabean对象默认是单例的. 通过在xml文件中,配置可以使用某些对象为多列. Spring容器中的javabean对象默认是立即加载(立即 ...

  2. Spring面向切面编程(AOP,Aspect Oriented Programming)

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  3. Spring面向切面编程AOP(around)实战

    spring aop的环绕通知around功能强大,我们这里就不细说,直接上代码,看着注释就能明白 需要的可以点击下载源码 1.如果使用注解的方式则需要先创建个注解类 package com.mb.a ...

  4. Spring学习手札(二)面向切面编程AOP

    AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...

  5. Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)

    一.面向切面编程AOP 目标:让我们可以“专心做事”,避免繁杂重复的功能编码 原理:将复杂的需求分解出不同方面,将公共功能集中解决 *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现 ...

  6. Spring框架学习笔记(2)——面向切面编程AOP

    介绍 概念 面向切面编程AOP与面向对象编程OOP有所不同,AOP不是对OOP的替换,而是对OOP的一种补充,AOP增强了OOP. 假设我们有几个业务代码,都调用了某个方法,按照OOP的思想,我们就会 ...

  7. Spring之控制反转——IoC、面向切面编程——AOP

      控制反转——IoC 提出IoC的目的 为了解决对象之间的耦合度过高的问题,提出了IoC理论,用来实现对象之间的解耦. 什么是IoC IoC是Inversion of Control的缩写,译为控制 ...

  8. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

  9. Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)

    在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...

随机推荐

  1. 浏览器兼容性之JavaScript篇

    近期公司职务变动,我大部分工作时间都在做web前端开发.工作性质主要是跟javascript和css(层叠样式表)打交道,而JavaScript兼容性一直是Web开发者的心病,当然我也不例外,虽然我大 ...

  2. Ali相关面试题

    接到的电话面试,人比较随和,当时IOS有一段时间没怎么碰了,因为近期一直在用C++,QT做IM.很多回答我都扯到了C++上,所以可能没戏- -! 回想一下,大概有如下几个问题:(都是很常见的问题) 1 ...

  3. JS跳转后台

    location.href = ROOT+"?"+VAR_MODULE+"=FaPiao&"+VAR_ACTION+"=dell&id ...

  4. 如何一次把所有wordpress插件都禁用了

    wordpress网站出现了问题,或者在更新一些数据的时候,需要先把插件全部都禁用了进行检查.那么如何一次把所有wordpress插件都禁用呢?试试下面的sql语句吧 UPDATE wp_option ...

  5. UVA1376.Animal Run (最小割转为最短路 && dijkstra)

    Animal Run Time Limit:6000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status ...

  6. Swift2.1 语法指南——扩展

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  7. /etc/bashrc和/etc/profile傻傻分不清楚?

    导读 在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 profile来设置用户的工作环境, 很多文章对于 profile 和 bashrc 也都有使用, 但究竟每个文 ...

  8. 剑指Offer 两个链表的第一个公共结点

    题目描述 输入两个链表,找出它们的第一个公共结点.   思路: 题目说的很笼统,应该是有2个链表,找出公共点,第一个公共点后面的链表是共同所有的.可以用map做,直接检测map里有没有出现这个节点. ...

  9. 取数据的前N行

    用awk中csv文件中取前1000行出来,代码虽少,很容易出错 BEGIN{ FS=","; OFS=","; i=; } { i++; )exit; prin ...

  10. CentOS 7更换 安装源

    centos 安转源更换 一:切换到目录 cd /etc/yum.repos.d二: [备份] mv CentOS-Base.repo  CentOS-Base.repo.bak三 : [下载] wg ...