切点是我们真正应用在哪些方法上,增强的那些方法上,就是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. PHP7中标量类型declare的用法详解

    这篇文章主要介绍了PHP7标量类型declare用法,结合实例形式分析了PHP7中标量类型declare的功能.特性与相关使用技巧,需要的朋友可以参考下 本文实例讲述了PHP7标量类型declare用 ...

  2. Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage; increase this mysqld variable and try again

    WARN: SQL Error: , SQLState: HY000 八月 , :: 下午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logEx ...

  3. JQuery学习:jquery对象和js对象区别和转换

    JQuery对象与JS对象区别与转换 1.JQuery对象在操作时,更加方便 2.JQuery对象和js对象方法不通用 3.两者相互转换 *  jq -- > js:jq对象[索引]  或者  ...

  4. UVa 12034 Race 递推?

    一开始是想排列组合做的,排列组合感觉确实可以推出公式,但是复杂度嘛.. dp[i][j]表示有i只马,j个名次的方法数,显然j<=i,然后递推公式就很好写了,一只马新加进来要么与任意一个名次的马 ...

  5. LUOGU P1978 集合

    题目描述 集合是数学中的一个概念,用通俗的话来讲就是:一大堆数在一起就构成了集合.集合有如 下的特性: •无序性:任一个集合中,每个元素的地位都是相同的,元素之间是无序的. •互异性:一个集合中,任何 ...

  6. 主流浏览器HTML5视频格式差异

    因最近在研究video.js,现在遇到的问题是在js中设置了swf,但是在ie8下只是显示黑屏并没有播放视频,在网上进行搜索时查到了有关各个浏览器支持哪些视频格式的文章,现在此记录下,方便以后查阅. ...

  7. MySQL加快批量更新 UPDATE优化

    如果是更新为同样的内容,没啥难度,直接在where里面下功夫就好了,大家都懂,我要说的是针对更新内容不一样的情况 首先,先看看网上转载的方法: mysql 批量更新如果一条条去更新效率是相当的慢, 循 ...

  8. Ubuntu18.04 修改IP地址、查看网关、防火墙

    1. Ubuntu18.04 修改IP地址 修改 sudo vim /etc/netplan/50-cloud-init.yaml文件 # This file is generated from in ...

  9. 禁用/移除WordPress页面的评论功能

    对于某些类型的WordPress站点,也许不需要在页面(page)提供评论功能,那么你可以通过下面的方法,很容易就禁用或移除WordPress页面的评论功能. 方法1:在页面编辑界面取消该页面的评论功 ...

  10. idea 创建properties配置文件

    [转载]原文链接:https://blog.csdn.net/caoPengFlying/article/details/78660379 我们在j2ee当中,连接数据库的时候经常会用到propert ...