禁用Spring的隐式方法依赖注入

时间:2015-07-22 16:14:06

标签: java spring dependency-injection struts2

我目前正在使用Struts来开发一个应用程序,并且hello world页面工作正常,直到我扩展ActionSupport以访问i18n功能。执行此操作后,操作开始始终返回input作为结果字符串。这显然是意外的,因为目前尚未进行验证。调试之后,我注意到Spring决定注入一个字段错误映射,因此验证总会看到一些东西,导致意外的返回值。

injection parameter] 1

这是堆栈跟踪:

Daemon Thread [http-0.0.0.0-8080-2] (Suspended (breakpoint at line 79 in ActionSupport))    
Index(ActionSupport).setFieldErrors(Map<String,List<String>>) line: 79  
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available   
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: not available   
Method.invoke(Object, Object...) line: not available    
BeanWrapperImpl.setPropertyValue(BeanWrapperImpl$PropertyTokenHolder, PropertyValue) line: 1134 
BeanWrapperImpl.setPropertyValue(PropertyValue) line: 903   
BeanWrapperImpl(AbstractPropertyAccessor).setPropertyValues(PropertyValues, boolean, boolean) line: 75  
BeanWrapperImpl(AbstractPropertyAccessor).setPropertyValues(PropertyValues) line: 57    
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).applyPropertyValues(String, BeanDefinition, BeanWrapper, PropertyValues) line: 1450  
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).populateBean(String, RootBeanDefinition, BeanWrapper) line: 1158 
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).autowireBeanProperties(Object, int, boolean) line: 376   
StrutsSpringObjectFactory(SpringObjectFactory).autoWireBean(Object, AutowireCapableBeanFactory) line: 218   
StrutsSpringObjectFactory(SpringObjectFactory).buildBean(Class, Map<String,Object>) line: 198   
StrutsSpringObjectFactory(SpringObjectFactory).buildBean(String, Map<String,Object>, boolean) line: 164 
StrutsSpringObjectFactory(ObjectFactory).buildBean(String, Map<String,Object>) line: 178    
DefaultActionFactory.buildAction(String, String, ActionConfig, Map<String,Object>) line: 22 
StrutsSpringObjectFactory(ObjectFactory).buildAction(String, String, ActionConfig, Map<String,Object>) line: 148    
DefaultActionInvocation.createAction(Map<String,Object>) line: 295  
DefaultActionInvocation.init(ActionProxy) line: 395 
StrutsActionProxy(DefaultActionProxy).prepare() line: 194   
StrutsActionProxy.prepare() line: 63    
StrutsActionProxyFactory.createActionProxy(ActionInvocation, String, String, String, boolean, boolean) line: 37 
StrutsActionProxyFactory(DefaultActionProxyFactory).createActionProxy(String, String, String, Map<String,Object>, boolean, boolean) line: 58    
Dispatcher.serviceAction(HttpServletRequest, HttpServletResponse, ActionMapping) line: 554  
ExecuteOperations.executeAction(HttpServletRequest, HttpServletResponse, ActionMapping) line: 81    
StrutsPrepareAndExecuteFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 99   
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 235  
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 206  
OpenSessionInViewFilter.doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain) line: 232    
OpenSessionInViewFilter(OncePerRequestFilter).doFilter(ServletRequest, ServletResponse, FilterChain) line: 106  
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 235  
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 206  
ReplyHeaderFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 96   
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 235  
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 206  
StandardWrapperValve.invoke(Request, Response) line: 230    
StandardContextValve.invoke(Request, Response) line: 175    
SecurityAssociationValve.invoke(Request, Response) line: 182    
JaccContextValve.invoke(Request, Response) line: 84 
StandardHostValve.invoke(Request, Response) line: 127   
ErrorReportValve.invoke(Request, Response) line: 102    
CachedConnectionValve.invoke(Request, Response) line: 157   
StandardEngineValve.invoke(Request, Response) line: 109 
CoyoteAdapter.service(Request, Response) line: 262  
Http11Processor.process(Socket) line: 844   
Http11Protocol$Http11ConnectionHandler.process(Socket) line: 583    
JIoEndpoint$Worker.run() line: 446  
Thread.run() line: not available    

这基本上是该类包含的所有内容:

public class Index extends ActionSupport implements UserAware
{
    private User user;


    @Override
    public String execute()
    {
        System.out.println("this doesn't print out");
        return "SUCCESS";
    }

    @Override
    public void setUser(User user)
    {
        this.user = user;
    }

    public User getUser()
    {
        return user;
    }
}

我只使用@Autowired带注释的属性注入,所以我想禁用方法注入来解决这个问题。那当然是问题所在。我可能误解了症状。

版本

  • Spring 3.2.9
  • Struts 2.3.24

3 个答案:

答案 0 :(得分:0)

默认情况下,Struts和Spring不能很好地结合在一起,你需要在你的struts lib目录中的WEB-INF / lib中添加struts2-spring-plugin-x.y.z.jar。并在web.xml中正确配置它,请参阅此http://www.tutorialspoint.com/struts_2/struts_spring.htm

答案 1 :(得分:0)

这是因为ActionSupport实现了验证所需的所有接口,例如ValidatableValidationAware

  

ValidationAware个类可以接受Action(类级别)或字段级错误消息。操作级别消息保存在Collection中。字段级错误消息保存在MapString字段名称到List字段错误消息中。

最后一个更具体到注入错误,但它不是Spring作业(Spring使用其对象工厂来构建动作并注入依赖项),它是&#39; sa Struts的工作是支持带有字段错误和操作错误的操作,并且使用validation拦截器做得很好。

  

此拦截器通过标准验证框架运行操作,标准验证框架依次检查针对任何验证规则(在ActionClass-validation.xml等文件中找到)的操作,并添加字段级和操作级错误消息(前提是该行动实施com.opensymphony.xwork2.ValidationAware)。

validation拦截器包含在defaultStack中,默认情况下在任何操作中使用。如果操作不需要验证功能,则可以通过创建新堆栈或覆盖操作配置从堆栈中删除此拦截器。如果将验证提供给其他方法,则可以从拦截器中排除某些方法或应用@SkipValidation注释。这将禁用操作方法的验证。

答案 2 :(得分:0)

我可以通过将none文件中的自动装配类型更改为struts.xml来解决此问题。我最初将其设置为type,因为我认为@Autowire注入不起作用。我想我错了,因为这些注射仍然有效。

<constant name="struts.objectFactory.spring.autoWire" value="type" />

<constant name="struts.objectFactory.spring.autoWire" value="none" />

感谢所有参与者的帮助。