切点是我们真正应用在哪些方法上,增强的那些方法上,就是add()、update()和find().delete()是没用的.没用的话就是这三个方法.为什么要定义一个切点呢?

所以可以直接在切面中定义一个切点.注解肯定要修饰一个属性或者是方法.

package cn.itcast.spring3.demo1;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; //我的切面类
/**
* 切面类:就是切点与增强的结合
* 切点就是哪些方法要使用什么增强
* @author zhongzh
*
*/
@Aspect //用来定义切面
public class MyAspect {
//写增强的代码,假设现在做的是一个前置增强.
@Before(value="execution(* cn.itcast.spring3.demo1.UserDao.add(..))") //前置增强的注解是Before 刚一写完Before就报错了,它里面肯定是有属性没写的.学过JDK的注解,如果只想使用它里面的value属性,value是可以不写的.
//现在只对UserDao.add()应用前置增强.
public void before(JoinPoint joinPoint){//JointPoint是可以用作增强的一个点.就是被增强的那个方法的描述.
//System.out.println("前置增强......");
System.out.println("前置增强......"+joinPoint);
}
@AfterReturning(value="execution(* cn.itcast.spring3.demo1.UserDao.update(..))",returning="returnVal")//这里也是要写切面表达式
//public void afterReturn(){
public void afterReturn(Object returnVal){
System.out.println("后置增强....方法的返回值:"+returnVal);
}
//@Around(value="execution(* cn.itcast.spring3.demo1.UserDao.find(..))")
@Around(value="MyAspect.myPointCut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕前增强.....");
Object obj = proceedingJoinPoint.proceed();
System.out.println("环绕后增强.....");
return obj;//如果想要阻止目标方法的执行这里可以return null
}
//@AfterThrowing(value="execution(* cn.itcast.spring3.demo1.UserDao.find(..))",throwing="e")
@AfterThrowing(value="MyAspect.myPointCut()",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("不好了 出异常了!!!!"+e.getMessage());
}
//@After(value="execution(* cn.itcast.spring3.demo1.UserDao.find(..))") //最终通知 类似于try catch finally里面的那个finally
//@After("MyAspect.myPointCut()")
@After(value="MyAspect.myPointCut()")
public void after(){
System.out.println("最终通知....");//最终通知它不管你什么情况下都会执行
}
@Pointcut("execution(* cn.itcast.spring3.demo1.UserDao.find(..))")
private void myPointCut(){//这个方法没有实际的意义,它的目的主要是为了我们在这上面定义一个通用的表达式 } }
package cn.itcast.spring3.demo1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringTest1 {
@Autowired
@Qualifier("userDao")
private UserDao userDao;
@Test
public void demo1(){
userDao.add();
userDao.delete();
userDao.update();
userDao.find();
}
}
package cn.itcast.spring3.demo1;

public class UserDao {
public void add(){
System.out.println("添加用户");
}
//public void update(){
public int update(){
System.out.println("修改用户");
return 1;
}
public void delete(){
System.out.println("删除用户");
}
public void find(){
//int i = 1/0;
System.out.println("查询用户");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引AOP的约束了,Schema里面必须是带有AOP的. -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自动的生成代理 底层就是AnnotationAwareAspectJAutoProxyCreator-->
<aop:aspectj-autoproxy />
<bean id="userDao" class="cn.itcast.spring3.demo1.UserDao"></bean> <bean id="myAspect" class="cn.itcast.spring3.demo1.MyAspect"></bean> </beans>

day39 10-Spring的AOP:基于AspectJ的切点定义的更多相关文章

  1. Spring的AOP基于AspectJ的注解方式开发2

    参考自黑马培训机构 上一篇博客提到了在配置文件中开启aop的注解开发,以及简单使用了@Before,@Aspect 这是为了告诉spring为前置通知和切面类 接下来介绍aop的注解的通知类型,和切入 ...

  2. Spring的AOP基于AspectJ的注解方式开发1

    参考自黑马培训机构 创建项目,引入jar包 编写目标类,切面类并完成配置 package spring.day2_aop2; /* * 编写目标类 */ public class OrderDao { ...

  3. Spring的AOP基于AspectJ的注解方式开发3

    上上偏博客介绍了@Aspect,@Before 上篇博客介绍了spring的AOP开发的注解通知类型:@Before,@AfterThrowing,@After,@AfterReturning,@Ar ...

  4. 10 Spring框架 AOP (三) Spring对AspectJ的整合

    上两节我们讲了Spring对AOP的实现,但是在我们的开发中我们不太使用Spring自身的对AOP的实现,而是使用AspectJ,AspectJ是一个面向切面的框架,它扩展了Java语言.Aspect ...

  5. Spring AOP 基于AspectJ

    简介 AspectJ是一个基于Java语言的AOP框架,Spring2.0以后新增了对AspectJ切点表达式支持.因为Spring1.0的时候Aspectj还未出现; AspectJ1.5中新增了对 ...

  6. Spring 使用AOP——基于注解配置

    首先,使用注解实现AOP是基于AspectJ方式的. 创建包含切点方法的类 package cn.ganlixin.test; import org.aspectj.lang.annotation.P ...

  7. 第三章 AOP 基于@AspectJ的AOP

    在前面,我们分别使用Pointcut.Advice.Advisor接口来描述切点.增强.切面.而现在我们使用@AdpectJ注解来描述. 在下面的例子中,我们是使用Spring自动扫描和管理Bean. ...

  8. spring(二) AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

  9. AOP——基于AspectJ的注解来实现AOP操作

    1.使用注解方式实现AOP操作 第一步:创建对象 <!-- 创建对象 --> <bean id="book" class="com.bjxb.aop.B ...

随机推荐

  1. PHP declare 之 strict_types=1

    PHP中申明 declare(strict_types=1)的作用:  strict_types=1 及开启严格模式.默认是弱类型校验.具体严格模式和普通模式的区别见下面代码. code1: < ...

  2. 在Bat批处理中调用Powershell脚本

    ##如何在BAT中调用powershell,把下面代码另存为bat格式pushd %~dp0powershell.exe -command ^  "& {set-executionp ...

  3. https://vjudge.net/problem/2198220/origin

    https://vjudge.net/problem/2198220/origin枚举等差数列第一个和第二个,然后二分确定数列后面是否存在,复杂度比较玄学,卡过了. #include<iostr ...

  4. bzoj 2503 相框——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2503 我也知道应该只关注度数. #include<iostream> #incl ...

  5. ubuntu中vi下删除键和上下左右键输入字符异常(ABCD)

    刚安装的Ubuntu系统,使用vi编辑文本的时候, 出现以下现象: 点删除键输入了 D 回车无效 上下左右为字母 光标乱跳 原因: 自带的vi功能问题 解决: 卸载原有vi,重新安装完整版本vim 执 ...

  6. 使用pdf.js在移动端预览pdf文档

    pdf.js 是一个技术原型主要用于在 HTML5 平台上展示 PDF 文档,无需任何本地技术支持. 在线演示地址:http://mozilla.github.com/pdf.js/web/viewe ...

  7. Functional Programming Contest - September'14

    比赛链接 A题 -- Enter your code here. Read input from STDIN. Print output to STDOUT main = do x <- get ...

  8. Hackerrank--String Function Calculation(后缀数组)

    题目链接 Jane loves string more than anything. She made a function related to the string some days ago a ...

  9. WPF 的另类资源方式 Resources.resx

      类似Winform的搞法,可以把资源放到Resources.resx中. 1.字符串 打开这个编辑器后,输入Name和Value就可以了. CS代码里面,很简单的调用: var title = W ...

  10. nginx的四个基本功能

    Nginx能做什么 1.反向代理2.负载均衡3.HTTP服务器(包含动静分离)4.正向代理 以上就是做网站小编了解到的Nginx在不依赖第三方模块能处理的事情,下面详细说明每种功能怎么做 1.反向代理 ...