4.ServletContextListener接口:
1)作用:通过这个接口合法的检测全局作用域对象被初始化时刻以及被销毁时刻
2)监听事件处理方法:
public void contextInitlized() :在全局作用域对象被Http服务器初始化被调用
public void contextDestory(): 在全局作用域对象被Http服务器销毁时候触发调用
public class ContextLoaderListener implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent sce)
{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//将app存入ServletContext域中
ServletContext servletContext = sce.getServletContext();
servletContext.setAttribute("app",app);
System.out.println("spring容器创建完毕...");
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
}
}
public class indexController extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
ServletContext servletContext = req.getServletContext();
ApplicationContext app = (ClassPathXmlApplicationContext) servletContext.getAttribute("app");
UserService userService = (UserServiceImpl) app.getBean("UserService");
userService.save();
}
}
这里applicationContext.xml已经耦合了,可以优化下
web.xml
<!--配置全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
ServletContextListener:
public class ContextLoaderListener implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent sce)
{
ServletContext servletContext = sce.getServletContext();
//读取web.xml的全局参数
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
//创建容器
ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
//将app存入ServletContext域中
servletContext.setAttribute("app",app);
System.out.println("spring容器创建完毕...");
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
}
}
1.导入spring-web坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
2.web.xml中配置spring的自带的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.配置全局初始化参数
<!--配置全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
servlet:
public class indexController extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
ServletContext servletContext = req.getServletContext();
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);//获取容器
UserService userService = (UserServiceImpl) app.getBean("UserService");
userService.save();
}
}
报错:Could not open ServletContext resource [jdbc.properties]
因为资源文件默认在webApp目录下面,在applicationContext.xml需要加入
location="classpath:jdbc.properties"
<bean id="UserDao" class="Dao.impl.UserDaoImpl"></bean>
<bean id="UserService" class="Service.impl.UserServiceImpl">
<property name="userDao" ref="UserDao"></property>
</bean>
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>