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 ...
随机推荐
- Web应用程序开发,基于Ajax技术的JavaScript树形控件
感谢http://www.cnblogs.com/dgrew/p/3181769.html#undefined 在Web应用程序开发领域,基于Ajax技术的JavaScript树形控件已经被广泛使用, ...
- PHP文件操作函数二
PHP部分文件访问函数总结: 1.filetype("文件路径") //可以输出相关文件类型,返回之为:dir/file... 2.stat("文件名") / ...
- 【Foreign】阅读 [线段树][DP]
阅读 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 0 10 4 10 2 3 10 8 ...
- 【Mysql优化】聚簇索引与非聚簇索引概念
必须为主键字段创建一个索引,这个索引就是所谓的"主索引".主索引与唯一索引的唯一区别是:前者在定义时使用的关键字是PRIMARY而不是UNIQUE. 首先明白两句话: innod ...
- Swift : missing argument label 'xxx' in call
http://stackoverflow.com/questions/24050844/swift-missing-argument-label-xxx-in-call up vote37down v ...
- linux进程的休眠(等待队列)【转】
转自:http://www.cnblogs.com/noaming1900/archive/2011/01/14/1935526.html (转载) bojan 收录于2010-10-09 阅读数: ...
- Linux设备模型(3)_Uevent【转】
转自:http://www.wowotech.net/device_model/uevent.html 1. Uevent的功能 Uevent是Kobject的一部分,用于在Kobject状态发生改变 ...
- c++文件流写入到execl中
#include <iostream> #include <fstream> #include <string> using namespace std; int ...
- 1.flask视图和URL
1.第一个flask程序 from flask import Flask ''' Flask这个类是项目的核心,以后很多操作都是基于这个类的对象 注册URL等等,都是基于这个类 ''' app = F ...
- 服务器重启之后wdcp打不开【解决】
service wdapache restart