对于AOP中切入点表达式,我们总共会学习三个内容,分别是语法格式
、通配符
和书写技巧
。
首先我们先要明确两个概念:
对于切入点的描述,我们其实是有两种方式的,先来看下前面的例子
描述方式一:执行com.itheima.dao包下的BookDao接口中的无参数update方法
execution(void com.itheima.dao.BookDao.update())
描述方式二:执行com.itheima.dao.impl包下的BookDaoImpl类中的无参数update方法
execution(void com.itheima.dao.impl.BookDaoImpl.update())
因为调用接口方法的时候最终运行的还是其实现类的方法,所以上面两种描述方式都是可以的。
对于切入点表达式的语法为:
对于这个格式,我们不需要硬记,通过一个例子,理解它:
execution(public User com.itheima.service.UserService.findById(int))
切入点表达式就是要找到需要增强的方法,所以它就是对一个具体方法的描述,但是方法的定义会有很多,所以如果每一个方法对应一个切入点表达式,想想这块就会觉得将来编写起来会比较麻烦,有没有更简单的方式呢?
就需要用到下面所学习的通配符。
我们使用通配符描述切入点,主要的目的就是简化之前的配置,具体都有哪些通配符可以使用?
*
:单个独立的任意符号,可以独立出现,也可以作为前缀或者后缀的匹配符出现
execution(public * com.itheima.*.UserService.find*(*))
匹配com.itheima包下的任意包中的UserService类或接口中所有find开头的带有一个参数的方法
..
:多个连续的任意符号,可以独立出现,常用于简化包名与参数的书写
execution(public User com..UserService.findById(..))
匹配com包下的任意包中的UserService类或接口中所有名称为findById的方法
+
:专用于匹配子类类型
execution(* *..*Service+.*(..))
这个使用率较低,描述子类的,咱们做JavaEE开发,继承机会就一次,使用都很慎重,所以很少用它。*Service+,表示所有以Service结尾的接口的子类。
接下来,我们把案例中使用到的切入点表达式来分析下:
execution(void com.itheima.dao.BookDao.update())
匹配接口,能匹配到
execution(void com.itheima.dao.impl.BookDaoImpl.update())
匹配实现类,能匹配到
execution(* com.itheima.dao.impl.BookDaoImpl.update())
返回值任意,能匹配到
execution(* com.itheima.dao.impl.BookDaoImpl.update(*))
返回值任意,但是update方法必须要有一个参数,无法匹配,要想匹配需要在update接口和实现类添加参数
execution(void com.*.*.*.*.update())
返回值为void,com包下的任意包三层包下的任意类的update方法,匹配到的是实现类,能匹配
execution(void com.*.*.*.update())
返回值为void,com包下的任意两层包下的任意类的update方法,匹配到的是接口,能匹配
execution(void *..update())
返回值为void,方法名是update的任意包下的任意类,能匹配
execution(* *..*(..))
匹配项目中任意类的任意方法,能匹配,但是不建议使用这种方式,影响范围广
execution(* *..u*(..))
匹配项目中任意包任意类下只要以u开头的方法,update方法能满足,能匹配
execution(* *..*e(..))
匹配项目中任意包任意类下只要以e结尾的方法,update和save方法能满足,能匹配
execution(void com..*())
返回值为void,com包下的任意包任意类任意方法,能匹配,*代表的是方法
execution(* com.itheima.*.*Service.find*(..))
将项目中所有业务层方法的以find开头的方法匹配
execution(* com.itheima.*.*Service.save*(..))
将项目中所有业务层方法的以save开头的方法匹配
后面两种更符合我们平常切入点表达式的编写规则
对于切入点表达式的编写其实是很灵活的,那么在编写的时候,有没有什么好的技巧让我们用用:
我们先来回顾下AOP通知:
通知具体要添加到切入点的哪里?
共提供了5种通知类型:
为了更好的理解这几种通知类型,我们来看一张图
(1)前置通知,追加功能到方法执行前,类似于在代码1或者代码2添加内容
(2)后置通知,追加功能到方法执行后,不管方法执行的过程中有没有抛出异常都会执行,类似于在代码5添加内容
(3)返回后通知,追加功能到方法执行后,只有方法正常执行结束后才进行,类似于在代码3添加内容,如果方法执行抛出异常,返回后通知将不会被添加
(4)抛出异常后通知,追加功能到方法抛出异常后,只有方法执行出异常才进行,类似于在代码4添加内容,只有方法抛出异常后才会被添加
(5)环绕通知,环绕通知功能比较强大,它可以追加功能到方法执行的前后,这也是比较常用的方式,它可以实现其他四种通知类型的功能,具体是如何实现的,需要我们往下学习。
创建一个Maven项目
pom.xml添加Spring依赖
org.springframework spring-context 5.2.10.RELEASE org.aspectj aspectjweaver 1.9.4
添加BookDao和BookDaoImpl类
public interface BookDao {public void update();public int select();
}@Repository
public class BookDaoImpl implements BookDao {public void update(){System.out.println("book dao update ...");}public int select() {System.out.println("book dao select is running ...");return 100;}
}
创建Spring的配置类
@Configuration
@ComponentScan("com.itheima")
@EnableAspectJAutoProxy
public class SpringConfig {
}
创建通知类
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(void com.itheima.dao.BookDao.update())")private void pt(){}public void before() {System.out.println("before advice ...");}public void after() {System.out.println("after advice ...");}public void around(){System.out.println("around before advice ...");System.out.println("around after advice ...");}public void afterReturning() {System.out.println("afterReturning advice ...");}public void afterThrowing() {System.out.println("afterThrowing advice ...");}
}
编写App运行类
public class App {public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);BookDao bookDao = ctx.getBean(BookDao.class);bookDao.update();}
}
最终创建好的项目结构如下:
修改MyAdvice,在before方法上添加@Before注解
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(void com.itheima.dao.BookDao.update())")private void pt(){}@Before("pt()")//此处也可以写成 @Before("MyAdvice.pt()"),不建议public void before() {System.out.println("before advice ...");}
}
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(void com.itheima.dao.BookDao.update())")private void pt(){}@Before("pt()")public void before() {System.out.println("before advice ...");}@After("pt()")public void after() {System.out.println("after advice ...");}
}
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(void com.itheima.dao.BookDao.update())")private void pt(){}@Around("pt()")public void around(){System.out.println("around before advice ...");System.out.println("around after advice ...");}
}
运行结果中,通知的内容打印出来,但是原始方法的内容却没有被执行。
因为环绕通知需要在原始方法的前后进行增强,所以环绕通知就必须要能对原始操作进行调用,具体如何实现?
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(void com.itheima.dao.BookDao.update())")private void pt(){}@Around("pt()")public void around(ProceedingJoinPoint pjp) throws Throwable{System.out.println("around before advice ...");//表示对原始操作的调用pjp.proceed();System.out.println("around after advice ...");}
}
**说明:**proceed()为什么要抛出异常?
原因很简单,看下源码就知道了
再次运行,程序可以看到原始方法已经被执行了
(1)原始方法有返回值的处理
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(void com.itheima.dao.BookDao.update())")private void pt(){}@Pointcut("execution(int com.itheima.dao.BookDao.select())")private void pt2(){}@Around("pt2()")public void aroundSelect(ProceedingJoinPoint pjp) throws Throwable {System.out.println("around before advice ...");//表示对原始操作的调用pjp.proceed();System.out.println("around after advice ...");}
}
public class App {public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);BookDao bookDao = ctx.getBean(BookDao.class);int num = bookDao.select();System.out.println(num);}
}
运行后会报错,错误内容为:
Exception in thread “main” org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int com.itheima.dao.BookDao.select()
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:226)
at com.sun.proxy.$Proxy19.select(Unknown Source)
at com.itheima.App.main(App.java:12)
错误大概的意思是:空的返回不匹配原始方法的int返回
所以如果我们使用环绕通知的话,要根据原始方法的返回值来设置环绕通知的返回值,具体解决方案为:
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(void com.itheima.dao.BookDao.update())")private void pt(){}@Pointcut("execution(int com.itheima.dao.BookDao.select())")private void pt2(){}@Around("pt2()")public Object aroundSelect(ProceedingJoinPoint pjp) throws Throwable {System.out.println("around before advice ...");//表示对原始操作的调用Object ret = pjp.proceed();System.out.println("around after advice ...");return ret;}
}
说明:
为什么返回的是Object而不是int的主要原因是Object类型更通用。
在环绕通知中是可以对原始方法返回值进行修改的。
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(void com.itheima.dao.BookDao.update())")private void pt(){}@Pointcut("execution(int com.itheima.dao.BookDao.select())")private void pt2(){}@AfterReturning("pt2()")public void afterReturning() {System.out.println("afterReturning advice ...");}
}
**注意:**返回后通知是需要在原始方法select
正常执行后才会被执行,如果select()
方法执行的过程中出现了异常,那么返回后通知是不会被执行。后置通知是不管原始方法有没有抛出异常都会被执行。这个案例大家下去可以自己练习验证下。
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(void com.itheima.dao.BookDao.update())")private void pt(){}@Pointcut("execution(int com.itheima.dao.BookDao.select())")private void pt2(){}@AfterReturning("pt2()")public void afterThrowing() {System.out.println("afterThrowing advice ...");}
}
**注意:**异常后通知是需要原始方法抛出异常,可以在select()
方法中添加一行代码int i = 1/0
即可。如果没有抛异常,异常后通知将不会被执行。
学习完这5种通知类型,我们来思考下环绕通知是如何实现其他通知类型的功能的?
因为环绕通知是可以控制原始方法执行的,所以我们把增强的代码写在调用原始方法的不同位置就可以实现不同的通知类型的功能,如:
名称 | @After |
---|---|
类型 | 方法注解 |
位置 | 通知方法定义上方 |
作用 | 设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法后运行 |
名称 | @AfterReturning |
---|---|
类型 | 方法注解 |
位置 | 通知方法定义上方 |
作用 | 设置当前通知方法与切入点之间绑定关系,当前通知方法在原始切入点方法正常执行完毕后执行 |
名称 | @AfterThrowing |
---|---|
类型 | 方法注解 |
位置 | 通知方法定义上方 |
作用 | 设置当前通知方法与切入点之间绑定关系,当前通知方法在原始切入点方法运行抛出异常后执行 |
名称 | @Around |
---|---|
类型 | 方法注解 |
位置 | 通知方法定义上方 |
作用 | 设置当前通知方法与切入点之间的绑定关系,当前通知方法在原始切入点方法前后运行 |
环绕通知注意事项
介绍完这么多种通知类型,具体该选哪一种呢?
我们可以通过一些案例加深下对通知类型的学习。
这个需求也比较简单,前面我们在介绍AOP的时候已经演示过:
这个案例的目的是查看每个业务层执行的时间,这样就可以监控出哪个业务比较耗时,将其查找出来方便优化。
具体实现的思路:
(1) 开始执行方法之前记录一个时间
(2) 执行方法
(3) 执行完方法之后记录一个时间
(4) 用后一个时间减去前一个时间的差值,就是我们需要的结果。
所以要在方法执行的前后添加业务,经过分析我们将采用环绕通知
。
**说明:**原始方法如果只执行一次,时间太快,两个时间差可能为0,所以我们要执行万次来计算时间差。
创建一个Maven项目
pom.xml添加Spring依赖
org.springframework spring-context 5.2.10.RELEASE org.springframework spring-jdbc 5.2.10.RELEASE org.springframework spring-test 5.2.10.RELEASE org.aspectj aspectjweaver 1.9.4 mysql mysql-connector-java 5.1.47 com.alibaba druid 1.1.16 org.mybatis mybatis 3.5.6 org.mybatis mybatis-spring 1.3.0 junit junit 4.12 test
添加AccountService、AccountServiceImpl、AccountDao与Account类
public interface AccountService {void save(Account account);void delete(Integer id);void update(Account account);List findAll();Account findById(Integer id);
}@Service
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;public void save(Account account) {accountDao.save(account);}public void update(Account account){accountDao.update(account);}public void delete(Integer id) {accountDao.delete(id);}public Account findById(Integer id) {return accountDao.findById(id);}public List findAll() {return accountDao.findAll();}
}
public interface AccountDao {@Insert("insert into tbl_account(name,money)values(#{name},#{money})")void save(Account account);@Delete("delete from tbl_account where id = #{id} ")void delete(Integer id);@Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ")void update(Account account);@Select("select * from tbl_account")List findAll();@Select("select * from tbl_account where id = #{id} ")Account findById(Integer id);
}public class Account implements Serializable {private Integer id;private String name;private Double money;//setter..getter..toString方法省略
}
resources下提供一个jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false
jdbc.username=root
jdbc.password=root
创建相关配置类
//Spring配置类:SpringConfig
@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}
//JdbcConfig配置类
public class JdbcConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String userName;@Value("${jdbc.password}")private String password;@Beanpublic DataSource dataSource(){DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;}
}
//MybatisConfig配置类
public class MybatisConfig {@Beanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();ssfb.setTypeAliasesPackage("com.itheima.domain");ssfb.setDataSource(dataSource);return ssfb;}@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc = new MapperScannerConfigurer();msc.setBasePackage("com.itheima.dao");return msc;}
}
编写Spring整合Junit的测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class AccountServiceTestCase {@Autowiredprivate AccountService accountService;@Testpublic void testFindById(){Account ac = accountService.findById(2);}@Testpublic void testFindAll(){List all = accountService.findAll();}}
最终创建好的项目结构如下:
在Spring的主配置文件SpringConfig类中添加注解
@EnableAspectJAutoProxy
该类要被Spring管理,需要添加@Component
要标识该类是一个AOP的切面类,需要添加@Aspect
配置切入点表达式,需要添加一个方法,并添加@Pointcut
@Component
@Aspect
public class ProjectAdvice {//配置业务层的所有方法@Pointcut("execution(* com.itheima.service.*Service.*(..))")private void servicePt(){}public void runSpeed(){}
}
在runSpeed()方法上添加@Around
@Component
@Aspect
public class ProjectAdvice {//配置业务层的所有方法@Pointcut("execution(* com.itheima.service.*Service.*(..))")private void servicePt(){}//@Around("ProjectAdvice.servicePt()") 可以简写为下面的方式@Around("servicePt()")public Object runSpeed(ProceedingJoinPoint pjp){Object ret = pjp.proceed();return ret;}
}
**注意:**目前并没有做任何增强
@Component
@Aspect
public class ProjectAdvice {//配置业务层的所有方法@Pointcut("execution(* com.itheima.service.*Service.*(..))")private void servicePt(){}//@Around("ProjectAdvice.servicePt()") 可以简写为下面的方式@Around("servicePt()")public void runSpeed(ProceedingJoinPoint pjp){long start = System.currentTimeMillis();for (int i = 0; i < 10000; i++) {pjp.proceed();}long end = System.currentTimeMillis();System.out.println("业务层接口万次执行时间: "+(end-start)+"ms");}
}
**注意:**因为程序每次执行的时长是不一样的,所以运行多次最终的结果是不一样的。
目前程序所面临的问题是,多个方法一起执行测试的时候,控制台都打印的是:
业务层接口万次执行时间:xxxms
我们没有办法区分到底是哪个接口的哪个方法执行的具体时间,具体如何优化?
@Component
@Aspect
public class ProjectAdvice {//配置业务层的所有方法@Pointcut("execution(* com.itheima.service.*Service.*(..))")private void servicePt(){}//@Around("ProjectAdvice.servicePt()") 可以简写为下面的方式@Around("servicePt()")public void runSpeed(ProceedingJoinPoint pjp){//获取执行签名信息Signature signature = pjp.getSignature();//通过签名获取执行操作名称(接口名)String className = signature.getDeclaringTypeName();//通过签名获取执行操作名称(方法名)String methodName = signature.getName();long start = System.currentTimeMillis();for (int i = 0; i < 10000; i++) {pjp.proceed();}long end = System.currentTimeMillis();System.out.println("万次执行:"+ className+"."+methodName+"---->" +(end-start) + "ms");}
}
补充说明
当前测试的接口执行效率仅仅是一个理论值,并不是一次完整的执行过程。
这块只是通过该案例把AOP的使用进行了学习,具体的实际值是有很多因素共同决定的。
目前我们写AOP仅仅是在原始方法前后追加一些操作,接下来我们要说说AOP中数据相关的内容,我们将从获取参数
、获取返回值
和获取异常
三个方面来研究切入点的相关信息。
前面我们介绍通知类型的时候总共讲了五种,那么对于这五种类型都会有参数,返回值和异常吗?
我们先来一个个分析下:
创建一个Maven项目
pom.xml添加Spring依赖
org.springframework spring-context 5.2.10.RELEASE org.aspectj aspectjweaver 1.9.4
添加BookDao和BookDaoImpl类
public interface BookDao {public String findName(int id);
}
@Repository
public class BookDaoImpl implements BookDao {public String findName(int id) {System.out.println("id:"+id);return "itcast";}
}
创建Spring的配置类
@Configuration
@ComponentScan("com.itheima")
@EnableAspectJAutoProxy
public class SpringConfig {
}
编写通知类
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")private void pt(){}@Before("pt()")public void before() {System.out.println("before advice ..." );}@After("pt()")public void after() {System.out.println("after advice ...");}@Around("pt()")public Object around() throws Throwable{Object ret = pjp.proceed();return ret;}@AfterReturning("pt()")public void afterReturning() {System.out.println("afterReturning advice ...");}@AfterThrowing("pt()")public void afterThrowing() {System.out.println("afterThrowing advice ...");}
}
编写App运行类
public class App {public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);BookDao bookDao = ctx.getBean(BookDao.class);String name = bookDao.findName(100);System.out.println(name);}
}
最终创建好的项目结构如下:
在方法上添加JoinPoint,通过JoinPoint来获取参数
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")private void pt(){}@Before("pt()")public void before(JoinPoint jp) Object[] args = jp.getArgs();System.out.println(Arrays.toString(args));System.out.println("before advice ..." );}//...其他的略
}
运行App类,可以获取如下内容,说明参数100已经被获取
思考:方法的参数只有一个,为什么获取的是一个数组?
因为参数的个数是不固定的,所以使用数组更通配些。
如果将参数改成两个会是什么效果呢?
(1)修改BookDao接口和BookDaoImpl实现类
public interface BookDao {public String findName(int id,String password);
}
@Repository
public class BookDaoImpl implements BookDao {public String findName(int id,String password) {System.out.println("id:"+id);return "itcast";}
}
(2)修改App类,调用方法传入多个参数
public class App {public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);BookDao bookDao = ctx.getBean(BookDao.class);String name = bookDao.findName(100,"itheima");System.out.println(name);}
}
(3)运行App,查看结果,说明两个参数都已经被获取到
说明:
使用JoinPoint的方式获取参数适用于前置
、后置
、返回后
、抛出异常后
通知。剩下的大家自行去验证。
环绕通知使用的是ProceedingJoinPoint,因为ProceedingJoinPoint是JoinPoint类的子类,所以对于ProceedingJoinPoint类中应该也会有对应的getArgs()
方法,我们去验证下:
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")private void pt(){}@Around("pt()")public Object around(ProceedingJoinPoint pjp)throws Throwable {Object[] args = pjp.getArgs();System.out.println(Arrays.toString(args));Object ret = pjp.proceed();return ret;}//其他的略
}
运行App后查看运行结果,说明ProceedingJoinPoint也是可以通过getArgs()获取参数
注意:
pjp.proceed()方法是有两个构造方法,分别是:
调用无参数的proceed,当原始方法有参数,会在调用的过程中自动传入参数
所以调用这两个方法的任意一个都可以完成功能
但是当需要修改原始方法的参数时,就只能采用带有参数的方法,如下:
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")private void pt(){}@Around("pt()")public Object around(ProceedingJoinPoint pjp) throws Throwable{Object[] args = pjp.getArgs();System.out.println(Arrays.toString(args));args[0] = 666;Object ret = pjp.proceed(args);return ret;}//其他的略
}
有了这个特性后,我们就可以在环绕通知中对原始方法的参数进行拦截过滤,避免由于参数的问题导致程序无法正确运行,保证代码的健壮性。
对于返回值,只有返回后AfterReturing
和环绕Around
这两个通知类型可以获取,具体如何获取?
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")private void pt(){}@Around("pt()")public Object around(ProceedingJoinPoint pjp) throws Throwable{Object[] args = pjp.getArgs();System.out.println(Arrays.toString(args));args[0] = 666;Object ret = pjp.proceed(args);return ret;}//其他的略
}
上述代码中,ret
就是方法的返回值,我们是可以直接获取,不但可以获取,如果需要还可以进行修改。
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")private void pt(){}@AfterReturning(value = "pt()",returning = "ret")public void afterReturning(Object ret) {System.out.println("afterReturning advice ..."+ret);}//其他的略
}
注意:
(1)参数名的问题
(2)afterReturning方法参数类型的问题
参数类型可以写成String,但是为了能匹配更多的参数类型,建议写成Object类型
(3)afterReturning方法参数的顺序问题
运行App后查看运行结果,说明返回值已经被获取到
对于获取抛出的异常,只有抛出异常后AfterThrowing
和环绕Around
这两个通知类型可以获取,具体如何获取?
这块比较简单,以前我们是抛出异常,现在只需要将异常捕获,就可以获取到原始方法的异常信息了
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")private void pt(){}@Around("pt()")public Object around(ProceedingJoinPoint pjp){Object[] args = pjp.getArgs();System.out.println(Arrays.toString(args));args[0] = 666;Object ret = null;try{ret = pjp.proceed(args);}catch(Throwable throwable){t.printStackTrace();}return ret;}//其他的略
}
在catch方法中就可以获取到异常,至于获取到异常以后该如何处理,这个就和你的业务需求有关了。
@Component
@Aspect
public class MyAdvice {@Pointcut("execution(* com.itheima.dao.BookDao.findName(..))")private void pt(){}@AfterThrowing(value = "pt()",throwing = "t")public void afterThrowing(Throwable t) {System.out.println("afterThrowing advice ..."+t);}//其他的略
}
如何让原始方法抛出异常,方式有很多,
@Repository
public class BookDaoImpl implements BookDao {public String findName(int id,String password) {System.out.println("id:"+id);if(true){throw new NullPointerException();}return "itcast";}
}
注意:
运行App后,查看控制台,就能看的异常信息被打印到控制台
至此,AOP通知如何获取数据就已经讲解完了,数据中包含参数
、返回值
、异常(了解)
。
需求: 对百度网盘分享链接输入密码时尾部多输入的空格做兼容处理。
问题描述:
点击链接,会提示,请输入提取码,如下图所示
当我们从别人发给我们的内容中复制提取码的时候,有时候会多复制到一些空格,直接粘贴到百度的提取码输入框
但是百度那边记录的提取码是没有空格的
这个时候如果不做处理,直接对比的话,就会引发提取码不一致,导致无法访问百度盘上的内容
所以多输入一个空格可能会导致项目的功能无法正常使用。
此时我们就想能不能将输入的参数先帮用户去掉空格再操作呢?
答案是可以的,我们只需要在业务方法执行之前对所有的输入参数进行格式处理——trim()
也没有必要,一般只需要针对字符串处理即可。
可以考虑使用AOP来统一处理。
我们的需求是将原始方法的参数处理后在参与原始方法的调用,能做这件事的就只有环绕通知。
综上所述,我们需要考虑两件事:
①:在业务方法执行之前对所有的输入参数进行格式处理——trim()
②:使用处理后的参数调用原始方法——环绕通知中存在对原始方法的调用
创建一个Maven项目
pom.xml添加Spring依赖
org.springframework spring-context 5.2.10.RELEASE org.aspectj aspectjweaver 1.9.4
添加ResourcesService,ResourcesServiceImpl,ResourcesDao和ResourcesDaoImpl类
public interface ResourcesDao {boolean readResources(String url, String password);
}
@Repository
public class ResourcesDaoImpl implements ResourcesDao {public boolean readResources(String url, String password) {//模拟校验return password.equals("root");}
}
public interface ResourcesService {public boolean openURL(String url ,String password);
}
@Service
public class ResourcesServiceImpl implements ResourcesService {@Autowiredprivate ResourcesDao resourcesDao;public boolean openURL(String url, String password) {return resourcesDao.readResources(url,password);}
}
创建Spring的配置类
@Configuration
@ComponentScan("com.itheima")
public class SpringConfig {
}
编写App运行类
public class App {public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);ResourcesService resourcesService = ctx.getBean(ResourcesService.class);boolean flag = resourcesService.openURL("http://pan.baidu.com/haha", "root");System.out.println(flag);}
}
最终创建好的项目结构如下:
现在项目的效果是,当输入密码为"root"控制台打印为true,如果密码改为"root "控制台打印的是false
需求是使用AOP将参数进行统一处理,不管输入的密码root
前后包含多少个空格,最终控制台打印的都是true。
@Configuration
@ComponentScan("com.itheima")
@EnableAspectJAutoProxy
public class SpringConfig {
}
@Component
@Aspect
public class DataAdvice {@Pointcut("execution(boolean com.itheima.service.*Service.*(*,*))")private void servicePt(){}}
@Component
@Aspect
public class DataAdvice {@Pointcut("execution(boolean com.itheima.service.*Service.*(*,*))")private void servicePt(){}@Around("DataAdvice.servicePt()")// @Around("servicePt()")这两种写法都对public Object trimStr(ProceedingJoinPoint pjp) throws Throwable {Object ret = pjp.proceed();return ret;}}
@Component
@Aspect
public class DataAdvice {@Pointcut("execution(boolean com.itheima.service.*Service.*(*,*))")private void servicePt(){}@Around("DataAdvice.servicePt()")// @Around("servicePt()")这两种写法都对public Object trimStr(ProceedingJoinPoint pjp) throws Throwable {//获取原始方法的参数Object[] args = pjp.getArgs();for (int i = 0; i < args.length; i++) {//判断参数是不是字符串if(args[i].getClass().equals(String.class)){args[i] = args[i].toString().trim();}}//将修改后的参数传入到原始方法的执行中Object ret = pjp.proceed(args);return ret;}}
不管密码root
前后是否加空格,最终控制台打印的都是true
为了能更好的看出AOP已经生效,我们可以修改ResourcesImpl类,在方法中将密码的长度进行打印
@Repository
public class ResourcesDaoImpl implements ResourcesDao {public boolean readResources(String url, String password) {System.out.println(password.length());//模拟校验return password.equals("root");}
}
再次运行成功,就可以根据最终打印的长度来看看,字符串的空格有没有被去除掉。
注意:
接下来对于AOP的知识进行一个总结:
切入点表达式标准格式:动作关键字(访问修饰符 返回值 包名.类/接口名.方法名(参数)异常名)
execution(* com.itheima.service.*Service.*(..))
切入点表达式描述通配符:
*
:匹配任意符号(常用)..
:匹配多个连续的任意符号(常用)+
:匹配子类类型切入点表达式书写技巧
1.按标准规范开发
2.查询操作的返回值建议使用*匹配
3.减少使用…的形式描述包
4.对接口进行描述,使用*表示模块名,例如UserService的匹配描述为*Service
5.方法名书写保留动词,例如get,使用*表示名词,例如getById匹配描述为getBy*
6.参数根据实际情况灵活调整