方式:通过listener监听器来监听,在webapp启动时加载application.xml(spring的xml文件),
在spring框架中提供了相应的监听器不需要使用者写,小编自定义模仿了一下,核心就是将加载的配置文件存放在最大的域对象之中,需要时利用对应的封装工具类加载得到对应的spring容器,在从容器中取出对应的Bean对象!
首先时配置对应的xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--lister集成spring-->
<!--<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>com.atchengdu.lisenter.Contextloderlisten</listener-class>
</listener>-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Usersevlet</servlet-name>
<servlet-class>com.atchengdu.web.Usersevlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Usersevlet</servlet-name>
<url-pattern>/user</url-pattern>
</servlet-mapping>
</web-app>
简单的写出监听器(加载xml文件配置的spring文件)
package com.atchengdu.lisenter;
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 Contextloderlisten implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
//将spring存储到最大的域中
ApplicationContext context=new ClassPathXmlApplicationContext(contextConfigLocation);
servletContext.setAttribute("apploder",context);
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
最后web层获得对应的容器和对象,调用方法
package com.atchengdu.web;
import com.atchengdu.service.Userservice;
import org.springframework.context.ApplicationContext;
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 Usersevlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext =req.getServletContext();
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Userservice userservice = context.getBean(Userservice.class);
userservice.user();
}
}
//小编的代码是写死的,,没有对获得spring容器的代码进行封装,而且更改配置文件的时候,listener监听器也要做相应的修改,主要的模仿一下实现的原理,spring提供的监听器可以不做任何修改读多个配置文件,通过封装的工具类拿到相应的容器
|