Announcement Announcement Module
No announcement yet.
autowire limitation? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • autowire limitation?

    We are upgrading our application from "xml based injection" to annotations and ran into the following problem. We want to replace the following injections with autowire.

    <!-- implements Generator interface -->
    <bean id="aGenerator" class="AGenerator">
    <bean id="bGenrator" class="BGenerator">
    <bean id="firstController" class="CommonController">
    	<property name="generator">
    		<ref bean="aGenerator" />
    <bean id="secondController" class="CommonController">
    	<property name="generator">
    		<ref bean="bGenerator" />
    The old controller:
    public class CommonController extends WebApplicationObjectSupport extends Controller {
            // DataTableGenerator implements Generator interface
    	private DataTableGenerator generator;
    	// Spring Injection:
    	public void setGenerator(DataTableGenerator generator) {
    		this.generator = generator;
    	// @Override
    	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    		DataSourceHelper.executeDataSourceServletFlow(request, response, generator, false);
    		return null;
    With the following mapping we were able to define different genrator beans depending on the request url

    <prop key="/firstController.htm">firstController</prop>
    <prop key="/secondController.htm">secondController</prop>
    We want to define the controller beans with the @Controller annotation and autoinject the generator beans respectivly depending on the request url. Is that possible or is it simply a limitiation with the autowire annotation?

  • #2
    I simply wouldn't split the one controller to 2 different urls. I would instead call commonController.htm?generator=A and commonController.htm?generator=B, then inject both generators inside the controller and use the request parameter to determine which one to use.

    I also recommend, since you are into annotations, not to map your requests to controllers with xml but to use the @RequestMapping annotation directly on your @Controllers instead:

    public class CommonController { //no extends
    	private DataTableGeneratorA generatorA;
            private DataTableGeneratorB generatorB;
    	public void setGeneratorA(DataTableGeneratorA generatorA) {
    		this.generatorA = generatorA;
    	public void setGeneratorB(DataTableGeneratorB generatorB) {
    		this.generatorB = generatorB;
    	public void doTheJob (@RequestParam("generator") String generator)  {
    		DataSourceHelper.executeDataSourceServletFlow(request, response, "A".equals(generator) ? generatorA : generatorB, false);