0

我一直在尝试从 JBOSS 5 升级到 jboss 7,但出现以下异常。

at java.lang.Thread.run(Thread.java:619) [rt.jar:1.6.0_20]
Caused by: java.lang.IllegalArgumentException: JBAS011089: @Resource injection target is invalid.  Only setter methods are allowed: void com.myaccount.admin.manager.dao.ManagerDaoImpl.init(com.ibatis.sqlmap.client.SqlMapClient)
    at org.jboss.as.ee.component.deployers.ResourceInjectionAnnotationParsingProcessor.processMethodResource(ResourceInjectionAnnotationParsingProcessor.java:192)
    at org.jboss.as.ee.component.deployers.ResourceInjectionAnnotationParsingProcessor.deploy(ResourceInjectionAnnotationParsingProcessor.java:153)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    ... 5 more

这是代码

  @Repository("managerDao")
  public class ManagerDaoImpl extends SqlMapClientDaoSupport implements ManagerDao {

  @Resource(name="sqlMapClient_ADMIN")
  public void init(SqlMapClient sqlMapClient) {
      setSqlMapClient(sqlMapClient);
   }

  ......

我用谷歌搜索,但除了 jboss jar 的源代码外,我什么也没找到。

4

1 回答 1

4

如果您查看ResourceInjectionAnnotationParsingProcessor processMethodResource源代码,您会看到:

if (!methodName.startsWith("set") || methodInfo.args().length != 1) {
    throw new IllegalArgumentException("@Resource injection target is invalid.  Only setter methods are allowed: " + methodInfo);
}

它检查方法名称是否不以它开头"set"是否引发异常。因此,要么重命名您的init方法以开始set或仅注入字段,使用@PostConstructinit注释来注释您的方法,如下所示:

  @Autowired
  private SqlMapClient sqlMapClient;

  @PostConstruct
  public void init() {
      setSqlMapClient(sqlMapClient);
  }

使用 post 构造注释方法可能是更好的解决方案,因为将来您可能需要为初始化添加(或更改)更多内容,因此您唯一需要做的就是修改init()方法。

于 2013-08-02T15:12:13.197 回答