Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Lazy init无法使用@Resource注入_Spring_Spring Mvc_Classnotfoundexception - Fatal编程技术网

Spring Lazy init无法使用@Resource注入

Spring Lazy init无法使用@Resource注入,spring,spring-mvc,classnotfoundexception,Spring,Spring Mvc,Classnotfoundexception,我有一个与真实和模拟实现的接口。由于明显的原因,模拟实现不在生产类路径中。 我使用以下方法注入bean: @Resource (name="${myClient}") 我正在使用SpringMVC,并将其注入到@控制器中 在外部配置中,我设置要使用的实际bean名称,并将其绑定到“myClient”参数。绑定可以工作,它尝试加载真正的实现,但在模拟上的ClassNotFound上也失败,尽管标记为lazy init=true 我正在使用Spring4.0.0 我知道在起诉@Autowire时,

我有一个与真实和模拟实现的接口。由于明显的原因,模拟实现不在生产类路径中。 我使用以下方法注入bean:

@Resource (name="${myClient}")
我正在使用SpringMVC,并将其注入到
@控制器中

在外部配置中,我设置要使用的实际bean名称,并将其绑定到“myClient”参数。绑定可以工作,它尝试加载真正的实现,但在模拟上的ClassNotFound上也失败,尽管标记为lazy init=true

我正在使用Spring4.0.0

我知道在起诉@Autowire时,这是意料之中的,但对于@Resource,我不希望它尝试在SpringXML中实例化所有bean

你知道怎么回事吗

以下是堆栈跟踪:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [MyMock] for bean with name 'myClientMock' defined in URL [file:/C:/myProject/target/classes/META-INF/springContext.xml]; nested exception is java.lang.ClassNotFoundException: MyMock1
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1327)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1396)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:382)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:361)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:347)
at org.springframework.context.support.AbstractApplicationContext.getBeanNamesForType(AbstractApplicationContext.java:1051)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:105)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:89)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:163)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)

我不知道您是否在使用Spring概要文件,但我们在这里使用它来初始化每个环境的不同bean。 从Tomcat中,我们发现了使用哪种配置文件,在Spring中,我们配置了类似这样的东西

<beans profile="deployed-local">
    <util:properties id="propertyConfigurer" location="classpath:app.deployed-performance.properties"/>
    <context:property-placeholder location="classpath:app.deployed-performance.properties,classpath:app.constants.properties"/>
    <import resource="spring/jdbc-config-test.xml"/>
    <import resource="spring/contacts-config-deployed.xml"/>
    <import resource="spring/security-config-local.xml"/>
    <import resource="spring/clamAV-service.xml"/>
    <import resource="spring/document-service.xml"/>
</beans>

<beans profile="deployed-prod">
    <import resource="spring/jdbc-config.xml"/>
    <import resource="spring/contacts-config-deployed.xml"/>
    <import resource="spring/security-config.xml"/>
    <import resource="spring/clamAV-service.xml"/>
    <import resource="spring/document-service.xml"/>
</beans>


附加springContext.xml可能有助于理解这个bean创建错误的原因。知道它来自哪里吗?“MyMock1”MyMock1是我的类名虽然这可能会起作用,但Spring初始化我的bean(尽管它不应该)不是一个bug吗?问题的根源似乎来自于此:我认为这不是您的实现问题。我提供给你这个选择,因为我认为它更清晰和优雅。