`
changhongbao
  • 浏览: 117227 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

jetty9.1.4配置jndi数据源的方法

阅读更多

jetty9.1.4配置jndi数据源的方法

摘要: 因为以前没怎么用jetty服务器,这段时间才开始使用,对于在jetty9.x 最新版本上配置jndi方面的资料也比较难找,还好东拼西凑,总算解决了。

    环境:j操作系统win7、jetty9.1.4 、jdk1.7、项目是spring mvc、数据库是mysql。 我使用的(前提是在安装好了jetty9.x服务器的基础上)配置步骤:
第一:在你的项目的WEB-INF/目录下新建一个jetty-env.xml 文件,这样方便项目移植。内容如下:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <!--mysql数据源 ,我这里用的是mysql数据库-->
     <New id="myDB" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg>jdbc/MYDB</Arg>
     <Arg>
        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
           <Set name="Url">jdbc:mysql://localhost:3306/mydatabasename</Set>
           <Set name="User">root</Set>
           <Set name="Password">root</Set>
        </New>
     </Arg>
    </New>
   </Configure>

第二,修改web.xml配置文件,对数据源的引用,如下:

<resource-ref>
    <description>MY DB Connection</description>
    <res-ref-name>jdbc/MYDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>


第三,在applicationContext.xml文件中使用数据源,配置如下(标了颜色的部分):

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee
    http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    ">
    
    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/MYDB" />
    <bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate"
          p:dataSource-ref="dataSource"/>

    ......

    ...

 
    java -jar start.jar 运行就ok了。

  

    靠,开始不会弄的时候怎么也找不到资料,会了后就发现很多资料可以用了....悲催啊,给大家推荐一个比较全面配置jndi的文章:http://www.javaarch.net/jiagoushi/896.htm

          https://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics