简体   繁体   中英

Exception when using @Scheduled spring annotation (Cannot find class [org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor])

I have created a scheduled job using a @Scheduled annotation. It looks like this:

package example.tools.rr; 

public class RequestProcessorJob {
    private RequestService requestService;
    ...
    @Scheduled(fixedRate = 10000)
    public void process() {
         ...
    }

    public RequestService getRequestService() { return requestService; }

    public void setRequestService(RequestService requestService) { this.requestService = requestService; }

}

I've put it inside a bundle A, so I can use this class in other bundles. I imported it inside bundle B. B's blueprint.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:cm="http://www.eclipse.org/gemini/blueprint/schema/blueprint-compendium"
  xmlns:ctx="http://www.springframework.org/schema/context" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:task="http://www.springframework.org/schema/task"
  xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
              http://www.eclipse.org/gemini/blueprint/schema/blueprint-compendium http://www.eclipse.org/gemini/blueprint/schema/blueprint-compendium/gemini-blueprint-compendium.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/task
              http://www.springframework.org/schema/task/spring-task-3.1.xsd
              ">
  ... 

  <bean id="handsetService" class="example.handset.impl.HandsetServiceImpl">
    ...
  </bean>

  <task:scheduler id="taskScheduler" />
  <task:executor id="taskExecutor" pool-size="1" />
  <task:annotation-driven executor="taskExecutor"
                          scheduler="taskScheduler" />

  <bean class="example.tools.rr.RequestProcessorJob">
    <cm:managed-properties persistent-id="example.handset.processing"
                           autowire-on-update="true" />
    <property name="requestService" ref="handsetService" />
  </bean>

</blueprint>

When I install the bundle (inside Apache Felix) a following exception is thrown:

Unable to create application context for [example.handset], unsatisfied dependencies:
none
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor] for bean with name 'org.springframework.context.annotation.internalAsyncAnnotationProcessor' defined in null; nested
exception is java.lang.ClassNotFoundException: org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor not found from bundle [example.handset]
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1262) ~[na:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:576) ~[na:na]
        at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1331) ~[na:na]
        at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:897) ~[na:na]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:566) ~[na:na]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) ~[na:na]
        at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:60) ~[na:na]
        at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:325) ~[na:na]
        at org.eclipse.gemini.blueprint.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85) ~[na:na]
        at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:290) ~[na:na]
        at org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:137) ~[na:na]
        at java.lang.Thread.run(Unknown Source) [na:1.7.0_67]
Caused by: java.lang.ClassNotFoundException: org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor not found from bundle [example.handset]
        at org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader.findClass(BundleDelegatingClassLoader.java:101) ~[na:na]
        at org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader.loadClass(BundleDelegatingClassLoader.java:155) ~[na:na]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.7.0_67]
        at org.springframework.util.ClassUtils.forName(ClassUtils.java:257) ~[na:na]
        at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:417) ~[na:na]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1283) ~[na:na]
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1254) ~[na:na]
        ... 11 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor not found by example.handset [443]
        at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1499) ~[felix.jar:na]
        at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
~[felix.jar:na]
        at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1882) ~[felix.jar:na]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.7.0_67]
        at org.apache.felix.framework.Felix.loadBundleClass(Felix.java:1793) ~[felix.jar:na]
        at org.apache.felix.framework.BundleImpl.loadClass(BundleImpl.java:929) ~[felix.jar:na]
        at org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader.findClass(BundleDelegatingClassLoader.java:97) ~[na:na]
        ... 17 common frames omitted

if I add an empty class which just has a @Scheduled annotation inside the bundle B, everything works fine and the RequestProcessorJob is scheduled properly (the empty class is not used, but it makes the error not occur).

public class MockJob {
    @Scheduled(fixedRate = 10000)
    public void process() { }
}

I've tried to include the module A inside of B in pom.xml as <scope>provided</scope> as well as <scope>compile</scope> , the result is the same.

Having a reduntant class to make it work doesn't really sounds like a solution. Do You have any ideas about this? Is this a bug, or maybe somehow it was supposed to work so strangely?

Ok, I was just missing org.springframework.scheduling.annotation in <Import-Package> section in pom.xml. Quite obvious after all, but the behaviour of spring is still weird here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM