使用注解进行实现:减少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. maven 向本地私库导入jar

    mvn install:install-file -DgroupId=<your_group_name> -DartifactId=<your_artifact_name> - ...

  2. 数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别

    join on 与 where 条件的执行先后顺序: join on 条件先执行,where条件后执行:join on的条件在连接表时过滤,而where则是在生成中间表后对临时表过滤 left joi ...

  3. Swift2.1 语法指南——访问控制

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

  4. python 多线程就这么简单(转)

    多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...

  5. event相关

    event.button 事件属性可返回一个整数,指示当事件被触发时哪个鼠标按键被点击. event.keyCode  事件属性可返回一个整数,指示当事件被触发时哪个键盘按键被点击. <scri ...

  6. SQLMAP脱裤

    之前就有一个朋友问,咋使用sqlmap脱裤.然后我丢了一个参数给他,他好像懵逼懵逼的.今天又有一个哥们儿在群里问,行吧.就直接教一下. 其实很简单. --dump这个参数就是用来脱裤的哟.如果你要拖整 ...

  7. echarts

    ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10/11 ...

  8. 4.了解AngularJS模块和依赖注入

    1.模块和依赖注入概述 1.了解模块 AngularJS模块是一种容器,把代码隔离并组织成简洁,整齐,可复用的块. 模块本身不提供直接的功能:包含其他提供功能的对象的实例:控制器,过滤器,服务,动画 ...

  9. Java通过httpclient获取cookie模拟登录

    package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Htt ...

  10. Apache shutdown unexpectedly启动错误解决方法

    这个问题比较常见, 通常是80.443端口被占用 cmd 通过运行apache/bin/httpd.exe 打印如下log: (OS 10048)通常每个套接字地址(协议/网络地址/端口)只允许使用一 ...