1.引入jar包

sprig框架基础包+JUntil整合包+日志包+AOP包

spring的传统AOP的开发的包

  spring-aop-4.2.4.RELEASE.jar

  com.springsource.org.aopalliance-1.0.0.jar

aspectJ的开发包

  com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

  spring-aspects-4.2.4.RELEASE.jar

2.引入配置文件(AOP约束,注解约束)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

</beans>

3.创建实现类

public interface UserService {

    public void save();

    public void update();

    public void delete();
}
@Service(value="userService")
public class UserServiceImpl implements UserService {

    @Override
    public void save() {
        // TODO Auto-generated method stub
        System.out.println("业务层:保存客户..");
    }

    @Override
    public void update() {
        // TODO Auto-generated method stub
        System.out.println("业务层:更新客户..");
    }

    @Override
    public void delete() {
        // TODO Auto-generated method stub
        System.out.println("业务层:删除客户..");
    }

}

4.将实现类托管于Spring注解方式

5.创建切面类

public class MyAspectAnn {
    public void log(){
        System.out.println("前置通知..");
    }

public void arround(ProceedingJoinPoint joinPoint){
        System.out.println("环绕通知1..");
        System.out.println("环绕通知2..");
    }
    public void after(){
        System.out.println("后置通知..");
    }

}

6.将切面类托管于Spring

7.定义切面类方法的切入点

8.在配置文件中开启自动代理,并扫描注解

    <!-- 扫描注解 -->
    <context:component-scan base-package="com.spring.demo1"/>

    <!-- 开启自动代理 -->
    <aop:aspectj-autoproxy/>

9.编写测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {

    @Resource(name="userService")
    private UserService userService;

    @Test
    public void m01(){
        System.out.println("=======================");
        userService.save();
        System.out.println("=======================");
        userService.update();
        System.out.println("=======================");
        userService.delete();
        System.out.println("=======================");
    }
}

效果:

【核心核心】8.Spring【AOP】注解方式的更多相关文章

  1. spring aop注解方式与xml方式配置

    注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...

  2. 转:Spring AOP 注解方式实现的一些“坑”

    使用过Spring AOP的注解方式实现,也入过不少坑,现在做一下记录,希望有同样需求的朋友可以少走弯路 使用之前还是先过一下官方文档吧,至少可以少走弯路,不用担心英文读不懂,其实只看代码例子就能理解 ...

  3. Spring Aop 注解方式参数传递

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  4. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  5. (转)使用Spring的注解方式实现AOP入门

    http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...

  6. Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop

    Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...

  7. 来一手 AOP 注解方式进行日志记录

    系统日志对于定位/排查问题的重要性不言而喻,相信许多开发和运维都深有体会. 通过日志追踪代码运行状况,模拟系统执行情况,并迅速定位代码/部署环境问题. 系统日志同样也是数据统计/建模的重要依据,通过分 ...

  8. Spring AOP配置方式

    AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...

  9. Spring AOP注解为什么失效?90%Java程序员不知道

    使用Spring Aop注解的时候,如@Transactional, @Cacheable等注解一般需要在类方法第一个入口的地方加,不然不会生效. 如下面几种场景 1.Controller直接调用Se ...

  10. Spring AOP注解形式简单实现

    实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...

随机推荐

  1. 使用Fiddler模拟弱网络环境测试

    https://blog.csdn.net/swordgirl2011/article/details/51765237 https://www.cnblogs.com/longronglang/p/ ...

  2. CF774L Bars

    题意:给你一个二进制表示是否可以吃巧克力.一共有k个巧克力,第一天和最后一天必须吃.最小化每两次吃巧克力的最大间隔? 标程: #include<bits/stdc++.h> using n ...

  3. soj115 御坂网络

    题意:平面上有n个A发射点和m个B发射点,可以选择安置相应A/B装置,装置范围是圆,自取半径(要求都相同且<=Rmax).异种要求范围不相交.求装置范围之和(不是并!). 标程: #includ ...

  4. 一个切图仔的 JS 笔记

    1,常用数据操作 Math.round(mum,2);num.toFixed(2);两位数 Math.floor(); 返回不大于的最大整数 Math.ceil(); 则是不小于他的最小整数 Math ...

  5. 阿里云代码管理平台 Teambition Codeup(行云)亮相,为企业代码安全护航

    2019杭州云栖大会企业协作与研发效能专场,企业协同平台Teambition负责人齐俊元正式发布阿里云自研的代码管理平台Teambition Codeup(行云),Codeup是一款企业级代码管理产品 ...

  6. 阿里云在云栖大会发布RPA最新3.4版本,将与达摩院联合探索人工智能领域

    9月26日,在2019年杭州云栖大会上,阿里云发布了RPA最新V3.4版本,全新升级了增加诸如录屏审计.JAVA应用录制能力.达摩院OCR内置组件.语法检查与智能提示能力增强等功能. RPA全名称Ro ...

  7. Linux复制指令

    功能: 复制文件或目录说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件或目录, ...

  8. HTML引入CSS的方法

    1.嵌入式 通过<style>标记,来引入CSS样式. 语法格式:<style type = “text/css”></style> 提示:<style> ...

  9. 基于token的验证

    认证.权限和限制 身份验证是将传入请求与一组标识凭据(例如请求来自的用户或其签名的令牌)相关联的机制.然后 权限 和 限制 组件决定是否拒绝这个请求. 简单来说就是: 认证确定了你是谁 权限确定你能不 ...

  10. 靖烜小哥哥之mybatis总结

    MyBatis是一个半自动映射的框架.“半自动”是相对于Hibernate全表映射而言的,MyBatis需要手动匹配提供POJO.SQL和映射关系,而Hibernate只需提供POJO和映射关系即可. ...