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 ...
随机推荐
- Google File System中文版
英文原文地址: Google File system 译文原文地址: The Google File System中文版 Google File System中文版 摘要 我们设计并实现了Google ...
- 如何把SSL公钥和私钥转化为PFX格式
1.登陆 https://myssl.com/cert_convert.html 2.原格式选择为 “PEM”,目标格式选择为 “PKCS12” 3.上传cer到 ”证书文件“,上传key到 ”私 ...
- sublime2创建一个html5的snippets文件
背景:跟了一个网上课程,老师哗啦啦敲代码,屏幕上只敲了几个字,键盘一操作,瞬间一大溜代码,看得我心惊肉跳连忙暂停抄抄抄. 举个简单的例子,我需要创建一个html文件.但是我不想每次都敲固定的格式.那么 ...
- React 使用 fetch 请求天气
中国天气网(http://www.weather.com.cn)提供了查询天气的 API,通过传入城市 id, 可以获得当前城市的天气信息,API 相关信息如下: 规格 描述 主机地址 http:/ ...
- Visual Studio Code 配置C/C++环境
0. 前言 VS Code 是微软发布一款跨平台的源代码编辑器,其拥有强大的功能和丰富的扩展,使之能适合编写许多语言. 本文面向初学者(但不是纯小白),分享一点我配置C/C++的经验. 本文所有内容均 ...
- 网络(bzoj 4538)
Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器的路径上的所有服 ...
- Topcoder SRM 608 div1 题解
Easy(300pts): 题目大意:有n个盒子,一共有S个苹果,每个盒子有多少个苹果不知道,但是知道每个盒子的苹果下限和上限.现在要至少选择X个苹果,问如果要保证无论如何都能获得至少X个苹果,至少需 ...
- DotNETCore 学习笔记 依赖注入和多环境
Dependency Injection ------------------------------------------------------------------------ ASP.NE ...
- JHDU 2601 An easy problem (数学 )
title: An easy problem 数学 杭电2601 tags: [数学] 题目链接 Problem Description When Teddy was a child , he was ...
- 2.kafka单节点broker的安装与启动
下载kafka,http://kafka.apache.org/downloads kafka下面的文件结构如下: 进入bin目录,启动kafka之前要先启动zookeeper ./zookeeper ...