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 注解配置的更多相关文章

  1. Spring之AOP注解配置

    1.导入相应jar包 2.引入约束并配置XML文件 <beans xmlns="http://www.springframework.org/schema/beans" xm ...

  2. spring aop注解配置

    spring aop是面向切面编程,使用了动态代理的技术,这样可以使业务逻辑的代码不掺入其他乱七八糟的代码 可以在切面上实现合法性校验.权限检验.日志记录... spring aop 用的多的有两种配 ...

  3. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  4. 【Spring】AOP注解方式实现机制

    一.概述 二.@EnableAspectJAutoProxy 注解分析 三.分析AnnotationAwareAspectJAutoProxyCreator 四.执行流程 1. registerBea ...

  5. springAop:Aop(Xml)配置,Aop注解配置,spring_Aop综合案例,Aop底层原理分析

    知识点梳理 课堂讲义 0)回顾Spring体系结构 Spring的两个核心:IoC和AOP 1)AOP简介 1.1)OOP开发思路 OOP规定程序开发以类为模型,一切围绕对象进行,OOP中完成某个任务 ...

  6. Spring MVC4 纯注解配置教程

    阅读本文需要又一定的sping基础,最起码要成功的运行过一个SpringMvc项目. 在传统的Spring项目中,我们要写一堆的XML文件.而这些XML文件格式要求又很严格,很不便于开发.而网上所谓的 ...

  7. spring mvc 基于注解 配置默认 handlermapping

    spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...

  8. Spring IOC-基于注解配置的容器

    Spring中提供了基于注解来配置bean的容器,即AnnotationConfigApplicationContext 1. 开始 先看看在Spring家族中,AnnotationConfigApp ...

  9. 采用spring的schedule注解配置定时任务

    1 在springmvc配置文件中新增以下配置 <!-- 此处对于定时时间的配置会被注解中的时间配置覆盖,因此,以注解配置为准 --> <task:scheduled-tasks s ...

随机推荐

  1. 7月17号day9总结

    今天学习过程和小结 今天学习了如何使用idea操作hdfs. public class HDFSTest {    Configuration configuration;    FileSystem ...

  2. python最简单发送邮件

    #!/usr/bin/env python #coding:utf8 #Author:lsp #Date:下午5:51:13 #Version:0.1 #Function: #导入smtplib和MI ...

  3. 「6月雅礼集训 2017 Day2」C

    [题目大意] 有一棵n个点的完全二叉树,边权均为1,每个点有小鸟容量c[i] 依次来了m只小鸟,第i只小鸟初始位置在pos[i]上,问来了x只小鸟的时候,怎样安排小鸟的路线可以使得小鸟移动的边权和最小 ...

  4. codeforces 854 problem E

    E. Boredom Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time t ...

  5. Python小程序之动态修改Haproxy配置文件

    需求如下: 1.动态的查询添加删除haproxy节点信息 2.程序功能:add(添加).Del(删除).Query(查询) 3.添加时实例字符串为:  {'backend': 'www.oldboy. ...

  6. 转:在android中button响应的两种方式

    1. 在布局文件中添加button的监听名字 Android:onClick="buttonOnClick" 例如: <Button android:id="@+i ...

  7. appium===setup/setupclass的区别,以及@classmathod的使用方法

    一.装饰器 1.用setUp与setUpClass区别 setup():每个测试case运行前运行 teardown():每个测试case运行完后执行 setUpClass():必须使用@classm ...

  8. python基础===修改idle的输入风格

    http://blog.csdn.net/aq_cainiao_aq/article/details/51701861

  9. Linux内核:关于中断你需要知道的【转】

    转自:http://blog.csdn.net/duqi_2009/article/details/38009717 1.中断处理程序与其他内核函数真正的区别在于,中断处理程序是被内核调用来相应中断的 ...

  10. js中给easyui的一列添加按钮

    $("#totalTb").datagrid({ columns: [[                { field: 'ENTITY_ACTNAME', title: '活动名 ...