在自定义mybatis拦截器中,如果通过@Autowired注入对象会报错
Requested bean is currently in creation: Is there an unresolvable circular reference
解决方案
MyUpdateLogInterceptor
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Properties;
/**
* @author xrj
* @date 2020/5/28
*/
@Slf4j
@Component
@Intercepts({@Signature(type = Executor.class,method = "update",args = {MappedStatement.class,
Object.class})})
public class MyUpdateLogInterceptor implements Interceptor {
private String UPDATE="UPDATE";
private String INSERT="INSERT";
private String CARP="com.cdls.carp.";
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
// 获取 SQL 命令
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
// 获取参数
Object object = invocation.getArgs()[1];
if(object!=null){
Class<?> classObject = object.getClass();
if ( classObject.getName().startsWith(CARP)) {
log.info("class:"+classObject+"获取到的sql命令为:{}",sqlCommandType);
try {
if(INSERT.equals(SqlCommandType.INSERT.toString())){
//创建人
//创建时间
Field createDate = null;
try {
createDate = classObject.getDeclaredField("createDate");
} catch (NoSuchFieldException e) {
//单独捕获,不能影响逻辑
log.error("MyUpdateLogInterceptor createDate获取异常NoSuchFieldException:"+e.getMessage());
}
if(createDate!=null){
createDate.setAccessible(true);
createDate.set(object,new Date());
}
Field visible = null;
try {
visible = classObject.getDeclaredField("visible");
} catch (NoSuchFieldException e) {
log.error("MyUpdateLogInterceptor visible获取异常NoSuchFieldException:"+e.getMessage());
}
if(visible!=null){
visible.setAccessible(true);
visible.set(object,true);
}
}else if(UPDATE.equals(SqlCommandType.UPDATE.toString())){
Field modifyDate = null;
try {
modifyDate = classObject.getDeclaredField("modifyDate");
} catch (NoSuchFieldException e) {
log.error("MyUpdateLogInterceptor modifyDate获取异常NoSuchFieldException:"+e.getMessage());
}
if(modifyDate!=null){
modifyDate.setAccessible(true);
modifyDate.set(object,new Date());
}
}
}catch (IllegalAccessException e) {
//id获取异常
log.error("id获取异常:",e);
}catch (Exception e) {
//获取异常
log.error("Exception getDeclaredField 获取异常:",e);
}
}
}
return invocation.proceed();
}
@Override
public Object plugin(Object o) {
return Plugin.wrap(o,this);
}
@Override
public void setProperties(Properties properties) {
}
ServiceHelper
import com.cdls.carp.business.service.AllDataHistoryService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
*
* 對象輔助類
* @author xrj
* @date 2020/5/29
*/
@Component
public class ServiceHelper implements InitializingBean {
private static ServiceHelper instance=null;
/**
* 自己的對象
*/
@Autowired
private AllDataHistoryService allDataHistoryService;
@Override
public void afterPropertiesSet() throws Exception {
ServiceHelper.instance=this;
}
public static AllDataHistoryService getAllDataHistoryService(){
return instance.allDataHistoryService;
}
}
SpringContextUtil
也可以这样
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author ckinghan
* @title: SpringContextUtil
* @projectName platform
* @description: 获取springcontext中的bean
* @date 2019/10/1811:23
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
public static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringContextUtil.context = context;
}
/**
* 获取容器中的实例
* @param clazz 根据class获取Spring容器中对应的Bean类
*/
public static <T> T getBean( Class<T> clazz){
return context.getBean(clazz);
}
public static ApplicationContext getContext(){
return context;
}
}
博主强烈推荐:https://blog.csdn.net/persistencegoing/article/details/84376427
希望大家关注我一波,防止以后迷路,有需要的可以加群讨论互相学习java ,学习路线探讨,经验分享与java求职
群号:721 515 304