【问题标题】:Spring AOP expression throw java.lang.IllegalArgumentException: warning no match for this type name [...]Spring AOP 表达式 throw java.lang.IllegalArgumentException: 警告 no match for this type name [...]
【发布时间】:2015-11-21 19:44:17
【问题描述】:

我是 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");
    }
}

最后是我想建议的课程:

@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");
}

如果你有更好的解决方案,请告诉我。

【问题讨论】:

  • 显然你的原始切入点中的包名与StatusCleanScheduler的包名不完全匹配。在您的代码中,sn-p 缺少这条关键信息。请比较两者,你会发现不匹配,我保证。

标签: java spring aop spring-aop


【解决方案1】:

正如我在评论中所说,[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(..))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多