8.spring获取上下文对象applicationContext

 


        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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值