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

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条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格