Announcement Announcement Module
Collapse
No announcement yet.
PizzaShop example and external entities Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PizzaShop example and external entities

    Hi
    let's say, that I have to use already existing mypackage.Pizza entity and not the one created by Roo. There is also mypackage.PizzaManager interface and mypackage.PizzaManagerImpl class, that is responsible for e.g. persistence of mypackage.Pizza.
    PizzaController_Roo_Controller should be modified, so that instead of
    Code:
    @RequestMapping(value = "/pizza", method = RequestMethod.POST)    
        public String PizzaController.create(@Valid Pizza pizza, BindingResult result, ModelMap modelMap) {    
            ...
            pizza.persist();        
            return "redirect:/pizza/" + pizza.getId();        
        }
    it will contain sth like this
    Code:
    @RequestMapping(value = "/pizza", method = RequestMethod.POST)    
        public String PizzaController.create(@Valid Pizza pizza, BindingResult result, ModelMap modelMap, PizzaManager pizzaManager ) {    
            ...
            pizzaManager.persist(pizza);        
            return "redirect:/pizza/" + pizza.getId();        
        }
    Question is how should I inject this mypackage.PizzaManager ?
    Any hints are appreciated.
    Thx in advance
    Łukasz

  • #2
    Dependency injection of services like your PizzaManager can be achieved using the normal Spring conventions. This is the beauty of Roo, it does not do any black magic . So if you annotate your PizzaManagerImpl with @Service and the service is picked up by the component scan (if it is in a registered package for component scanning as per applicationContext.xml) you can use

    Code:
    @Autowired private PizzaManager pizzaManager;
    in your Controller. Note, you should push in your create method in the controller, so Roo will not interfere with your custom code.

    Comment


    • #3
      Well, I've tried that, but if I add
      Code:
      @Autowired private PizzaManager pizzaManager;
      to the Pizza_Roo_Controller, then I can not use it, cause methods' definitions in aspect are static and I get compile time error
      Cannot make a static reference to the non-static field pizzaManager.

      br
      Łukasz

      Comment


      • #4
        You should not try to customize by hand _Roo_ files because there´re managed by Roo.

        As Stephan said you have to "push in" that method to the controller before customizing it, if you have STS you can use "Push in" refactor from AJDT, if not you can simply copy-paste that code from the aspect file into your .java file (remember to delete the PizzaController. in the method declaration) and Roo will automaticly delete it from the aspect and your application will use your customized code.

        Regards.

        Comment


        • #5
          Ok, so I copied/pasted code from PizzaController_Roo_Controller.aj to PizzaController.java, modified it a bit and injected external entity manager. After that Roo removed PizzaController_Roo_Controller.aj by itself and everything works properly now.
          Guys, thx for help

          Łukasz

          Comment


          • #6
            Yeah sorry if I was not clear on this.

            As a side comment, you don't need to copy all code from the PizzaController_Roo_Controller.aj to the PizzaController.java. Instead, you can just copy (push-in) the create method to the Java sources and let roo still manage the rest of the methods. This keeps your controller cleaner (less code in there). So if you delete all methods which don't contain customized code from your PizzaController.java you will see that Roo automatically generates the aspect again in the background.

            HTH

            Comment

            Working...
            X