2025-02-15

思路:把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 httpServletResponse) 

72   

73             throws ServletException, IOException, RuntimeException { 

74   

75         …. 

76   

77         …. 

78   

79         userService.getUserByCode();(註入後bean的使用) 

80   

81         …. 

82   

83         …. 

84   

85     }     

86   

87 }

 

 

三、業務serlvet配置為spring的bean:

<bean id="userCheckServlet" class="com.XXX.xxxxx.web.UserCheckServlet " />

四、web.xml中業務servlet的配置:

 

view sourceprint?01 <servlet> 

02   

03         <servlet-name>UserCheckProxy</servlet-name> 

04   

05         <servlet-class>com.XXX.xxxx.web.HttpServletProxy</servlet-class> 

06   

07         <init-param> 

08   

09             <param-name>targetServlet</param-name> 

10   

11             <param-value>userCheckServlet</param-value>(業務servlet配置為spring的bean時的名字) 

12   

13         </init-param> 

14   

15     </servlet> 

16   

17     <servlet-mapping> 

18   

19         <servlet-name>UserCheckProxy</servlet-name> 

20   

21         <url-pattern>/UserCheck</url-pattern> 

22   

23     </servlet-mapping>

作者“lwei的博客”
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *