AOP实现
AOP方式一(使用原生的Spring的API实现)
Maven需要导入的依赖
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>
接口
public interface userService { void add(); void delete(); void update(); void select(); }
实现类
public class userServiceimpl implements userService{ @Override public void add() { System.out.println("增加了一个用户"); } @Override public void delete() { System.out.println("删除了一个用户"); } @Override public void update() { System.out.println("更新了一个用户"); } @Override public void select() { System.out.println("查询了一个用户"); } }
前置日志
public class beforeLog implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了"); } }
后置日志
public class afterLog implements AfterReturningAdvice { //returnValue:返回值 @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("执行了"+method.getName()+"方法,返回的结果为:"+returnValue); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.Google.service.userServiceimpl"/> <bean id="beforeLog" class="com.Google.LOG.beforeLog"/> <bean id="afterLog" class="com.Google.LOG.afterLog"/> <!--方法一:使用原生的Spring的API接口--> <!--配置AOP 需要导入AOP的约束--> <aop:config> <!--切入点· expression:表达式 execution(要具体执行的位置 修饰词 返回值 类名 方法名 参数)--> <aop:pointcut id="pointcut" expression="execution(* com.Google.service.userServiceimpl.* (..))"/> <!--执行环绕--> <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config> </beans>
AOP方式二、自定义
自定义类
public class diy { public void before(){ System.out.println("====方法执行前===="); } public void after(){ System.out.println("====方法执行后==="); } }
文件配置
<!--方法二:自定义AOP--> <bean id="diy" class="com.Google.DIY.diy"/> <aop:config> <!--自定义切面:ref 要引用的类--> <aop:aspect ref="diy"> <!--切入点 :com.Google.service.userServiceimpl该类下的所有方法--> <aop:pointcut id="pointcut" expression="execution(* com.Google.service.userServiceimpl.* (..))"/> <aop:before method="before" pointcut-ref="pointcut"/> <aop:after method="after" pointcut-ref="pointcut"/> </aop:aspect> </aop:config>
AOP方式三、注解
注解类
@Aspect//标注这个类是一个切面 public class annotation { @Before("execution(* com.Google.service.userServiceimpl.*(..))") public void before (){ System.out.println("执行方法前"); } @After("execution(* com.Google.service.userServiceimpl.*(..))") public void after(){ System.out.println("执行方法后"); } }
配置文件
<!--方式三:注解--> <bean id="annotation" class="com.Google.DIY.annotation"/> <!--开启注解--> <aop:aspectj-autoproxy/>
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术