Aop:面向切面,在不修改代码的前提下对方法进行增强
pom.xml
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.20</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> </dependencies>
切面类
package com.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component //将当前类交给Spring进行管理,作为普通的bean进行管理 @Aspect //将当前类告诉Spring作为切面类 public class UserAdvice {//切面类 //@Pointcut("execution(public void com.dao.UserDao.*User(*))") //定义切入点 @Pointcut("execution(* com.dao.*Dao.*User(..))") //定义切入点 返回值用*代替 类名方法名用通配符 多个参数用..代替 public void pt(){} @Before("pt()") //前置通知 public void addLog(){ //通知/增强 与切入点结合起来 System.out.println("前置通知 添加日志功能"); } @AfterReturning("pt()") public void afterReturning(){ System.out.println("后置通知"); } @After("pt()") public void after(){ System.out.println("最终通知"); } @AfterThrowing("pt()") public void afterThrowing(){ System.out.println("异常抛出通知"); } @Around("pt()") public void around(ProceedingJoinPoint point) throws Throwable{ System.out.println("方法执行之前"); point.proceed();//执行目标方法 System.out.println("方法执行之后"); } } /* * // @Pointcut("execution(public void com.dao.UserDao.insertUser(com.entity.User))") //定义切入点 // @Pointcut("execution(void com.dao.UserDao.insertUser(com.entity.User))") //省略权限修饰符 // @Pointcut("execution(* com.dao.UserDao.insertUser(com.entity.User))") //返回值使用*代替 // @Pointcut("execution(* com..UserDao.insertUser(com.entity.User))") //多层包名使用 .. 代替,但是不推荐这么使用 // @Pointcut("execution(* com.dao.*Dao.*User(com.entity.User))") //报包名写完整,类名和方法名使用通配符 // @Pointcut("execution(* com.dao.*Dao.*User(*))") //参数列表使用*代替 // @Pointcut("execution(* com.dao.*Service.*User(..))") //多个参数使用 .. 代替 @Pointcut("execution(* com.dao.*Dao.*User(..))") //多个参数使用 .. 代替 */ /* * 通知(增强)类型: 前置通知,后置通知,异常抛出通知,环绕通知, 最终通知 try{ 前置通知 环绕通知 切入点 环绕通知 后置通知 }catch(){ 异常抛出通知 }finally{ 最终通知 } * */
SpringConfig
package com.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration//表示该类是Spring配置类,用于代替ApplicationContext.xml @ComponentScan(value = {"com.aop","com.dao"})//包扫描 @EnableAspectJAutoProxy //开启注解形式的aop public class SpringConfig { }
dao层
import com.entity.User; import java.util.List; public interface UserDao { void insertUser(User user); } 实现类 @Repository("userDao") public class UserDaoImpl implements UserDao { @Override public void insertUser(User user) { System.out.println("添加用户"); } }
test
@Test public void test(){ ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); UserDao userDao = context.getBean(UserDao.class); userDao.insertUser(null); }
标签:04,Spring,void,dao,User,SpringAOP,org,com,public From: https://www.cnblogs.com/oyww-2027/p/17727440.html