使用springMVC AOP实现日记记录

1 . 配置springmvc驱动,以及包扫描,还有AOP配置,如下:

<context:component-scan base-package="org.lee" /> 
	<mvc:annotation-driven />

	<!-- 注解实现日记记录 -->
    <aop:aspectj-autoproxy />  


2 . 引入相关jar包


3 . 目录树


4 . 编写日记记录类 LogInterceptor.java
package org.lee.Interceptor;
import java.lang.reflect.Method;
import java.util.Arrays;
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;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogInterceptor {
	
   /**
    * 这里针对org.lee.service.Imp目录下的新增,修改方法进行记录
    * */
	 @Pointcut("execution(* org.lee.service.Imp.*.*create*(..)) || execution(* org.lee.service.Imp.*.*edit*(..))")
	public void allMethod(){
		
	}
	 @Before("allMethod()") 
	 public void before(){  
	    }
	@After("allMethod()")
	public void afterLog(JoinPoint point){
		System.out.println("--------------afterLog:--------------最终通知");
		System.out.println("--------------afterLog:=========目标方法为:"+point.getSignature().getDeclaringTypeName()+"."+point.getSignature().getName());
		System.out.println("--------------afterLog:--------------参数为:"+Arrays.toString(point.getArgs()));
		System.out.println("--------------afterLog:--------------被织入的对象为:"+point.getTarget());
		

	}
	
	@AfterReturning(value="allMethod()",returning="returnValue")
	public void afterRunningLog(JoinPoint point,Object returnValue ){
		System.out.println("--------------afterRunningLog--------------返回值后通知");
		System.out.println("--------------afterRunningLog----------------目标方法:"+point.getSignature().getDeclaringTypeName()+"."+point.getSignature().getName());
		System.out.println("--------------afterRunningLog----------------参数为:"+Arrays.toString(point.getArgs()));
		System.out.println("--------------afterRunningLog----------------返回值:"+returnValue);	
		
		 
		
	}
	
	@AfterThrowing(value="allMethod()",throwing="ex")
	public void AfterThrowingLog(Throwable ex){
		System.out.println("--------------AfterThrowingLog--------------进入异常通知");
		System.out.println("--------------AfterThrowingLog--------------异常信息:"+ex.getMessage());
	}
	
	
	@Around("allMethod()")
	public Object doAround(ProceedingJoinPoint p) throws Throwable{
		System.out.println("--------------Around--------------进入环绕通知");
		  long startTime = System.currentTimeMillis();  
		Object obj = p.proceed();
		  long endTime = System.currentTimeMillis();  
		  MethodSignature signature = (MethodSignature) p.getSignature();  
	        String methodName = signature.getDeclaringTypeName() + "." + signature.getName(); 
	        
		  System.out.println("目标方法:"+methodName);
		  System.out.println("运行耗时:"+(endTime-startTime)+"ms");
		System.out.println("--------------Around--------------结束方法调用");
		return obj;
		
	}
	

}


测试:


Spring提供了一种开发模式,称为面向切面编程(AOP),可以通过它来实现数据库的切换。在Spring MVC中,我们可以使用AOP来拦截和修改方法的执行,以便动态切换数据库。 首先,我们需要配置SpringAOP,以便在运行时拦截方法。这可以通过在Spring配置文件中添加以下代码来实现: ``` <aop:aspectj-autoproxy/> ``` 接下来,我们需要创建一个切面类,该类将拦截和处理我们选择的目标方法。在这个切面类中,我们可以编写切面逻辑来判断需要使用哪个数据库。下面是一个示例切面类的代码: ```java @Aspect @Component public class DatabaseSwitchAspect { @Before("execution(* com.example.controller.*.*(..))") public void switchDatabase(JoinPoint joinPoint) { // 判断需要使用哪个数据库 if (需要使用第一个数据库的条件) { // 设置第一个数据库的相关配置 DataSourceContextHolder.setDataSourceType(第一个数据库); } else { // 设置第二个数据库的相关配置 DataSourceContextHolder.setDataSourceType(第二个数据库); } } } ``` 在以上代码中,我们使用@Before注解来指定拦截哪些方法。在拦截到方法之后,我们可以在切面逻辑中根据判断条件来选择需要使用的数据库,并使用自定义的DataSourceContextHolder来动态设置数据源。 最后,我们需要在Spring配置文件中配置数据源和事务管理器。这里我们需要配置两个数据源和对应的事务管理器,以便与切面类中设置的数据源保持一致。 通过以上步骤,我们就能够实现Spring MVCAOP的方式来动态切换数据库。当我们拦截到目标方法时,根据条件切换数据库,从而实现数据库的动态切换。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值