{"id":21974124,"url":"https://github.com/javaobjects/demo_spring03_init_web","last_synced_at":"2026-05-07T07:33:39.129Z","repository":{"id":105562400,"uuid":"192717489","full_name":"javaobjects/demo_spring03_init_web","owner":"javaobjects","description":"demo_spring03_init_web","archived":false,"fork":false,"pushed_at":"2019-06-19T17:54:23.000Z","size":6269,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-28T03:17:17.291Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/javaobjects.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-19T11:16:48.000Z","updated_at":"2019-06-19T17:54:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"41bf67db-2477-4d57-a31b-06e2a6228192","html_url":"https://github.com/javaobjects/demo_spring03_init_web","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javaobjects%2Fdemo_spring03_init_web","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javaobjects%2Fdemo_spring03_init_web/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javaobjects%2Fdemo_spring03_init_web/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/javaobjects%2Fdemo_spring03_init_web/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/javaobjects","download_url":"https://codeload.github.com/javaobjects/demo_spring03_init_web/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245033768,"owners_count":20550329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-29T15:38:44.941Z","updated_at":"2026-05-07T07:33:39.074Z","avatar_url":"https://github.com/javaobjects.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Java使用Spring的DI功能改造WEB应用\r\n\r\n1. 创建一个动态WEB工程 Dynamic Web Project 并命名为demo_spring03_init_web\r\n![](53-Images/1.png)\r\n![](53-Images/2.png)\r\n\r\n2. 搭建环境（导入jar包commons-logging beans context core expression test web）\r\n![](53-Images/3.png)\r\n\r\n**添加到libraries路径下面**\r\n![](53-Images/4.png)\r\n\r\n成功后会成这样\r\n\r\n![](53-Images/5.png)\r\n\r\n3. 新建Servlet(controller/UserServlet)\r\n\r\n![](53-Images/6.png)\r\n![](53-Images/7.png)\r\n\r\n**如图所示Servlet报错**\r\n![](53-Images/8.png)\r\n\r\n**解决Servlet报错:**\r\n![](53-Images/9.png)\r\n![](53-Images/10.png)\r\n![](53-Images/11.png)\r\n![](53-Images/12.png)\r\n**如图所示已经解决咯报错**\r\n![](53-Images/13.png)\r\n**删除无用的方法**\r\n![](53-Images/14.png)\r\n**重写Service方法**\r\n![](53-Images/15.png)\r\n![](53-Images/16.png)\r\n\r\n**测试是否能够请求到这个Servlet**\r\n![](53-Images/17.png)\r\n![](53-Images/18.png)\r\n\r\n启动完成\r\n\r\n![](53-Images/19.png)\r\n\r\n输入http://localhost:9090/demo_spring03_init_web/UserServlet(备注我的端口是9090)\r\n\r\n如图所示，测试成功\r\n\r\n![](53-Images/20.png)\r\n\r\n3. 新建Service(service/UserService)\r\n\r\n![](53-Images/21.png)\r\n```\r\npackage service;\r\n\r\npublic class UserService {\r\n\r\n\tpublic boolean login(String username,String password) {\r\n\t\tif(\"zhangsan\".equals(username) \u0026\u0026 \"123456\".equals(password)) {\r\n\t\t\treturn true;\r\n\t\t}else {\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n}\r\n```\r\n4. 拷贝或新建配置文件\r\n![](53-Images/22.png)\r\n**beans.xml**\r\n```\r\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\r\n\r\n\u003cbeans xmlns=\"http://www.springframework.org/schema/beans\"\r\n       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n       xmlns:context=\"http://www.springframework.org/schema/context\" \r\n       xmlns:aop=\"http://www.springframework.org/schema/aop\"      \r\n       xsi:schemaLocation=\"http://www.springframework.org/schema/beans\r\n           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\r\n           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd\r\n           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd\"\u003e\r\n\t\u003cbean id=\"userService\" class=\"service.UserService\"\u003e\u003c/bean\u003e\r\n\u003c/beans\u003e\r\n```\r\n**UserServlet**\r\n```\r\n@Override\r\nprotected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n    System.out.println(\"welcome!!!!\");\r\n    //假设：请求传递过来username:password\r\n    //那么接收参数\r\n    String username = request.getParameter(\"username\");\r\n    String password = request.getParameter(\"password\");\r\n\r\n//\t\tUserService userService = new UserService();//之前的写法\r\n    //使用userservice不需要servlet自己实例化\r\n    //获取web片的spring容器\r\n    WebApplicationContext act = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); \r\n    UserService userService = (UserService) act.getBean(\"userService\");\r\n    //调用底层service\r\n    boolean result = userService.login(username, password);\r\n    //根据调用结果返回响应\r\n    response.setContentType(\"text/html;charset=utf-8\");\r\n    if(result) {\r\n        response.getWriter().write(\"\u003cscript\u003ealert('login success');\u003c/script\u003e\");\r\n    }else {\r\n        response.getWriter().write(\"\u003cscript\u003ealert('login fail');\u003c/script\u003e\");\r\n    }\r\n}\r\n```\r\n5. 配置web.xml让spring容器初始化\r\n![](53-Images/23.png)\r\n![](53-Images/24.png)\r\n\r\n**web.xml中配置一个侦听器侦听已配置的jar包中的spring-web包中的web.context/ContextloaderListener.css**\r\n![](53-Images/25.png)\r\n\r\n```\r\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\r\n\u003cweb-app xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://java.sun.com/xml/ns/javaee\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\" version=\"3.0\"\u003e\r\n  \u003cdisplay-name\u003edemo_spring03_init_web\u003c/display-name\u003e\r\n  \r\n  \u003clistener\u003e\r\n  \t\u003clistener-class\u003e\r\n  \t\torg.springframework.web.context.ContextLoaderListener\r\n  \t\u003c/listener-class\u003e\r\n  \u003c/listener\u003e\r\n  \r\n  \u003cwelcome-file-list\u003e\r\n    \u003cwelcome-file\u003eindex.html\u003c/welcome-file\u003e\r\n    \u003cwelcome-file\u003eindex.htm\u003c/welcome-file\u003e \r\n    \u003cwelcome-file\u003eindex.jsp\u003c/welcome-file\u003e\r\n    \u003cwelcome-file\u003edefault.html\u003c/welcome-file\u003e\r\n    \u003cwelcome-file\u003edefault.htm\u003c/welcome-file\u003e\r\n    \u003cwelcome-file\u003edefault.jsp\u003c/welcome-file\u003e\r\n  \u003c/welcome-file-list\u003e\r\n\u003c/web-app\u003e\r\n```\r\n\r\n**关联源码** ctrl+鼠标左键跳转\r\n\r\n![](53-Images/26.png)\r\n![](53-Images/27.png)\r\n\r\n**如图所示关联成功**\r\n\r\n![](53-Images/28.png)\r\n![](53-Images/29.png)\r\n\r\n**web.xml**\r\n```\r\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\r\n\u003cweb-app xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://java.sun.com/xml/ns/javaee\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\" version=\"3.0\"\u003e\r\n  \u003cdisplay-name\u003edemo_spring03_init_web\u003c/display-name\u003e\r\n\u003c!--\r\n配置一个侦听器，侦听web应用启动事件，\r\n一旦web应用启动,那么listener会读取contextConfigLocation参数值,\r\n也就是读取spring的bean的配置文件然后把文件中注册的bean全部实例化\r\n --\u003e\r\n  \u003clistener\u003e\r\n  \t\u003clistener-class\u003e\r\n  \t\torg.springframework.web.context.ContextLoaderListener\r\n  \t\u003c/listener-class\u003e\r\n  \u003c/listener\u003e\r\n  \u003ccontext-param\u003e\r\n  \t\u003cparam-name\u003e\r\n  \t\tcontextConfigLocation\r\n  \t\u003c/param-name\u003e\r\n  \t\u003cparam-value\u003e/WEB-INF/beans.xml\u003c/param-value\u003e\r\n    \u003c!--\r\n    如果侦听多个的写法是\r\n    \u003cparam-value\u003e/WEB-INF/beans.xml,x,x,x\u003c/param-value\u003e\r\n    或者\r\n    \u003cparam-value\u003e/WEB-INF/*_beans.xml\u003c/param-value\u003e\r\n    或者\r\n    \u003cparam-value\u003e/WEB-INF/beans.xml,classpath:beans.xml\u003c/param-value\u003e\r\n    --\u003e\r\n  \u003c/context-param\u003e\r\n  \r\n  \r\n  \u003cwelcome-file-list\u003e\r\n    \u003cwelcome-file\u003eindex.html\u003c/welcome-file\u003e\r\n    \u003cwelcome-file\u003eindex.htm\u003c/welcome-file\u003e \r\n    \u003cwelcome-file\u003eindex.jsp\u003c/welcome-file\u003e\r\n    \u003cwelcome-file\u003edefault.html\u003c/welcome-file\u003e\r\n    \u003cwelcome-file\u003edefault.htm\u003c/welcome-file\u003e\r\n    \u003cwelcome-file\u003edefault.jsp\u003c/welcome-file\u003e\r\n  \u003c/welcome-file-list\u003e\r\n\u003c/web-app\u003e\r\n```\r\n**UserService**\r\n```\r\npackage service;\r\npublic class UserService {\r\n\tpublic UserService() {\r\n\t\tSystem.out.println(\"userService 实例化了\");\r\n\t}\r\n\r\n\tpublic boolean login(String username,String password) {\r\n\t\tif(\"zhangsan\".equals(username) \u0026\u0026 \"123456\".equals(password)) {\r\n\t\t\treturn true;\r\n\t\t}else {\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n}\r\n```\r\n6. 启动项目测试是否配置成功\r\n![](53-Images/30.png)\r\n![](53-Images/31.png)\r\n\r\n**如图所示,测试成功**\r\n\r\n![](53-Images/32.png)\r\n\r\n**拓展知识：** [Eclipse如何修改启动项目与项目名称不一致的情况](https://www.jianshu.com/p/c6961258679a)\r\n\r\n7. 带参访问UserServlet\r\n\r\nhttp://localhost:9090/demo_spring03_init_web/UserServlet?username=zhangsan\u0026password=123456\r\n\r\n![](53-Images/33.png)\r\n\r\n##### 总结\r\n\r\n第一步：导入jar包\r\n```\r\nbeans context  core  expression  test web  commons-logging\r\n```\r\n\r\n第二步：编写service并配置  编写servlet调用service\r\n```\r\n//获取web版的spring容器\r\nWebApplicationContext act=WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());\r\nUserService userService=(UserService) act.getBean(\"userService\");\r\n```\r\n\r\n第三步：在web.xml中注册listener  \r\n\r\n```\r\n\u003c!-- \r\n  配置一个侦听器，侦听web应用启动事件，\r\n  一旦web应用启动，那么listener会读取contextConfigLocation参数值，也就是读取spring的bean的配置文件\r\n  然后把文件中注册的bean全部实例化  \r\n   --\u003e\r\n  \r\n  \u003clistener\u003e\r\n \u003clistener-class\u003e org.springframework.web.context.ContextLoaderListener\u003c/listener-class\u003e\r\n  \u003c/listener\u003e\r\n  \r\n  \u003ccontext-param\u003e\r\n  \u003cparam-name\u003econtextConfigLocation\u003c/param-name\u003e\r\n  \u003cparam-value\u003e/WEB-INF/beans.xml\u003c/param-value\u003e\r\n  \u003c/context-param\u003e\r\n  ```\r\n\r\n  以上就是我对于**Java使用spring的di功能对web应用进行改造** 总结的全部内容，附上[源代码](https://github.com/javaobjects/demo_spring03_init_web)\r\n\r\n==================================================================\r\n#### 分割线\r\n==================================================================\r\n\r\n**博主为咯学编程：父母不同意学编程，现已断绝关系;恋人不同意学编程，现已分手;亲戚不同意学编程，现已断绝来往;老板不同意学编程,现已失业三十年。。。。。。如果此博文有帮到你欢迎打赏，金额不限。。。**\r\n\r\n![](https://upload-images.jianshu.io/upload_images/5227364-0824589594f944c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavaobjects%2Fdemo_spring03_init_web","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjavaobjects%2Fdemo_spring03_init_web","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjavaobjects%2Fdemo_spring03_init_web/lists"}