本文介紹了Spring-AOP:@在異常:java.lang.IlLegalArgumentException:引用的錯誤類型不是批注類型的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我是AOP和AspectJ的新手。我正嘗試圍繞元素類型為TYPE
的自定義注釋編寫一個自定義邏輯。我還想在我的注釋下使用這些參數來在環繞方法中編寫一個定制邏輯。我收到了下面提到的錯誤。我試著閱讀了多個來源,大多數來源都與使用@annotation
有關。我發現很難理解這一點。誰能解釋一下,為什么以下@within
的方法不適用于一些明顯的例子?
Annotation(Slf4jTrace.java):
package io.ud.project.falcon.logging;
import io.ud.project.falcon.logging.generator.AbstractLogTraceIdGenerator;
import io.ud.project.falcon.logging.generator.DefaultLogTraceIdGenerator;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Slf4jTrace {
String traceKey() default "traceId";
Class<? extends AbstractLogTraceIdGenerator> traceValueGenerator() default DefaultLogTraceIdGenerator.class;
}
方面Component(Slf4jTraceAspect.java):
package io.ud.project.falcon.logging;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
@SuppressWarnings("unused")
@Slf4j
public class Slf4jTraceAspect {
@Pointcut("execution(public * *(..))")
public void publicOperation() {
//Using this pointcut annotation for Around method.
}
@Around(value = "@within(slf4jTrace) && publicOperation()")
public void logTrace(ProceedingJoinPoint joinPoint, Slf4jTrace slf4jTrace) throws Throwable {
//Custom logic that uses slf4jTrace's parameters
joinPoint.proceed();
//Custom logic that uses slf4jTrace's parameters
}
}
帶注釋的類(Foo.java):
package io.ud.project.falcon.logging;
import io.ud.project.falcon.logging.Slf4jTrace;
@Slf4jTrace(traceKey = "auditID")
public class Foo {
public void doSomething() {
//Something happens here.
}
}
應用程序啟動時出錯:
, 2021-01-02 22:16:41,340, ERROR [main] o.s.b.SpringApplication.reportFailure(833) | Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectMapperConfigurer' defined in springfox.documentation.spring.web.SpringfoxWebMvcConfiguration
: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error Type referred to is not an annotation type: slf4jTrace
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:497)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205)
at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:238)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:709)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:534)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)
at io.ud.project.falcon.FalconApplication.main(FalconServiceApplication.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
推薦答案
我沒有嘗試運行您的代碼,但當我瀏覽它時,立即注意到了兩件事:
BeanCreationException
如果方面切入點太過寬泛,就經常會出現內部Spring類型的問題,也就是說,方面也被編織到了Spring類中(即使沒有在其中執行)。在這種情況下,您希望通過向切入點添加類似... && within(my.package..*)
的內容來限制方面范圍。或者,如果您的方面位于可能針對未知包名運行的庫中,您可以通過!within(org.springframework..*)
或任何適用于您的包名來排除引起問題的Spring包或類。另請參閱my answer here。
您的@Around
建議返回void
,即它還將只匹配void
方法,這可能不是您想要的。為了匹配非空方法,建議必須返回void
以外的內容,如果要匹配任何返回類型,通常只返回Object
。還要確保返回proceed()
的結果或您希望返回的任何其他結果。
這篇關于Spring-AOP:@在異常:java.lang.IlLegalArgumentException:引用的錯誤類型不是批注類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,