zoukankan      html  css  js  c++  java
  • Spring在Web中使用的基本思路

    1. 加入额外的jar包:spring-web-4.0.0.RELEASEspring-webmvc-4.0.0.RELEASE。

    2. Spring的配置文件没有什么不同之处,按常规配置即可。

    3. 如何创建IOC容器:

         (1). 在非Web应用中,我们直接在main()中创建IOC容器的实例对象,即:

    public static void main(String[] args) {
            //1.创建Spring的IOC容器对象,在该过程中会调用实体类的构造器和set方法
            ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
            //2.从IOC容器中获取实例对象
            HelloWorld helloWorld = (HelloWorld) act.getBean("helloworld");
            //3.调用实例方法
            helloWorld.hello();
    }

         (2). 在Web应用中如何创建呢?在Web应用被服务器加载时就创建IOC容器的实例。

                此时可以利用监听器,我们知道监听器是Servlet规范中定义的一种特殊的类,它主要是对ServletContext、Session和Request的监听。所以

         当监听器监听ServletContext的时候,可以在ServletContextListener中的contextInitialized(ServletContextEvent  sce)中创建IOC容器。

          (3). Web应用中的其他组件如何来访问IOC容器呢?

                 当在ServletContextListener中的contextInitialized(ServletContextEvent  sce)中创建IOC容器以后,将IOC实例存放到

          ServletContext的一个属性中(即application域中)。

          (4). 在开发中为了增加应用的灵活性,可以将Spring配置文件的名字和位置配置到当前应用的初始化参数当中比较好,即在web.xml文件中配置。

        (5). 测试代码: 

    实体类:

    package bean;
    
    public class HelloWorld {
        public void hello() {
            System.out.println("Hello,My name is Kate.");
        }
    }

    Spring配置文件:

    <?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="helloWorld" class="bean.HelloWorld"></bean>
    </beans>

    Servlet配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        
        <!-- 表示application域中的参数 -->
        <context-param>
            <param-name>configLocation</param-name>
            <param-value>bean.xml</param-value>
        </context-param>
        
        <!-- 配置监听器 -->
        <listener>
            <listener-class>listener.IOCListener</listener-class>
        </listener>
        
        <servlet>
            <servlet-name>TestSpringServlet</servlet-name>
            <servlet-class>servlet.TestSpringServlet</servlet-class>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>TestSpringServlet</servlet-name>
            <url-pattern>/testSpring</url-pattern>
        </servlet-mapping>
    </web-app>

    监听器:

    package listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class IOCListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            //1.获取Spring配置文件的名称
            ServletContext servletContext = sce.getServletContext();
            String config = servletContext.getInitParameter("configLocation");
            //2.创建IOC容器
            ApplicationContext ac = new ClassPathXmlApplicationContext(config);
            //3.把IOC容器实例存放到ServletContext的属性中
            servletContext.setAttribute("applicationContext", ac);
        }
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            System.out.println("IOCListener destroyed...");
        }
    }

    测试类:

    package servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.context.ApplicationContext;
    
    import bean.HelloWorld;
    
    public class TestSpringServlet extends HttpServlet {
        
        private static final long serialVersionUID = 1L;
        
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            //获取IOC容器实例
            ApplicationContext ac = (ApplicationContext) this.getServletContext()
                    .getAttribute("applicationContext");
            System.out.println(ac);
            HelloWorld hello = (HelloWorld) ac.getBean("helloWorld");
            hello.hello();
        }
    
    }
  • 相关阅读:
    JSP error: Only a type can be imported
    关于JAVA插入Mysql数据库中文乱码问题解决方案
    MySQL SQL优化——分片搜索
    myeclipse 调试JSP页面
    jsp:usebean 常用注意事项
    spring XML格式
    VB 要求对象
    VB 对象变量或with块变量未设置
    Spring依赖注入
    Spring 读取XML配置文件的两种方式
  • 原文地址:https://www.cnblogs.com/shi-blog/p/4133380.html
Copyright © 2011-2022 走看看