根据此question中给出的答案,我已从我的lib中删除了spring-webmvc.jar
文件,以避免重复核心项目中的文件。但是,当我这样做时,似乎至少一个bean的@Autowired
不再起作用。
具有@Autowired
的类如下(其中没有填充任何字段):
public class SecurityUserCheckBeforeControllerHandler implements BeforeControllerHandler
{
@Resource(name = "userService")
private UserService userService;
@Autowired
private CMSPageContextService cmsPageContextService;
@Override
public boolean beforeController(final HttpServletRequest request, final HttpServletResponse response,
final HandlerMethod handler) throws IOException
{
// Code where the autowired fields are used (-> produces null pointer)
}
}
弹簧配置可归纳如下:
<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>
如果我遵循大多数类似问题中给出的建议(即在要扫描的类上方添加@Component
),则将创建两次bean:
NullPointerException
component-scan
:
这些字段已正确自动装配,但该列表中未使用该bean。奇怪的是,如果我因为question而放回先前删除的spring-webmvc.jar
,@Autowired
将按预期工作。
尝试比较两种配置之间的堆栈跟踪,我看到在服务器启动期间在类org.springframework.context.support.AbstractApplicationContext
的不同时刻创建了bean。
最后一点:在编译和启动服务器期间没有错误。
您对解决方案有任何想法吗?
感谢您阅读我,
劳伦