zhangyanfengzi 2012-10-15 09:23 采纳率: 100%
浏览 2632
已采纳

ContextLoader.getCurrentWebApplicationContext();返回null怎么回事

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    <!-- 数据源 -->
    <bean id="NNMiDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@10.27.125.61:1521:reporter"></property>
        <property name="username" value="openview"></property>
        <property name="password" value="openview"></property>
    </bean>
    
    <!-- 数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@//147.16.17.98:1521/hpsm"></property>
        <property name="username" value="hndsPilot"></property>
        <property name="password" value="hndsPilot"></property>
    </bean>

    <!-- 网络监控(NNMi) 性能数据 -->
    <bean id="sybaseDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.sybase.jdbc4.jdbc.SybDriver"></property>
        <property name="url" value="jdbc:sybase:Tds:10.27.124.221:9303/hpnnmi_perfspi"></property>
        <property name="username" value="DBA"></property>
        <property name="password" value="HP_IQ"></property>
    </bean>
</beans>
  // 获取spring配置文件地址
        ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
  
        // 获取数据源
        try {
            ds = (DataSource) ctx.getBean("dataSource");
            
            sybaseDs = (DataSource) ctx.getBean("sybaseDS");
            my= (DataSource) ctx.getBean("NNMiDS");
            
        } catch (Exception e) {
            
            e.printStackTrace();
        }

 异常

  这行报null怎么回事,我灰常不理解.请大家帮忙指点给出方案来,谢谢.

  • 写回答

2条回答 默认 最新

  • jinnianshilongnian 2012-10-15 13:23
    关注

    你什么使用调用的
    ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();

    此处需要注意 一定要ContextLoaderListener 才行 即只有它才能通过 ContextLoader.getCurrentWebApplicationContext(); 获取

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch