方法一:实现ServletContextListener
1 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" 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_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>com.ithings.listensers.SpringServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.ithings.servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
2 初始化IOC容器
ServletContextListener 的 contextInitialized方法中初始化
public class SpringServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
//获取Spring配置文件的地址
ServletContext servletContext = sce.getServletContext();
String config = servletContext.getInitParameter("contextConfigLocation");
//创建IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config);
//将IOC容器放入ServletContext的一个属性中
servletContext.setAttribute("ApplicationContext", applicationContext);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
3 Servlet中使用IOC容器获取bean
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//从application中获取IOC容器的引用
ServletContext servletContext = getServletContext();
ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("ApplicationContext");
//从IOC容器中得到需要的bean
Employees employees = (Employees) applicationContext.getBean("employees");
System.out.println(employees.getUid());
System.out.println(employees.getUname());
}
}
方法二:实现ClassLoaderListener
1 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" 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_2_5.xsd">
<!-- 配置spring配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动IOC容器的ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.ithings.servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
2 初始化IOC容器
ContextLoaderListener类代做
3 Servlet中使用IOC容器获取bean
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//从application域中获取IOC容器的实例
ServletContext servletContext = getServletContext();
ApplicationContext applicationContext = (ApplicationContext)WebApplicationContextUtils.getWebApplicationContext(servletContext);
//从IOC容器中得到需要的bean
Employees employees = (Employees) applicationContext.getBean("employees");
System.out.println(employees.getUid());
System.out.println(employees.getUname());
}
}
------------------------- 创建SpringContextUtil类获取bean------------------------------------------
1.实现ApplicationContextAware
public class SpringContextUtil implements ApplicationContextAware {
protected static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
applicationContext = ac;
}
public static Object getBean(Class<?> clazz){
return applicationContext.getBean(clazz);
}
public static Object getBean(String beanName){
return applicationContext.getBean(beanName);
}
}
2.applicationContext.xml中注册这个bean
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.0.RELEASE.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.0.RELEASE.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.0.RELEASE.xsd
">
<!--自动扫描--> <pre name="code" class="html"> <context:component-scan base-package="com.ithings.beans"></context:component-scan>
<!--注册SpringContextUtil--> <bean class="com.ithings.utils.SpringContextUtil"/></span></beans>
3.Servlet中的代码改写为
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//从IOC容器中得到需要的bean
Employees employees = (Employees) SpringContextUtil.getBean("employees");
System.out.println(employees.getUid());
System.out.println(employees.getUname());
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>contextConfigLocationValue></param-value>
</context-param>
作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。
param-name 设定上下文的参数名称。必须是唯一名称
param-value 设定的参数名称的值
- 初始化过程:
- 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。
- 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
- 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
- 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
- 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
- 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。