Announcement Announcement Module
Collapse
No announcement yet.
@Autowired for @ModelAttribute Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowired for @ModelAttribute

    Hi @all,

    I'm pretty new to Spring, so sorry, if the question is quite simple, but I
    couldn't find an answer in the reference or by googling.

    I've got a @Controller, in which the @Autowired works quite good.

    Code:
    @Controller
    @RequestMapping("/my")
    public class MyController {
    	private ApplicationContext applicationContext;
    	
    	@Autowired
    	public void setApplicationContext(ApplicationContext context){
    		this.applicationContext = context;
    	}
    		
    	@RequestMapping(value="/form", method = RequestMethod.POST)
    	public ModelAndView handlePost(@ModelAttribute("foo") Foo foo, BindingResult result){
    		// ...
    		// applicationContext.getBean(MySessionBean.class).use();
    	}
    }
    But when I submit my form, the @Autowired field in my Foo object remains null.

    Code:
    @Service
    @Scope("request")
    public class Foo {
    
    	@Autowired
    	private Bar bar;
    }
    
    @Service
    @Scope("singleton")
    public class Bar {
    }
    I also tried to write a setter with @Autowired...

    I think it's because of the way Foo is instantiated.
    How can I solve this issue and have my singleton bean assigned to the field?

    btw:
    What is the best way to access a bean of the type session in my controller?
    Is it proper to get the ApplicationContext by @Autowired and call getBean() on it?
    How would you do this?

    Thank you for your help,
    Mark

  • #2
    I have the same problem, did anyone find a solution?

    Thanks

    Comment


    • #3
      It is difficult to understand exactly what you are trying to do, but I can tell you what is going on with your code.

      When you have a Java bean as a parameter to a controller handler method (a @Controller method with @RequestMapping on it), Spring will instantiate a copy of that bean each time the handler method is called. The lifecyle of this bean only applies to the one method invocation - it is not added to the application context.

      This technique of putting a bean on a handler method signature is typically only done for form-backing beans, not for beans matching the @Service stereotype like in your example. Assuming that the "@Controller MyController", the "@Service Foo", and the "@Service Bar" classes are all in packages covered by component scanning, you should be able to just autowire the Foo class into the controller.

      So, you might want your code to look something like this:

      Code:
      @Controller
      @RequestMapping("/my")
      public class MyController {
      	@Autowired
      	private Foo foo;
      	
      	@RequestMapping(value="/form", method = RequestMethod.POST)
      	public ModelAndView handlePost(@ModelAttribute FormBackingBean bean, BindingResult result) {
      		// do something with foo
      	}
      }
      Autowiring the ApplicationContext into a controller would be unusual, and is bad practice IMO. You should wire the specific beans that your controller needs into the controller.

      Likewise, you should not wire a session object into your controller. Using a session-scoped or request-scoped bean is usually a much better design.

      Comment

      Working...
      X