当前位置:编程学习 > JAVA >>

servlet中注入spring托管bean的方法

思路:把servlet配置为spring的bean,就可以实现其他bean的注入,然后使用代理servlet来辅助配置和运行:

 

一、代理servlet的写法:

 

view sourceprint?01 import javax.servlet.ServletException; 

02   

03 import javax.servlet.http.HttpServlet; 

04   

05 import javax.servlet.http.HttpServletRequest; 

06   

07 import javax.servlet.http.HttpServletResponse; 

08   

09    

10   

11 import org.apache.commons.logging.Log; 

12   

13 import org.apache.commons.logging.LogFactory; 

14   

15 import org.springframework.web.context.WebApplicationContext; 

16   

17 import org.springframework.web.context.support.WebApplicationContextUtils; 

18   

19    

20   

21 /** 

22   

23  * HttpServlet 代理 

24   

25  * @author lwei 

26   

27  * @since 2011-03-17 

28   

29  * @version 1.0 

30   

31  */

32   

33 public class HttpServletProxy extends HttpServlet { 

34   

35     /** 

36   

37      * random serialVersionUID 

38   

39      */

40   

41     private static final long serialVersionUID = -7208519469035631940L; 

42   

43     Log logger = LogFactory.getLog(HttpServletProxy.class); 

44   

45     private String targetServlet; 

46   

47     private HttpServlet proxy; 

48   

49       

50   

51     public void init() throws ServletException { 

52   

53         this.targetServlet = getInitParameter("targetServlet"); 

54   

55         getServletBean(); 

56   

57         proxy.init(getServletConfig()); 

58   

59         logger.info(targetServlet + " was inited by HttpServletProxy  successfully......"); 

60   

61     } 

62   

63       

64   

65     private void getServletBean() { 

66   

67         WebApplicationContext wac = 

68   

69         WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); 

70   

71         this.proxy = (HttpServlet) wac.getBean(targetServlet); 

72   

73     } 

74   

75    

76   

77     @Override

78   

79     public void service(HttpServletRequest request, HttpServletResponse response) 

80   

81             throws ServletException, IOException, RuntimeException { 

82   

83         proxy.service(request, response);        

84   

85     } 

86   

87 }


二、业务servlet的写法:

 

 


view sourceprint?01 import java.io.IOException; 

02   

03    

04   

05 import javax.servlet.ServletException; 

06   

07 import javax.servlet.ServletOutputStream; 

08   

09 import javax.servlet.http.HttpServlet; 

10   

11 import javax.servlet.http.HttpServletRequest; 

12   

13 import javax.servlet.http.HttpServletResponse; 

14   

15    

16   

17 /** 

18   

19  * @author lwei 

20   

21  */

22   

23 public class UserCheckServlet extends HttpServlet { 

24   

25    

26   

27     /** 

28   

29      * random serialVersionUID 

30   

31      */

32   

33     private static final long serialVersionUID = 3075635113536622929L; 

34   

35       

36   

37     private UserService userService;(UserService 是spring托管的bean,通过set方法注入) 

38   

39    

40   

41     //如果直接配置在spring配置文件中,则set方法不是必须的。 

42   

43         //具体怎么注入bean,看个人使用spring的习惯了 

44   

45     public void setUserService (UserService userService) { 

46   

47         this.userService = userService; 

48   

49     } 

50   

51    

52   

53     public UserCheckServlet() { 

54   

55         super(); 

56   

57     } 

58   

59    

60   

61     public void init() throws ServletException { 

62   

63         super.init(); 

64   

65     } 

66   

67    

68   

69     @Override

70   

71     public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServle

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,