我有一個請求scoped bean,我需要訪問HttpServletResponse和HttpServletRequest對象。我如何在Spring 3.1中構造自動裝載HttpServletResponse?
我需要在構造函數中訪問這些對象,所以屬性自動裝配不是一個選項。
我做了以下內容:
@Component
@Scope("request")
public class MyClass{
@Autowired(required=true)
public MyClass(HttpServletRequest request, HttpServletResponse response) {
// do stuff I need to do in the constructor
}
}
這給了我下面的錯誤:
No default constructor found; nested exception is java.lang.NoSuchMethodException: com.foo.bar.MyClass.()
根據此錯誤信息,它正在尋找paramless默認的構造函數,它不存在。我自動裝配構造函數,並將「required」設置爲true,所以注入器應該選擇這個構造函數。由於這個bean是請求範圍,所以它應該可以工作,但事實並非如此。
任何想法?
謝謝。自定義解決方案看起來有點冒失,所以我重寫了我的邏輯,把bean函數放到HandlerInterceptor中,默認情況下這兩個對象可用。 – GeorgeStone 2012-01-17 23:21:41
如果我在做一個portlet,PortletRequest可用於自動裝配? – rascio 2012-01-31 16:02:50