javax.naming.NameNotFoundException Name java:comp is not bound in this Context之解决

Spring mvc项目调试的时候,常用的两种方法是:

一、使用tomcat插件

(1)、下载tomcat插件,http://www.eclipsetotale.com/tomcatPlugin.html,然后解压缩后放在F:\eclipse-jee-juno-SR1-win32\eclipse\dropins下面即可

(2)、重启eclipse,就会看到上面的工具栏有3个tomcat的图标了

(3)、然后需要配置windowsàpreferencesàtomcat  路径指向tomcat的安装目录

(4)、最后tomcat/conf/server.xml加上当前项目生成的war,比如:

 <Context path="/figo" docBase="D:\workspace\pos\codes\figo-server\figo-webapp\target\figo"reloadable="true" />

(5)、点击eclipse上的tomcat图标,就可以启动我们的项目了,打开浏览器,java代码里加上断点,就可以访问和调试程序了。

比如:http://localhost:8080/figo/version/toInsertVersion.htm就可以调试当前这个action了。

这种方法的缺点就是,页面上改个文字也要maven install –Dmaven.test.skip=true重新生成war,才能看到效果,个人建议不要使用这种方式。

二、使用eclipse自带的Debug on Server

个人比较推荐使用这种方式,开发效率比较高,不用向上面那样,每次都要整个项目重新生成一下,这种方式,改了之后直接Debug on Server就可以看到效果了。之所以写这篇文章,是因为今天使用这种方式,项目里面使用了jndi配置数据源的时候出现了。

javax.naming.NameNotFoundException: Name java:compis not bound in this Context

 

这个异常,折腾了一个下午,终于在同事的帮助下解决,这里分享出来,希望能解大家的燃眉之急。

虽然我们一般把jndi配置在tomcat,conf/context.xml文件上的,比如:

<Resource name="figoDataSource"

                   auth="Container"

                  type="javax.sql.DataSource"

                  driverClassName="oracle.jdbc.driver.OracleDriver"

                  url="jdbc:oracle:thin:@10.136.123.123:1521:dsdb1"

                   username="tester"

                   password=" tester "

                   maxActive="5"

                   maxIdle="1"

                  maxWait="10000" />

但是使用Debug on server之后,就找不到了,也许是使用这种方式之后,spring框架找不到配置吧。

 

解决办法:

1、 把jndi的数据源配置写入一个xml配置文件jndi-context.xml,

 <?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:task="http://www.springframework.org/schema/task"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:jaxws="http://cxf.apache.org/jaxws"

    xmlns="http://www.springframework.org/schema/beans"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

               http://www.springframework.org/schema/tx

         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

               http://www.springframework.org/schema/context

               http://www.springframework.org/schema/context/spring-context-3.0.xsd

                  http://www.springframework.org/schema/task

                  http://www.springframework.org/schema/task/spring-task-3.0.xsd

                  http://www.springframework.org/schema/aop

                  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

                  http://cxf.apache.org/core

                  http://cxf.apache.org/schemas/core.xsd

                  http://cxf.apache.org/jaxws

                  http://cxf.apache.org/schemas/jaxws.xsd">

                 

 

    <beanid="jndiContext"class="com.figo.commons.springtools.base.JndiContextMock"init-method="init">

       <propertyname="contextMap">

           <map>

              <entry key="java:comp/env/figoDataSource"><!—和数据源配置文件figo-datasource.xml里面名称要保持一致-->

                  <bean class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

                      <property name="driverClassName"value="oracle.jdbc.driver.OracleDriver"/>

                      <property name="url"value="jdbc:oracle:thin:@10.136.123.123:1521:dsdb1"/>

                      <property name="username"value="tester"/>

                      <property name="password"value="tester "/>

                      <property name="initialSize"value="5"/>

                      <property name="maxActive"value="5"/>

                      <property name="maxIdle"value="2"/>

                      <property name="maxWait"value="5000"/>

                  </bean>

              </entry>

             

           </map>

       </property>

    </bean>

</beans>

 

注意:

applicationContext.xml里面要加上

<importresource="classpath:META-INF/spring/jndi-context.xml" />

 

 

2、 加载类com.figo.commons.springtools.base.JndiContextMock.java

package  com.figo.commons.springtools.base;

import java.util.Map;

import java.util.Map.Entry;

import javax.naming.NamingException;

importorg.springframework.mock.jndi.SimpleNamingContextBuilder;

 

public class JndiContextMock

{

 private Map<String, Object> contextMap;

 

 public void init()

 {

   SimpleNamingContextBuilder builder;

   try

   {

     builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();

   } catch (NamingException e) {

     throw new Exception("初始化JNDI上下文环境时发生异常:"+e);

   }

 

   for (Map.Entry contextEntry : this.contextMap.entrySet())

     builder.bind((String)contextEntry.getKey(), contextEntry.getValue());

 }

 

 public void setContextMap(Map<String, Object> contextMap)

 {

   this.contextMap = contextMap;

 }

}

 

3、 数据源配置figo-datasource.xml

 <?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:osgi="http://www.springframework.org/schema/osgi"

    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/osgihttp://www.springframework.org/schema/osgi/spring-osgi.xsd"

    default-autowire="byName">

   

      

    <!-- figoControl(system forfigo)-->

    <!-- data source -->

    <beanid="figoDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

       <propertyname="jndiName">

           <value>java:comp/env/figoDataSource</value>

       </property>

    </bean>

   

    <beanid="figoSqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

        <propertyname="configLocation">

            <value>classpath:META-INF/spring/sqlmap-figo.xml</value>

        </property>

    </bean>

   

    <!-- DAO -->

    <beanid="figoBaseSqlMapClientDAO"abstract="true">

       <propertyname="sqlMapClient"ref="figoSqlMapClient"></property>

       <propertyname="dataSource"ref="figoDataSource"/>

    </bean>

   

    <!--transcation -->

    <beanid="figoTransactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <propertyname="dataSource"ref="figoDataSource"/>

    </bean>

    <beanname="figoTransactionTemplate"class="org.springframework.transaction.support.TransactionTemplate">

       <propertyname="transactionManager"ref="figoTransactionManager"/>

    </bean>

</beans>

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值