zoukankan      html  css  js  c++  java
  • 课时11:拆分Sping配置文件,Spring整合Web项目

    .1)拆分Sping配置文件

      1.java项目 :

        applicationContext1.xml, applicationContext2.xml, applicationContext3.xml

        需要哪个就直接加载哪个就可以

      2.web项目:根据什么拆分

        2.1 三层结构:

        applicationDao.xml

        applicationService.xml

        applicationController.xml

        applicationDB.xml

        2.2 功能拆分

        学生相关的功能:applicationStudent.xml

        班级相关的功能:applicationClass.xml

        2.3 合并:如何将多个配置文件 加载

          2.3.1 第一种方式在web.xml中配置

     <!--  指定Ioc容器的位置-->
      <context-param>
        <!--    监听器的父类ContextLoader中有一个属性contextConfigLocation  来指定配置的ioc容器-->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml,
                      classpath:application-Dao.xml,
                      classpath:application-Service.xml,
                      classpath:application-Controller.xml
        </param-value>
      </context-param>
      <!--  指定鉴定器的位置-->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

          2.3.2 也可以这样写 推荐使用

      <!--  指定Ioc容器的位置-->
      <context-param>
        <!--    监听器的父类ContextLoader中有一个属性contextConfigLocation  来指定配置的ioc容器-->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml,
          classpath:application-*.xml
        </param-value>
      </context-param>
      <!--  指定鉴定器的位置-->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

    2.)Spring整合WEB 

      1.将dao service servlet创建好

      2.通过依赖注入一层一层的注入

     

         2.1 application-Controller.xml代码如下

    <bean id="studentServlet" class="net.bdqn.hbz.web.StudentServlet">
            <property name="iStudentService" ref="studentService"></property>
        </bean>

        2.2 application-Service.xml代码如下

     <bean id="studentService" class="net.bdqn.hbz.service.impl.IStudentServiceImpl">
                <property name="iStudentDao" ref="studentDao"></property>
            </bean>

        2.3 application-Dao.xml代码额如下

     <bean id="studentService" class="net.bdqn.hbz.service.impl.IStudentServiceImpl">
                <property name="iStudentDao" ref="studentDao"></property>
            </bean>

      3.测试 通过a标签测试 但是页面报null指针的错误 下节课解答 (因为不在同一个容器中)

        3.1 一个在servlet容器中 一个在springioc中

    package net.bdqn.hbz.web;
    
    import net.bdqn.hbz.service.IStudentService;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/StudentServlet")
    public class StudentServlet extends HttpServlet {
        private IStudentService iStudentService;
    
        public void setiStudentService(IStudentService iStudentService) {
            this.iStudentService = iStudentService;
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String name=iStudentService.queryStudent();
            //传送到页面
            req.setAttribute("name",name);
            req.getRequestDispatcher("result.jsp").forward(req,resp);
        }
    }
  • 相关阅读:
    leetcode Remove Linked List Elements
    leetcode Word Pattern
    leetcode Isomorphic Strings
    leetcode Valid Parentheses
    leetcode Remove Nth Node From End of List
    leetcode Contains Duplicate II
    leetcode Rectangle Area
    leetcode Length of Last Word
    leetcode Valid Sudoku
    leetcode Reverse Bits
  • 原文地址:https://www.cnblogs.com/thisHBZ/p/12512056.html
Copyright © 2011-2022 走看看