我是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");
}
如果您有更好的解决方案,请告诉我。
答案 0 :(得分:0)
正如我在评论中所说,[Xlint:invalidAbsoluteTypeName]
告诉您,您真实的,完全合格的班级名称与您在切入点中使用的名称不同。比较两者,你会发现一个容易修复的不匹配。也许这只是一个错字。
如果你想要更加防御性地编码,可以使用这样的通配符,这样你就可以在你的班级中移动到子包中,如果你愿意的话:
在StatusCleanScheduler
的任何子包中查找类com.project.scheduler
(这可能是您想要的):
execution(* com.project.scheduler..StatusCleanScheduler.schedule(..))
仅供参考,您也可以这样做:
在StatusCleanScheduler
的任何子包中找到课程com.project
:
execution(* com.project..StatusCleanScheduler.schedule(..))
在Scheduler
的任何子包中找到以com.project
结尾的类:
execution(* com.project..*Scheduler.schedule(..))
在项目中查找任何schedule
方法:
execution(* com.project..schedule(..))