spring mvc 初级流程代码

	应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,
	但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,
	'这样的弊端是配置文件加载多次,应用上下文对象创建多次。'

	在Web项目中,可以使用S'ervletContextListener监听Web应用的启动',我们可以在Web应用启动时,
	就加载Spring的配置文件,创建应用上下文对象ApplicationContext,
	在将其存储到最大的域servletContext域中,
	这样就可以在任意位置从域中获得应用上下文'ApplicationContext'对象了。
	tomcat服务器启动之初就创建了上下文引用,且只有一份,提升用户体验,优化代码性能。 

web.xml

<?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">

    <!--全局初始化参数-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--配置监听器-->
    <listener>
        <listener-class>com.itheima.listener.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.itheima.web.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>
</web-app>

applicationContext.xml 对象创建交给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"
       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">

    <!--配置Dao-->
    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>

    <!--配置service-->
    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

</beans>

dao

package com.itheima.dao.impl;

import com.itheima.dao.UserDao;

public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("save running....");
    }
}

service

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save() {
        userDao.save();
        System.out.println("service也运行过了");
    }
}

servlet

package com.itheima.web;

import com.itheima.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 UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
    throws ServletException, IOException {
        //ApplicationContext app = 
        	new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = this.getServletContext();
        //ApplicationContext app =
        	 (ApplicationContext) servletContext.getAttribute("app");
        //ApplicationContext app = 
        WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ApplicationContext app = 
        WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

监听器

'服务器启动的时候就创建上下文应用ApplicationContext,提升用户体验性,并存入ServletContext'

package com.itheima.listener;

import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextLoaderListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
       /* ServletContext servletContext = servletContextEvent.getServletContext();
        //读取web.xml中的全局参数
        String contextConfigLocation = servletContext.
        	getInitParameter("contextConfigLocation");
        ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
        //将Spring的应用上下文对象存储到ServletContext域中
        servletContext.setAttribute("app",app);*/
        ServletContext sc = servletContextEvent.getServletContext();
       ApplicationContext app = new 
       ClassPathXmlApplicationContext(sc.getInitParameter("contextConfigLocation"));

        sc.setAttribute("app",app);
        //服务器启动的时候就创建上下文应用,提升用户体验性
        System.out.println("spring容器创建完毕....");
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}

最大域中获取ApplicationContext

public class ApplicationContextUtils {

    public static ApplicationContext getApplicationContext(ServletContext sc) {
               return (ApplicationContext) sc.getAttribute("app");
    }
}

你可能感兴趣的:(spring,mvc,JAVA)