1.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<!--设置自动代理-->

<aop:aspectj-autoproxy />

<!-- 自动装箱扫描,检测注释的组件-->

<context:component-scan base-package="com.donghua.aop.annotation"></context:component-scan>

<!-- Aspect ,设置切面-->

<bean id="logAd" class="com.donghua.aop.annotation.LogAd" />

</beans>

2  定义切面

@Aspect
public class LogAd{

//expression格式 (修饰符类型?(问好代表可省略如:public)

          返回值类型

          全限定包名?

          方法名(..) ..代表参数类型


@Before("execution(* *(..))")
public void before(){
System.out.println("before==========>");
}

@After("execution(public * *.save(..))")
public void after(){
System.out.println("afte==========>");
}

@AfterReturning("execution(* *(..))")
public void afterreturn(){
System.out.println("afterreturn==========>");
}

@Around("execution(* *.remove*(..))")
public void around(ProceedingJoinPoint proceed){
try{
System.out.println("around==before========>");
proceed.proceed();
System.out.println("around==after====");

}catch(Throwable e){
e.getStackTrace();
}

}

}

3 定义注释的组件

@Component("userDaoImp")
public class UserDaoImp{

public UserDaoImp(){}

public void save() {
System.out.println("save======>");

}

public void delete() {
System.out.println("delete======>");

}

public void remove() {
System.out.println("remove======>");

}

}

4.测试

public class TestAop {

private static ApplicationContext ac = new
ClassPathXmlApplicationContext("com/donghua/aop/annotation/applicationContext.xml");
@Test
public void testaop(){
UserDaoImp user = (UserDaoImp) ac.getBean("userDaoImp");
System.out.println(user.getClass());
user.delete();
user.save();
user.remove();
}
}

结果:

class com.donghua.aop.annotation.UserDaoImp$$EnhancerByCGLIB$$1cb1e29b
before==========>
delete======>
afterreturn==========>
before==========>
save======>
afte==========>
afterreturn==========>
before==========>
around==before========>
remove======>
around==after====
afterreturn==========>

AOP annotation的更多相关文章

  1. 使用AOP+Annotation实现操作日志记录

    先创建注解 OperInfo @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @ ...

  2. 尚学堂Spring视频教程(六):AOP Annotation

    此处省略N个字.... 直接看下面 推荐链接: Spring Aop实例之AspectJ注解配置

  3. Spring 一二事(10) - annotation AOP

    先贴出POM的内容,这个毕竟是用的maven来简单构建的 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...

  4. 转载:Spring AOP (下)

    昨天记录了Spring AOP学习的一部分(http://www.cnblogs.com/yanbincn/archive/2012/08/13/2635413.html),本来是想一口气梳理完的.但 ...

  5. Spring学习4-面向切面(AOP)之aspectj注解方式

    一.简介    1.AOP用在哪些方面:AOP能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任,例如事务处理.日志管理.权限控制,异常处理等,封装起来,便于减少系统的重复代码,降低模块间的耦合 ...

  6. 【Spring五】AOP之使用注解配置

    AOP使用注解配置流程: 1.当spring容器启动时候.    < context:component- scan base-package= "cn.itheima03.sprin ...

  7. Spring AOP (下)

    4.方式二:schema配置 a.业务类: /** * 业务类 * * @author yanbin * */ public class AspectBusiness { /** * 切入点 */ p ...

  8. Spring 入门 AOP

    通过一个小例子演视怎么使用 Spring 现实面向切面编程. 导入 Spring 所需要的包 spring-framework-2.5.6 版需要导入以下包: 1.----- spring.jar 2 ...

  9. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

随机推荐

  1. delphi 线程池基础 TSimplePool

    1. TSimpleThread 2. TSimpleList 3. 以1,2构成 TSimplePool 用法 先定义: TDoSomeThingThread=class(TSimpleThread ...

  2. material风格的日期/时间选择:SublimePicker

    介绍: 一个material风格的view,提供了各种关于日期选择的功能,可以选择日期,选择时间,选择重复次数等,可以在不离开选择器的情况下在不同的选择界面间切换.其实这些功能是从5.0的日历中抠出来 ...

  3. poj2578---三个数中找出第一个大于168的

    #include <stdio.h> #include <stdlib.h> int main() { int a,b,c; scanf("%d %d %d" ...

  4. Cpp again

    1,

  5. html---textarea初始化时就有个table空格以及tab键操作无效

    1 初始化时就有一个tab空格 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRnJlZUFwZQ==/font/5a6L5L2T/fontsize/400 ...

  6. Pro Android学习笔记(十一):了解Intent(中)

    Intent的构成 Intent能够带有action,data(由URI表达),extra data(key/value map,键值对),指定的类名(成为component name).一个inte ...

  7. 谷歌识图、google识图如何知道图片相似?

    转自:http://www.hahait.com/news/185.html 原理非常简单易懂.我们可以用一个快速算法,就达到基本的效果.这里的关键技术叫做“感知哈希算法”(Perceptual ha ...

  8. HTTP错误500.22 检测到在集成的托管管道模式下不适用的ASP.NET设置

    这里主要把集成模式改成经典模式 解决方案一: 解决方案二: 修改配置文件web.config 将 <configuration> <system.web> <compil ...

  9. Android常用工具类封装---SharedPreferencesUtil

    SharedPreferences常用于保存一些简单的数据,如记录用户操作的配置等,使用简单. public class SharedPreferencesUtil {              // ...

  10. 本地环境下 WordPress 环境搭建与安装

    本地环境:Ubuntu 14.04 使用软件: WordPress 4.1.1 中文优化版 EasyEngine 安装步骤: 安装 LNMP 环境; wget -qO ee rt.cx/ee & ...