Wednesday, January 23, 2008

Spring Autowiring

I was just struggling with an error message I was getting in my application. I am using struts 2, spring and JSP basically.
In my JSP I wanted to save some properties into the name of an account:


<s:textfield name="account.name" value="%{account.name}">
</s:textfield>


The action handling the form which posts the "account.name" property above, does have a setter for the Account interface


public class SaveTransactionAction {
.
.
.
public void setAccount(Account account) {
.
.
.
}

}


Problem is, for some reason the property was not correctly set into the action, and even worst, I got an exception thrown:


2008-01-23 21:57:52,203 ERROR util.InstantiatingNullHandler (InstantiatingNullHandler.java:110) - Could not create and/or set value back on to object
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jc.mny.domain.vo.Account': Could not resolve matching constructor


As I later found out, Spring was trying to autowire the creation of the Account property for my action, but as it was an interface, it could not find the correct class to instantiate. I had two options:

  1. Change the struts configuration not to use the autowire properties from spring.
  2. Declare the creation of the bean I needed in the applicationContext.xml. This is the one I chose, just added the excerpt below into the file:

<bean id="account" class="jc.mny.domain.vo.AccountVO">
</bean>

1 comment:

Anonymous said...

Test to add a empty constructor in this class instead of inserting bean in applicationContext