间歇性NoSuchBeanDefinitionException

时间:2019-07-16 17:12:50

标签: java spring

我有一个小的Java库,该库带有少量@Components注释,并具有两个@configuration

,当我们从groovy脚本 SOMETIMES 中调用它时,出现以下错误。我不明白为什么这是一个间歇性问题

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'oms': Unsatisfied dependency expressed through constructor argument with index 0 of type [ component.oms.moegw.MqoegwMock]: : No qualifying bean of type [ component.oms.moegw.MqoegwMock] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ component.oms.moegw.MqoegwMock] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at  component.oms.OmsServices.init(OmsServices.java:51)
at  component.oms.OmsServices.<clinit>(OmsServices.java:14)
... 24 more

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ component.oms.moegw.MqoegwMock] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)

通过mvn执行的常规是:

import component.oms;
OmsService.acknoledgeOrder("123");

这是我们从groovy脚本中调用的Java类

package component.oms;

import component.oms.testdata.TestData;
import component.oms.config.MqoegwConfig;
import component.oms.config.OegwConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public final class OmsServices
{
    private static AnnotationConfigApplicationContext ctx;

    static
    {
        init();
    }

    private OmsServices()
    {
        // static only
    }   

    public static void acknowledgeOrder(String orderId)
    {
        //load OMSMock context to be injected
        Oms oms = ctx.getBean(Oms.class);
        oms.acknowledgeOrder(orderId);
    }

    public static AnnotationConfigApplicationContext init()
    {
        ctx = new AnnotationConfigApplicationContext();

        ctx.register(component.oms.config.OegwConfig.class);
        ctx.register(component.oms.config.MqoegwConfig.class);
        ctx.register(component.oms.oss.OssMock.class);
        ctx.register(component.oms.Oms.class);
        ctx.refresh();
        return ctx;
    }
}

component.oms.moegw.MqoegwMock是一个注释为@Component的类(不要与component.oms.config.MqoegwConfig(@配置类)混淆

init()始终仅被调用一次,因为它是静态块。在我看来,这可能是某个类路径问题,或者我的静态类不是初始化该Java库的好方法?我不知道如何调试它。

提前感谢所有帮助

这里是用于说明的Oms类

@Component
public class Oms
{
    private MqoegwMock mqoegwMock;
    private OegwMock oegwMock;
    private OssMock ossMock;

    @Autowired
    public Oms(MqoegwMock mqoegwMock, OegwMock oegwMock, OssMock ossMock)
    {
        this.mqoegwMock = mqoegwMock;
        this.oegwMock = oegwMock;
        this.ossMock = ossMock;
    }

这里是MqoegwMock类,以供澄清

    @Component
    public class MqoegwMock
    {   

        private MessageProcessor messageProcessor;
        private OrderDao orderDao;

        @Autowired
        public MqoegwMock(MessageProcessor messageProcessor, OrderDao orderDao)
        {
            this.messageProcessor = messageProcessor;
            this.orderDao = orderDao;
        }
      ....
}

编辑

可能是由于不同的类加载器...正在调查

0 个答案:

没有答案