Spring的Aop 注解配置
1,导包

2,准备目标对象
package com.songyan.anno;
public interface UserService {
void save();
void delete();
void find( );
void update();
}
package com.songyan.anno;
public class UserServiceImpl implements UserService {
public void save()
{
System.out.println("添加用户");
}
public void delete()
{
System.out.println("删除用户");
}
public void find( )
{
System.out.println("查询用户");
}
public void update()
{
System.out.println("更新用户信息");
}
}
<!--配置目标对象 -->
<bean id="userservice" class="com.songyan.anno.UserServiceImpl"></bean>
3,准备通知
<!--配置通知 -->
<bean id="myadvice" class="com.songyan.anno.Myadvice"></bean>
package com.songyan.anno; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; /**
* 通知类
* @author sy
*/
public class Myadvice {
public void before()
{
System.out.println("前置通知");
}
public void after_returning()
{
System.out.println("后置通知");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知之前的部分!!");
Object proceed = pjp.proceed();//调用目标方法
System.out.println("这是环绕通知之后的部分!!");
return proceed;
}
public void after_throuble()
{
System.out.println("异常后通知");
}
public void after()
{
System.out.println("后置通知(finally)");
}
}
4,开启使用注解完成注入
s<!--开启使用注解完成织入 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
完整xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!--配置目标对象 -->
<bean id="userservice" class="com.songyan.anno.UserServiceImpl"></bean>
<!--配置通知 -->
<bean id="myadvice" class="com.songyan.anno.Myadvice"></bean>
<!--开启使用注完成织入 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
5,设置注解
package com.songyan.anno; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*; /**
* 通知类
* @author sy
*/
@Aspect//该注解表示这是一个通知类
public class Myadvice { //该注解表示该方法为前置注解,参数是对应的方法
@Before("execution(* com.songyan.anno.*ServiceImpl.*(..))")
public void before()
{
System.out.println("前置通知");
} @AfterReturning("execution(* com.songyan.anno.*ServiceImpl.*(..))")
public void after_returning()
{
System.out.println("后置通知");
} @Around("execution(* com.songyan.anno.*ServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知之前的部分!!");
Object proceed = pjp.proceed();//调用目标方法
System.out.println("这是环绕通知之后的部分!!");
return proceed;
} @AfterThrowing("execution(* com.songyan.anno.*ServiceImpl.*(..))")
public void after_throuble()
{
System.out.println("异常后通知");
} @After("execution(* com.songyan.anno.*ServiceImpl.*(..))")
public void after()
{
System.out.println("后置通知(finally)");
}
}
6,测试类
package com.songyan.anno; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/anno/beans.xml");
UserService u=(UserService)applicationContext.getBean("userservice");
u.save();
} }

以上代码还存在不足:
execution(* com.songyan.anno.*ServiceImpl.*(..))此内容重复出现多次
因此:
@Aspect//该注解表示这是一个通知类
public class Myadvice {
@Pointcut("execution(* com.songyan.anno.*ServiceImpl.*(..))")
public void pc(){}; //该注解表示该方法为前置注解,参数是对应的方法
@Before("Myadvice.pc()")
public void before()
{
System.out.println("前置通知");
}
以这种方式声明一次,多次调用。
Spring的Aop 注解配置的更多相关文章
- Spring之AOP注解配置
1.导入相应jar包 2.引入约束并配置XML文件 <beans xmlns="http://www.springframework.org/schema/beans" xm ...
- spring aop注解配置
spring aop是面向切面编程,使用了动态代理的技术,这样可以使业务逻辑的代码不掺入其他乱七八糟的代码 可以在切面上实现合法性校验.权限检验.日志记录... spring aop 用的多的有两种配 ...
- Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探
由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...
- 【Spring】AOP注解方式实现机制
一.概述 二.@EnableAspectJAutoProxy 注解分析 三.分析AnnotationAwareAspectJAutoProxyCreator 四.执行流程 1. registerBea ...
- springAop:Aop(Xml)配置,Aop注解配置,spring_Aop综合案例,Aop底层原理分析
知识点梳理 课堂讲义 0)回顾Spring体系结构 Spring的两个核心:IoC和AOP 1)AOP简介 1.1)OOP开发思路 OOP规定程序开发以类为模型,一切围绕对象进行,OOP中完成某个任务 ...
- Spring MVC4 纯注解配置教程
阅读本文需要又一定的sping基础,最起码要成功的运行过一个SpringMvc项目. 在传统的Spring项目中,我们要写一堆的XML文件.而这些XML文件格式要求又很严格,很不便于开发.而网上所谓的 ...
- spring mvc 基于注解 配置默认 handlermapping
spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...
- Spring IOC-基于注解配置的容器
Spring中提供了基于注解来配置bean的容器,即AnnotationConfigApplicationContext 1. 开始 先看看在Spring家族中,AnnotationConfigApp ...
- 采用spring的schedule注解配置定时任务
1 在springmvc配置文件中新增以下配置 <!-- 此处对于定时时间的配置会被注解中的时间配置覆盖,因此,以注解配置为准 --> <task:scheduled-tasks s ...
随机推荐
- cloudera manager 5.3完整卸载脚本
service cloudera-scm-agent stop service cloudera-scm-agent stop umount /var/run/cloudera-scm-agent/p ...
- Spring学习--通过注解配置 Bean (一)
在 classpath 中扫描组件: 组件扫描(component scanning): Spring 能够从 classpath 下自动扫描 , 侦测和实例化具有特定注解的组件. 特定组件包括: @ ...
- 利用vue-cli创建Vue项目
1.安装node.js:Node.js安装包及源码下载地址为:https://nodejs.org/en/download/. 配置参考:http://www.runoob.com/nodejs/no ...
- 【BZOJ3450】Easy [期望DP]
Easy Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 某一天WJMZBMR在打osu~~ ...
- git分支开发,分支(feature)同步主干(master)代码,以及最终分支合并到主干的操作流程
由于rebase执行速度慢,分支同步主干代码时,分支的每次提交都可能和主干产生冲突,需要解决的次数太多,影响提交效率. 同时,为了保证主干提交线干净(可以安全回溯),所以采用下面所说的merge法. ...
- NGINX: 限制连接的实践 (Defense DDOS)
参考: [ nginx防止DDOS攻击配置 ] 关于限制用户连接,Nginx 提供的模块: [ ngx_http_limit_req_module ] [ ngx_http_limit_conn_mo ...
- python configparse
# 参考:https://www.cnblogs.com/lily1989/p/8401005.html # https://blog.csdn.net/willhuo/article/details ...
- POJ1080(LCS变形)
Human Gene Functions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- UVALIVE 2686 Stargates
尼玛真深坑合时p[x] = y 就RE,p[y] = x 就AC . #include <map> #include <set> #include <list> # ...
- Linux编写Shell脚本
——<Linux就该这么学>笔记Shell脚本命令的工作方式有两种 交互式: 用户每输入一条命令就立即执行 批处理: 由用户事先编写好一个完整的Shell脚本,Shell会一次性执行脚本中 ...