开发者学堂课程【Java Web 开发系列课程:Spring 框架入门:使用注解实现 aop】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/538/detail/7347
使用注解实现 aop
内容简介:
一、自定义类来实现
二、通过注解来实现
一、自定义类来实现
第二种方式实现 aop:自定义类来实现
代码如下:
Log java
public class Log{
public void before(){
System.out.println("-----
方法执行前---");
public void after(){
System.out.println("----
方法执行后---“);
业务类不改变
配置文件
class="cn.sxt.service.impl.UserServiceImpl"/>
cn.sxt.service.impl.*.*(..))”id="pointcut"/>
pointcut-ref="pointcut"/>
二、通过注解来实现
第三种实现方法:通过注解来实现
Logjava
public class Log {
@Before("execution(* cn.sxt.service,impl.."(..))")
public void before
(){
System.out.println("---
方法执行前---");
}
@After(*execution(*cn.sxt.service.impl...(..))")
public void after()
System.out.println("-
方法执行后");
@Around("execution(* cn.sxt.service,impl.**(..))")
public obiect aroud(ProceedingjoinPoint ip) throws Throwablef
System.out.println("
环绕前");
System.out.println(*
签名:"+jp.getSignature());
//
执行目标方法
Object result = jp.proceed();
System.out.println("
环绕后");
return result;
}
}
配置文件
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.xs
d
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"
class="cn.sxt.service.impl.UserServiceImpl"/>