@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