Spring Lazy-init不使用@Resource注入

时间:2014-09-30 07:21:42

标签: spring spring-mvc classnotfoundexception

我有一个真实和模拟实现的接口。由于显而易见的原因,模拟实现不在生产类路径中。 我使用:

注入bean
@Resource (name="${myClient}")

我正在使用Spring MVC并将其注入@Controller

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

我使用的是Spring 4.0.0。

我知道在起诉@Autowire时会有这种情况,但是对于@Resource我不希望它尝试在spring xml中实例化所有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)

1 个答案:

答案 0 :(得分:1)

我不知道你是否使用Spring配置文件,但我们在这里使用它来初始化每个环境的不同bean。 从Tomcat我们spcecify使用哪个配置文件,在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>