讨教大家,spring与struts整合,本人想用spring注解给struts注入action,但是打印出来却是null,是哪里错了吗?测试代码很简单:
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
@Component
@Scope(“prototype”)
public class MyAction extends ActionSupport{
public MyAction myAction;
public MyAction getMyAction() {
return myAction;
}
@Resource
public void setMyAction(MyAction myAction) {
this.myAction = myAction;
}
@Override
public String execute() throws Exception {
System.out.println(“ok!”);
return super.execute();
}
}
用junit4测试打印myAction的时候是null,<context:annotation-config /><context:component-scan base-package=”com.*“/>也都配了,是哪里错了呢?
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
@Component
@Scope(“prototype”)
public class MyAction extends ActionSupport{
public MyAction myAction;
public MyAction getMyAction() {
return myAction;
}
@Resource
public void setMyAction(MyAction myAction) {
this.myAction = myAction;
}
@Override
public String execute() throws Exception {
System.out.println(“ok!”);
return super.execute();
}
}
用junit4测试打印myAction的时候是null,<context:annotation-config /><context:component-scan base-package=”com.*“/>也都配了,是哪里错了呢?
解决方案
50
有两种整合方式,第一使用spring创建action注解有效,第二使用struts2-spring-plugin插件,必须有struts2-spring-plugin-X.X.X.jar此方法注解无效,默认按name装配,需要有对应的set方法,看题主用的哪种方式了。