public class TestAction extends ActionSupport{
private TestService testService;
public void setTestService(TestService testService) {
this.testService= testService;
}
}
两种方式:
- 自动装配,不在spring中注入Action
在struts.xml中:
<action name="testAction" class="com.xxxxx.action.TestAction">
就可以了
- spring配置
spring.xml中
<bean id="testAction" class="com.xxxxx.action.TestAction" >
<property ref="testService" />
</bean>
struts.xml中
<action name="testAction" class="testAction">
在struts.xml中action指定的class像上面这种方式指定全类路径名的话,这时,不论spring配置文件中的<bean id="loginAction" class="org.xxxxx.action.LoginAction"></bean>有没有指定<property name="testService" ref="testService"/>,只要有<bean id="testService" .../>存在,并且Action中有相应的set方法,当实例化Action类时,会一并将service的实例注入
如果<action name="testAction" class="testAction">这里的class指定spring配置文件中的bean的id,则不会出现testService自动注入问题,而是根据<bean id="testAction" class="com.xxxxx.action.TestAction"></bean>有没有指定<property name="testService" ref="testService"/>来决定,有指定则实例化Action类时,会一并将testService实例注入,没有配置service则为空