开发者

@Autowired property in bean constructed as MVC method parameter

I'm using the Spring MVC framework. In the code fragment below, is it possible for me to have Spring inject a bean into the MyRequestClass request parameter when it is constructed (ie. using @Autowired or ApplicationContextAware interface)? I need that request object to access one of my singleton Spring beans and would rather not have to create an ApplicationContextAware implementing singleton just for this purpose.

So far, I'm just getting NullPointerExceptions trying to access the beanaroo property with either approach.

@RequestMapping("/**")
@Controller
public class MyController {

    @RequestMapping(value = "/mymethod", m开发者_如何学Pythonethod = RequestMethod.POST)
    public ModelAndView myMethod(@RequestBody MyRequestClass request, 
                                   ModelMap modelMap,
                                   HttpServletResponse response) {
        ...
    }
}

eg.

public class MyRequestClass {

    @Autowired
    private MyInjectedBean beanaroo;

}

I also tried defining a prototype bean of that class in my application context file but it didn't have any effect either.


You can, using @Configurable and aspectJ, but I wouldn't if I were you.

I'd just have the bean have a method that takes MyInjectedBean as param, and call that from the controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜