Results 1 to 2 of 2

Thread: autowire limitation?

  1. #1
    Join Date
    Aug 2007
    Posts
    8

    Default 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.

    Code:
    <!-- implements Generator interface -->
    <bean id="aGenerator" class="AGenerator">
    <bean id="bGenrator" class="BGenerator">
    
    <bean id="firstController" class="CommonController">
    	<property name="generator">
    		<ref bean="aGenerator" />
    	</property>
    </bean>
    <bean id="secondController" class="CommonController">
    	<property name="generator">
    		<ref bean="bGenerator" />
    	</property>    
    </bean>
    The old controller:
    Code:
    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

    Code:
    <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. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    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:

    Code:
    @Controller
    @RequestMapping("/commonController.htm")
    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;
    	}
    
            @RequestMapping(method=RequestMethod.GET)
    	public void doTheJob (@RequestParam("generator") String generator)  {
    		DataSourceHelper.executeDataSourceServletFlow(request, response, "A".equals(generator) ? generatorA : generatorB, false);
    	}
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •