---
title: "struts2与Spring3集成中Action的自动装配不起作用解决方法"
author: "glyustb"
date: "2012-04-12T18:25:59Z"
tags: []
categories: ["企业架构"]
slug: "struts2-spring3-action"
draft: false
lastmod: "2012-04-12T18:25:59Z"
id: 1484698
url: "https://glyustb.iteye.com/blog/1484698"
---

今天碰到一个Spring的自动装配问题，关于Struts2和Spring3.0的集成。在网上搜到一篇文章：

[http://aeqopq231.blog.stcn.com/archives/2012/407199.html](http://aeqopq231.blog.stcn.com/archives/2012/407199.html)，

该文章指出使用autodect来解决，实际上这是错误的。autodect代表在bean寻找的方式上使用type或者**constructor的方式来查找bean，因此不是这个问题。经过我的调试，我发现问题出现在beans的头声明上：**

**我原来的声明方式是：**

&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
 xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd](http://www.springframework.org/schema/beans/spring-beans-3.0.xsd)"  default-autowire="byName"&gt;

 

应更改为：

&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "[http://www.springframework.org/dtd/spring-beans.dtd](http://www.springframework.org/dtd/spring-beans.dtd)"&gt;  
&lt;beans default-autowire="byName"&gt;

 

该设置除了开头的applictaion.xml不是这么配置外，其他关于spring的配置文件均如此，这样default-autowire="byName"的装配方式才会在struts2+spring3中真正起效！