@Resource注入目标无效。只允许使用setter方法:

时间:2013-08-02 14:51:29

标签: java spring jboss7.x jboss5.x ibatis

我一直在尝试从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的源代码我没找到任何东西。

1 个答案:

答案 0 :(得分: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,要么只是注入字段,使用@PostConstruct注释注释您的init方法:

  @Autowired
  private SqlMapClient sqlMapClient;

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

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