【核心核心】8.Spring【AOP】注解方式
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】注解方式的更多相关文章
- spring aop注解方式与xml方式配置
注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...
- 转:Spring AOP 注解方式实现的一些“坑”
使用过Spring AOP的注解方式实现,也入过不少坑,现在做一下记录,希望有同样需求的朋友可以少走弯路 使用之前还是先过一下官方文档吧,至少可以少走弯路,不用担心英文读不懂,其实只看代码例子就能理解 ...
- Spring Aop 注解方式参数传递
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...
- (转)使用Spring的注解方式实现AOP入门
http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...
- Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop
Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...
- 来一手 AOP 注解方式进行日志记录
系统日志对于定位/排查问题的重要性不言而喻,相信许多开发和运维都深有体会. 通过日志追踪代码运行状况,模拟系统执行情况,并迅速定位代码/部署环境问题. 系统日志同样也是数据统计/建模的重要依据,通过分 ...
- Spring AOP配置方式
AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...
- Spring AOP注解为什么失效?90%Java程序员不知道
使用Spring Aop注解的时候,如@Transactional, @Cacheable等注解一般需要在类方法第一个入口的地方加,不然不会生效. 如下面几种场景 1.Controller直接调用Se ...
- Spring AOP注解形式简单实现
实现步骤: 1:导入类扫描的注解解析器 命名空间:xmlns:context="http://www.springframework.org/schema/context" xsi ...
随机推荐
- CSS——溢出文字隐藏
溢出的文字隐藏 word-break:自动换行 normal 使用浏览器默认的换行规则. break-all 允许在单词内换行. keep-all 只能在半角空格或连字符处换行. 主要处理英文单词 w ...
- 边双联通分量缩点+树的直径——cf1000E
题意理解了就很好做 题意:给一张无向图,任意取两个点s,t,s->t的路径上必经边数量为k 求这样的s,t,使得k最大 #include<bits/stdc++.h> #define ...
- BZOJ 2957楼房重建
传送门 线段树 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include< ...
- hdu多校第二场1011 (hdu6601) Keen On Everything But Triangle 主席树
题意: 给定一个数列,每次询问一个区间,问这个区间中的值可组成的周长最大的三角形的周长. 题解: 定理1:给定一些值,这些值中组成边长最大的三角形的三条边的大小排名一定是连续的. 证明:假如第k大,第 ...
- VS2010-MFC(图形图像:GDI对象之画刷CBrush)
转自:http://www.jizhuomi.com/software/248.html 上一节讲的是画笔CPen的用法,前面也说了,GDI对象中最常用的就是画笔和画刷,本节就讲讲画刷CBrush. ...
- idea从github中pull或者push成功之后ssm项目全部controller报红色下划线的解决方案
某次从github上pull下来之后,报出如下一堆红色波浪线错误 解决方法: 在随便一个红色波浪线处,按下alt+enter,点击add maven dependency, 选中所有,点击添加即可,此 ...
- 调试存储过程与declare语句差异
当应用有调用存储过程,而节点有几十个或者上百个,找错是不是一个很麻烦的事情,这个时候,我建议写到数据库中,下面是我做的一个demo. 1. 建立错误日志记录表 drop table PUB_PROC ...
- 2018-8-10-win10-uwp-使用资源在后台创建控件
title author date CreateTime categories win10 uwp 使用资源在后台创建控件 lindexi 2018-08-10 19:17:19 +0800 2018 ...
- 在 my_rootfs 中建立 Linux 目录树
[arm@localhost my_rootfs]#mkdir bin dev etc home lib mnt proc sbin sys tmp root usr [arm@localhost m ...
- LUOGU P1453 城市环路(基环树+dp)
传送门 解题思路 一道基环树上$dp$的题,这种题比较套路吧,首先第一遍$dfs$把环找出来,然后对于环上的每一个点都向它子树内做一次树形$dp$,$f[i][0/1]$表示到了$i$这个点选或不选的 ...