spring框架学习 spring集成web环境、自定义scl监听器

我又来水文了,但是!!!真的不水,很认真的在写笔记了,这一次我进度慢一些,因为是周日嘛,早上补了会觉,中午没吃到饭来赶进度写笔记,写的有问题的地方还请大佬们给我拉一把。

回到正题了,昨天学习了注解,但没有对xml文件的作用产生丝毫怀疑。我的代码里有方法使用的强调,包括我自己也经常看代码复习,今天的Junit集成不是很难,重点是啥?重点是我们终于来到了web时代,但是目前和那些华丽的前端页面是不会接壤的,web框架下会有一个web.xml文件,它也是相当重要的,先总结一下今天学到的web.xml标签:

  • servlet标签:主要作用是将写好的web服务类导入进去,name子标签是java类名,class子标签是java类路径。同时还要编写servlet-mapping映射标签对应起来使用。
  • servlet-mapping标签:主要起到映射作用,name子标签是java类名,url子标签是网站页面跳转的动态访问路径,这个其他的jsp文件互相跳估计跟这个脱不了干系。
  • listener标签:主要作用就是设置监听器。自定义或者使用spring提供的监听器都可以。class子标签是自定义的监听器java类路径。使用spring封装监听器的话需要在pom文件导入spring-web包的坐标。
  • context-param标签:用于上下文变量的设置,name子标签是自己设置的名字,value子标签是对应的需要被读取的带后缀的xml上下文文件名。

其他的在代码注释里,合理对照,相互理解,我想吃饭了,再水一下吧!

  • UserDao
  • package com.hlc.dao;
    
    public interface UserDao {
        void save();
    }
  • UserDaoImpl.java
  • package com.hlc.dao;
    
    public class UserDaoImpl implements UserDao{
    
        public void save() {
            System.out.println("sava.....");
        }
    }
    
  • UserService
  • package com.hlc.service;
    
    public interface UserService {
        void service();
    }
    
  • UserServiceImpl.java
  • package com.hlc.service;
    
    import com.hlc.dao.UserDao;
    
    
    public class UserServiceImpl implements UserService{
    
        private UserDao userDao;
    
    //    采用set注入的方式注入bean;注解会好用一些吧
        public void setUserDao(UserDao userDao){
            this.userDao=userDao;
        }
    
        @Override
        public void service() {
            userDao.save();
        }
    }
    
  • ContextLoaderListener.java
  • package com.hlc.Listener;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    //设置监听类,这里标注一下:在web项目里,当服务器一启动,servletContext这个域就一定会被使用到,他里面有一系列的监听器,
    //而我们正在创建的类实现的接口ServletContextListener就是监听服务器启动的一个接口。
    //创建完之后还要在web.xml文件里配置监听器
    public class ContextLoaderListener implements ServletContextListener {
    
    // 初始化上下文
        public void contextInitialized(ServletContextEvent sce) {
    //      利用方法自带的sce去获取servletContext域
            ServletContext servletContext = sce.getServletContext();
    //      在这里去获取web.xml文件中的全局变量
            String context = servletContext.getInitParameter("context");
    //      在web.xml文件里我设置的context指向的是applicationContext.xml文件,达到解耦合的作用
            ApplicationContext app = new ClassPathXmlApplicationContext(context);
    //      将这个spring容器上下文app存到servletContext域中
    //       利用setAttribute();方法将app存在我们获取的域中
            servletContext.setAttribute("app",app);
            System.out.println("spring 容器创建完毕");
        }
    //销毁上下文
        public void contextDestroyed(ServletContextEvent sce) {
    //        这里我有将app取出来,既然是销毁的方法,肯定是要将app这个上下文对象销毁了,就是回收容器。
    //        ServletContext servletContext = sce.getServletContext();
    //        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        }
    }
  • webservlet.java
  • package com.hlc.webservlet;
    
    import com.hlc.service.UserService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class webServlet extends HttpServlet {
    
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //前期的学习发现这句(1)代码的作用就是读取xml文件创建spring容器,每次执行这行代码就会随之出现一个容器,会不会很麻烦且不符合商业逻辑呢?
    //我们来创建一个监听器,只要服务器一启动,我们就可以在比服务器启动器更高的域里设置这个app的实现,放在ServletContext里就再好不过了。
    //      (1) ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    
    //监听器在目录里面的Listener包中配置好并在web.xml文件中投入使用,使用req或者this.getServletContext().var;
    //来获取servletContext对象
            ServletContext servletContext = req.getServletContext();
    
    //过程比较繁琐,练习以及多看代码就明白了,(取得自定义监听器里的app容器,要知道监听器所属的域就是servletContext
            ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
    
    //        这里使用的WebApplicationContextUtils.getWebApplicationContext是spring封装好的监听器,作用于你自己整的监听器一样。
    //        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    
            UserService userService = app.getBean(UserService.class);
            userService.service();
        }
    }
  • applicationContext.xml
  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="userDao" class="com.hlc.dao.UserDaoImpl"></bean>
    
        <bean id="userService" class="com.hlc.service.UserServiceImpl">
    <!--一定记住在bean与bean的互调过程中,调用类要在原类中写好set方法来对应property标签,或者使用注解
    但是注解也要注意设置context空间,且要对包进行componentScan扫描注释-->
            <property name="userDao" ref="userDao"/>
        </bean>
    </beans>
  • web.xml
  • <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    
    <!--  配置变量指向spring的xml文件-->
      <context-param>
        <param-name>context</param-name>
        <param-value>ClassPath:applicationContext.xml</param-value>
      </context-param>
      
    <!--  配置自己写的监听器-->
      <listener>
        <listener-class>com.hlc.Listener.ContextLoaderListener</listener-class>
      </listener>
    
    <!--  配置spring封装好的监听器-->
    <!--  <listener>-->
    <!--    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
    <!--  </listener>-->
    
    <!--  配置web服务-->
      <servlet>
        <servlet-name>webServlet</servlet-name>
        <servlet-class>com.hlc.webservlet.webServlet</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>webServlet</servlet-name>
        <url-pattern>/webServlet</url-pattern>
      </servlet-mapping>
    </web-app>
    
  • index.jsp
  • <html>
    <body>
    <a href="/SpringMVC_war_exploded/webServlet">Hello World!</a>
    </body>
    </html>
  • pom文件
  • <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>org.example</groupId>
      <artifactId>SpringMVC</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <name>SpringMVC Maven Webapp</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
    <!--这个spring-web包里有spring封装好的监听器,我们就不用那么累的码那些代码实现在服务器启动时创建spring容器了-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.1.9.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.9.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.27</version>
        </dependency>
    
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>4.0.1</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>javax.servlet.jsp-api</artifactId>
          <version>2.3.3</version>
        </dependency>
      </dependencies>
    
      <build>
        <finalName>SpringMVC</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-war-plugin</artifactId>
              <version>3.2.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>
    

    水代码?没有,我就是小白,真不敢那句代码不去写,每天看看博客上的大佬们的面试题,慌得很,计算机基础还算可以,有的操作系统也懂得不少,但有一说一,java语言听课上肯定不行呀,就比如intger与int的区别,老师不给你讲,只有自己注意细节慢慢发现钻研。z昨天看了半夜的jvm原理,感觉自己自学能力没毛病,理解也还行,就是有时候过于急功近利了,得静下心来钻研,不能过于要求进步!

下一次进度就是springMVC了,AOP还是得等后面再学,我跟的是黑马程序员的视频课,买的书是浪潮insuper的ssm实战开发与案例详解。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ForestSpringH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值