切点是我们真正应用在哪些方法上,增强的那些方法上,就是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. 19-10-23-L-Mor

    ZJ一下: 挺好,T2打表差点出规律(最近拿PFGYL硬卡提升自己几乎没有的打表水平) T1竟然……是个××题 T3的Floyd写死了. T1 简单思考会发现……直接全异或起来就AC 话说T1真叫最大 ...

  2. VS2012与windos版本不兼容问题

    昨天晚上加完班,想着把windows更新下.今天上午就发现再运行VS报错了,提示VS2012与windows版本不兼容,打开.sln文件后,VS自动关闭.错误如下: 查看后网上提示安装VS2012的一 ...

  3. 自己动手编写JEECMS自定义栏目统计标签

    今天想在给Java客二级版面加入栏目文章统计效果,如下图, 查看JEECMS的源代码发现开发者版本还没有类似现成的统计标签,一种解决的办法是使用现有的JEECMS标签,像这样Struts( [@cms ...

  4. 稀疏表示step by step(转)

    原文地址:稀疏表示step by step(转)作者:野火春风 稀疏表示step by step(1)     声明:本人属于绝对的新手,刚刚接触“稀疏表示”这个领域.之所以写下以下的若干个连载,是鼓 ...

  5. 啊啊我找不到web.xml怎么办呀~~

    创建Dymamic Web Project的时候不要忘记勾选这玩意(about servlet) 其实还有一个办法就是右键点击你的web项目名--->Java EE Tools-->Gen ...

  6. Leetcode441Arranging Coins排列硬币

    你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 ...

  7. TZ_09_常用jQuery操作

    1.实现对chexkbox进行批量删除 checkbox属性 <td><input name="ids" type="checkbox" va ...

  8. 现在学习 JavaScript 的哪种技术更好:Angular、jQuery 还是 Node.js?(转)

    本文选自<开发者头条>1 月 7 日最受欢迎文章 Top 3,感谢作者 @WEB资源网 分享. 欢迎分享:http://toutiao.io/contribute 这是一个发布在 Quor ...

  9. FreeMarker 对null值的处理技巧

    以下引用官方描述: ? The FreeMarker template language doesn't know the Java language null at all. It doesn't ...

  10. leetcode 839 Similar String Groups

    题目 Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that ...