struts2 在action中获取不到bean

王威 发布于 2011/08/16 10:23
阅读 2K+
收藏 0

【DevOps必读】产品经理与程序员之间如何破局?>>>

spring3 struts2 在action中提供set get方法能得到bean 但注解得不到:
beans.xnl
  <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 
  <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
  <bean id="beforeAddAccount" class="com.communityservice.service.BeforeAddAccount"></bean>
  <bean id="addAccount" class="com.communityservice.action.TestAccount" scope="prototype">
  <property name="beforeAddAccount">
  <ref bean="beforeAddAccount"/>
  </property>
  </bean>

struts.xml

<struts>
<constant name="struts.objectFactory" value="spring"></constant>
  <package name="test" namespace="/test" extends="struts-default">
  <action name="addAccount" class="addAccount" method="execute">
  <result name="success" >/test.jsp</result>
  </action>
  </package>
</struts>

TestAccount.java

  public class TestAccount {
private BeforeAddAccount beforeAddAccount;
public BeforeAddAccount getBeforeAddAccount() {
return beforeAddAccount;
}
public void setBeforeAddAccount(BeforeAddAccount beforeAddAccount) {
this.beforeAddAccount = beforeAddAccount;
}
public String execute(){

return "success";

}
}


BeforeAddAccount .java
public class BeforeAddAccount {

public void before() {
System.out.println("hahahahaha");
}


}

上面代码是正确的 只要把TestAccount.java该成
 public class TestAccount {
  @Autowired
private BeforeAddAccount beforeAddAccount;

public String execute(){

return "success";

}
}
就报错

Unable to instantiate Action, addAccount, defined for 'addAccount' in namespace '/test'Error creating bean with name 'addAccount' defined in file [D:\tomcat-6.0\webapps\zyserver\WEB-INF\classes\applicationContext-action.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'beforeAddAccount' of bean class [com.communityservice.action.TestAccount]: Bean property 'beforeAddAccount' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:318)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:399)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:198)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

前提 注解 是好使的 在service里都可以
加载中
0
王威
王威

如果只用@Autowired 的话:

  <bean id="addAccount" class="com.communityservice.action.TestAccount" scope="prototype">
  <property name="beforeAddAccount">
  <ref bean="beforeAddAccount"/>
  </property>
  </bean>

中的 <ref bean="beforeAddAccount"/> 是不是不用写了

0
呆瓜
呆瓜

public class TestAccount {

上面没有加标注,不加标注的话,spring是不会管理这个bean的,里面的@Autowired也就没用了,

应该

@Service
public class TestAccount{
       ..........
}

//如果用标注的话,就不要配置文件了。只需要struts2的配置文件即可
@Service("addAccout")
public class TestAccount{
       ..........
}
王威
王威
嗯,以前没咋用过注解 现在也在慢慢尝试。当前的问题 : 应该不是因为没有@sercice 导致的 因为那个是Action 在配置文件里已经是交给spring管理了 理论上讲 不去标记为bean也没问题的。我想是不是因为上面说的那个 原因 用注解 不要<ref bean="beforeAccount" />这句了。现在没环境 ,==再试试
0
高东
高东
一般不用注解
返回顶部
顶部