我试图在一个类中注入一个utils类,这个类将截获数据库查询,但是,很显然,spring找不到构造函数。两个类都用 @Component
并对 Package 中的其他Spring部件进行了扫描。
utils类如下所示:
@Component
public class Utils {
private static final String LOCAL_PROFILE = "local";
private final Environment environment;
private final String dummy = "dummy";
@Autowired
public Utils(Environment environment) {
this.environment = environment;
}
public String getInfo() {
//to avoid ST Visa issues in Local environment
if (environment != null && Arrays.asList(environment.getActiveProfiles()).contains(LOCAL_PROFILE)) {
return dummy;
}
return SecurityContextHolder.getContext().getAuthentication().getName();
}
}
我要在其中注入utils的拦截器如下所示:
@Component
public class ContractInterceptor implements StatementInspector {
private final Utils utils;
@Autowired
public ContractInterceptor(Utils utils) {
this.utils = utils;
}
@Override
public String inspect(String sql) {
//String info = utils.getInfo();
log.info("Enter Interceptor" + sql);
return sql;
}
}
Spring失效
Caused by: java.lang.NoSuchMethodException: com.package.interceptor.<init>() at
java.base/java.lang.Class.getConstructor0(Class.java:3349) at
java.base/java.lang.Class.newInstance(Class.java:556)
谢谢。
暂无答案!
目前还没有任何答案,快来回答吧!