Java 当我删除spring-webmvc.jar时,Autowired/Resource不起作用
按照本文给出的答案,我从lib中删除了Java 当我删除spring-webmvc.jar时,Autowired/Resource不起作用,java,spring,spring-mvc,Java,Spring,Spring Mvc,按照本文给出的答案,我从lib中删除了springwebmvc.jar文件,以避免与核心项目中的文件重复。然而,当我这样做时,至少一个bean的@Autowired似乎不再工作了 具有@Autowired的类如下(其中未填充任何字段): 弹簧配置可总结如下: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
springwebmvc.jar
文件,以避免与核心项目中的文件重复。然而,当我这样做时,至少一个bean的@Autowired
似乎不再工作了
具有@Autowired
的类如下(其中未填充任何字段):
弹簧配置可总结如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="my.package" scope-resolver="de.hybris.platform.spring.IgnoreTenantScopeMetadataResolver" />
<mvc:annotation-driven ignore-default-model-on-redirect="true" validator="validator">
<mvc:message-converters>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<alias name="defaultBeforeControllerHandlersList" alias="beforeControllerHandlersList" />
<util:list id="defaultBeforeControllerHandlersList" >
<bean class="be.sbh.site.storefront.interceptors.beforecontroller.SecurityUserCheckBeforeControllerHandler" />
<!-- other beans in the list -->
</util:list>
<alias alias="cmsPageContextService" name="defaultCMSPageContextService" />
<bean id="defaultCMSPageContextService"
class="de.hybris.platform.acceleratorcms.services.impl.DefaultCMSPageContextService">
<!-- Properties -->
</bean>
<alias alias="userService" name="defaultUserService"/>
<bean id="defaultUserService" class="de.hybris.platform.servicelayer.user.impl.DefaultUserService" parent="abstractBusinessService">
<!-- Properties -->
</bean>
</beans>
,则@Autowired
将按预期工作
尝试比较两种配置之间的堆栈跟踪,我发现bean是在服务器启动期间在类org.springframework.context.support.AbstractApplicationContext
中的不同时刻创建的
最后一点:编译和启动服务器期间没有错误
你有什么解决办法吗
谢谢你读我的书
Laurent将需要spring-webmvc.jar。我建议您阅读@Sheetal:谢谢您的评论。springwebmvc.jar
已经包含在当前项目的依赖项中。此外,这里的问题是,即使组件扫描处于配置中,@Autowired
也无法工作。此外,根据您的链接,mvc:annotation-driven
将只扫描不在此类中的注释,如果我没有错的话?所有自动连接字段都为空吗?@user3360241:是的,此类中都为空。除了扫描组件时的@Component
,它们都已填写。当您使用@Componenet注释类时,您是如何检测到有两个实例的。这应该是单身。将需要spring-webmvc.jar。我建议您阅读@Sheetal:谢谢您的评论。springwebmvc.jar
已经包含在当前项目的依赖项中。此外,这里的问题是,即使组件扫描处于配置中,@Autowired
也无法工作。此外,根据您的链接,mvc:annotation-driven
将只扫描不在此类中的注释,如果我没有错的话?所有自动连接字段都为空吗?@user3360241:是的,此类中都为空。除了扫描组件时的@Component
,它们都已填写。当您使用@Componenet注释类时,您是如何检测到有两个实例的。这应该是单身。将需要spring-webmvc.jar。我建议您阅读@Sheetal:谢谢您的评论。springwebmvc.jar
已经包含在当前项目的依赖项中。此外,这里的问题是,即使组件扫描处于配置中,@Autowired
也无法工作。此外,根据您的链接,mvc:annotation-driven
将只扫描不在此类中的注释,如果我没有错的话?所有自动连接字段都为空吗?@user3360241:是的,此类中都为空。除了扫描组件时的@Component
,它们都已填写。当您使用@Componenet注释类时,您是如何检测到有两个实例的。这应该是单身。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="my.package" scope-resolver="de.hybris.platform.spring.IgnoreTenantScopeMetadataResolver" />
<mvc:annotation-driven ignore-default-model-on-redirect="true" validator="validator">
<mvc:message-converters>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<alias name="defaultBeforeControllerHandlersList" alias="beforeControllerHandlersList" />
<util:list id="defaultBeforeControllerHandlersList" >
<bean class="be.sbh.site.storefront.interceptors.beforecontroller.SecurityUserCheckBeforeControllerHandler" />
<!-- other beans in the list -->
</util:list>
<alias alias="cmsPageContextService" name="defaultCMSPageContextService" />
<bean id="defaultCMSPageContextService"
class="de.hybris.platform.acceleratorcms.services.impl.DefaultCMSPageContextService">
<!-- Properties -->
</bean>
<alias alias="userService" name="defaultUserService"/>
<bean id="defaultUserService" class="de.hybris.platform.servicelayer.user.impl.DefaultUserService" parent="abstractBusinessService">
<!-- Properties -->
</bean>
</beans>