SpringMVC 温习笔记(一)服务器启动和组件解析

末蓝、 2022-10-25 01:00 40阅读 0赞

spring 组件扫描解析

首先小猿先引入一张图片,该图片可以全面的诠释自定义组件的全部运行过程:
在这里插入图片描述
整个过程中最为核心的思想就是tomcat服务器启动的时候可以直接通过监听器去加载spring配置文件applicationContext.xml文件,以这种方式,我们可以借助tomcat服务器加载监听器的过程,可将service层,dao层的各类bean纳入到spring容器中来进行管理,这样就使得整个启动过程变得非常完美,总之自定义监听类的本质作用是将tomcat容器初始化和spring容器初始化紧密的链接在了一起
下面小猿将整个小案例的代码附在下面。

项目依赖

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <version>5.1.32</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>c3p0</groupId>
  8. <artifactId>c3p0</artifactId>
  9. <version>0.9.1.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.alibaba</groupId>
  13. <artifactId>druid</artifactId>
  14. <version>1.1.10</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>junit</groupId>
  18. <artifactId>junit</artifactId>
  19. <version>4.12</version>
  20. <scope>test</scope>
  21. </dependency>
  22. <dependency>
  23. <groupId>commons-fileupload</groupId>
  24. <artifactId>commons-fileupload</artifactId>
  25. <version>1.3.3</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-context</artifactId>
  30. <version>5.0.5.RELEASE</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-test</artifactId>
  35. <version>5.0.5.RELEASE</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework</groupId>
  39. <artifactId>spring-web</artifactId>
  40. <version>5.0.5.RELEASE</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework</groupId>
  44. <artifactId>spring-webmvc</artifactId>
  45. <version>5.0.5.RELEASE</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>javax.servlet</groupId>
  49. <artifactId>javax.servlet-api</artifactId>
  50. <version>3.0.1</version>
  51. <scope>provided</scope>
  52. </dependency>
  53. <dependency>
  54. <groupId>javax.servlet.jsp</groupId>
  55. <artifactId>javax.servlet.jsp-api</artifactId>
  56. <version>2.2.1</version>
  57. <scope>provided</scope>
  58. </dependency>

dao 层

接口

  1. public interface UserDao {
  2. public void save();
  3. }

实现类

  1. public class UserDaoImpl implements UserDao {
  2. public void save() {
  3. System.out.println("save running .......");
  4. }
  5. }

service层

接口

  1. public interface UserService {
  2. public void save();
  3. }

实现类

  1. public class UserServiceImpl implements UserService {
  2. private UserDao userDao;
  3. public void setUserDao(UserDao userDao) {
  4. this.userDao = userDao;
  5. }
  6. public void save() {
  7. userDao.save();
  8. }
  9. }

自定义监听类和工具类

监听类

  1. public class ContextLoaderListener implements ServletContextListener{
  2. public void contextInitialized(ServletContextEvent sce) {
  3. ServletContext servletContext = sce.getServletContext();
  4. String contextConfigLocation = servletContext
  5. .getInitParameter("contextConfigLocation");
  6. ApplicationContext applicationContext = new
  7. ClassPathXmlApplicationContext(contextConfigLocation);
  8. servletContext.setAttribute("applicationContext",applicationContext);
  9. System.out.println("容器已经创建完毕。。。。。。");
  10. }
  11. public void contextDestroyed(ServletContextEvent sce) {
  12. }
  13. }

工具类

  1. public class WebApplicationContextUtils {
  2. public static ApplicationContext getAttribute(ServletContext servletContext){
  3. return (ApplicationContext) servletContext.getAttribute("applicationContext");
  4. }
  5. }

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  3. <!--加载外部的properties文件-->
  4. <context:property-placeholder location="classpath:jdbc.properties"/>
  5. <!--配置数据源-->
  6. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  7. <property name="driverClass" value="${jdbc.driver}"></property>
  8. <property name="jdbcUrl" value="${jdbc.url}"></property>
  9. <property name="user" value="${jdbc.username}"></property>
  10. <property name="password" value="${jdbc.password}"></property>
  11. </bean>
  12. <!--配置Dao-->
  13. <bean id="userDao" class="com.feitian.component.dao.impl.UserDaoImpl"></bean>
  14. <!--配置service-->
  15. <bean id="userService" class="com.feitian.component.servicce.impl.UserServiceImpl">
  16. <property name="userDao" ref="userDao"/>
  17. </bean>
  18. </beans>

当然此处配置的数据库暂时没有用。

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  3. <!--将applicationContext.xml放入ServletContext,方便自定义监听类初始化该文件。-->
  4. <context-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:applicationContext.xml</param-value>
  7. </context-param>
  8. <!--监听器启动时要加载定义监听类-->
  9. <listener>
  10. <listener-class>com.feitian.component.web.listener.ContextLoaderListener</listener-class>
  11. </listener>
  12. <!--将UserServlet加载到web容器中-->
  13. <servlet>
  14. <servlet-name>UserServlet</servlet-name>
  15. <servlet-class>com.feitian.component.web.servlet.UserServlet</servlet-class>
  16. </servlet>
  17. <!--映射UserServlet为userServlet-->
  18. <servlet-mapping>
  19. <servlet-name>UserServlet</servlet-name>
  20. <url-pattern>/userServlet</url-pattern>
  21. </servlet-mapping>
  22. </web-app>

web层

  1. public class UserServlet extends HttpServlet {
  2. @Override
  3. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  4. throws ServletException, IOException {
  5. ServletContext servletContext = this.getServletContext();
  6. ApplicationContext applicationContext =
  7. WebApplicationContextUtils.getAttribute(servletContext);
  8. UserService userService = applicationContext.getBean(UserService.class);
  9. userService.save();
  10. }
  11. @Override
  12. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  13. throws ServletException, IOException {
  14. this.doGet(req,resp);
  15. }
  16. }

上述案例基本上完整的演绎了整个tomcat服务器启动并加载spring容器的过程,从下节小猿正式开始复习springMVC。

发表评论

表情:
评论列表 (有 0 条评论,40人围观)

还没有评论,来说两句吧...

相关阅读