Spring3: adding bean properties to an annotated controller
Requirement: given a Spring3 annotated controller, set bean properties on it
Comments: I have a Spring 3 controller defined with an annotation:
@Controller
public class MyController {
...
public String getFred() {...}
public void setFred(String x) {...}
...
}
I want to set the value of 'fred' from the XML config file.
Normally, the only place MyController is defined is in the class. An component scan of the form:
<context:component-scan base-package="my.package"/>
(in webBeans.xml or similar) finds this class and associates it with requests.
Solution:
To configure this bean with properties, we need to define it in webBeans.xml:
<bean id="myController" class="my.package.MyController" >
<property name="fred" value="fredsValue" />
</bean>
Note that we need to set the id *and* the class. The id also needs, I think, to match the id that gets autogenerated for the class. This isn't well documented, and if it isn't right, it'll fail with either a duplicate bean or a missing bean.
Also, if you need to autowire, you can set autowire="byType" or whatever on the bean.