我是Spring AOP的新手,我很不安......
我运行 JBoss 7.2、Mojara 2.2.2 和 Spring 3.1.2.RELEASE。
我有以下 dispatcher-servlet.xml
<mvc:annotation-driven />
<context:annotation-config />
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:component-scan base-package="com.project.scheduler" />
<task:annotation-driven scheduler="taskScheduler" />
<task:scheduler id="taskScheduler" pool-size="2" />
这是我的观点:
@Aspect
@Component
public class BatchHistorizerAspect implements Serializable {
private static final long serialVersionUID = 2620746940572873202L;
public static final Logger LOGGER = LogManager.getLogger(BatchHistorizerAspect.class);
@Around("execution(* com.project.scheduler.StatusCleanScheduler.schedule(..))")
public void aroundSchedule(ProceedingJoinPoint jp) {
LOGGER.debug("aroundSchedule start");
try {
jp.proceed();
} catch (Throwable e) {
LOGGER.debug("aroundSchedule exception:", e);
}
LOGGER.debug("aroundSchedule end");
}
}
最后是我想建议的类(class):
@Component
public class StatusCleanScheduler implements Serializable {
private static final long serialVersionUID = -8061115218184844863L;
private final static Logger LOGGER = LogManager.getLogger(StatusCleanScheduler.class);
@Autowired
private StatusService statusService;
@Override
@Scheduled(cron = "0 * * * * *")
public void schedule() {
LOGGER.info("Start Status orphans cleaning");
statusService.cleanOrphans();
LOGGER.info("End Status orphans cleaning");
}
}
当我启动我的服务器时,它会抛出以下内容:
ERROR [ServerService Thread Pool -- 54] org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/agathe]: JBWEB000289: Servlet dispatcher threw load() exception: java.lang.IllegalArgumentException: warning no match for this type name: com.project.scheduler.StatusCleanScheduler [Xlint:invalidAbsoluteTypeName]
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301) [aspectjweaver-1.6.12.jar:1.6.12]
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207) [spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getFallbackPointcutExpression(AspectJExpressionPointcut.java:358) [spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getShadowMatch(AspectJExpressionPointcut.java:409) [spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
...
但是当我将 @Around
更改为 @Around("execution(* com.project.scheduler.*.schedule(..))")
时,一切都是好的,我的方面被正确调用。
我做错了什么?
更新:
我做了一些解决方法,但我发现它很脏......
@Around("execution(* com.project.scheduler.*.schedule(..))")
public void aroundSchedule(ProceedingJoinPoint jp) {
Object obj = jp.getThis();
if (!(obj instanceof StatusCleanScheduler)) {
jp.proceed();
return;
}
StatusCleanScheduler batch = (StatusCleanScheduler) obj;
LOGGER.debug("aroundSchedule start");
try {
jp.proceed();
} catch (Throwable e) {
LOGGER.debug("aroundSchedule exception:", e);
}
LOGGER.debug("aroundSchedule end");
}
如果你有更好的解决方案,请告诉我。
最佳答案
正如我在评论中所说,[Xlint:invalidAbsoluteTypeName]
告诉您您真正的、完全限定的类名与您在切入点中使用的类名不同。比较两者,您会发现不匹配很容易解决。也许这只是一个错字。
如果您想编写更具防御性的代码,请使用这样的通配符,这样您就可以根据需要将类移动到子包中:
在 com.project.scheduler
的任何子包中找到类 StatusCleanScheduler
(这可能是你想要的):
execution(* com.project.scheduler..StatusCleanScheduler.schedule(..))
仅供引用,您也可以这样做:
在 com.project
的任何子包中找到类 StatusCleanScheduler
:
execution(* com.project..StatusCleanScheduler.schedule(..))
在com.project
的任意子包中查找以Scheduler
结尾的类:
execution(* com.project..*Scheduler.schedule(..))
在您的项目中找到任何 schedule
方法:
execution(* com.project..schedule(..))
关于java - Spring AOP 表达式抛出 java.lang.IllegalArgumentException : warning no match for this type name [. ..],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33759074/