Announcement Announcement Module
Collapse
No announcement yet.
autowire limitation? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.

    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
    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);
    	}
    
    }

    Comment

    Working...
    X