我正嘗試創建maven web應用程序項目,該項目在打包時爲此目的創建了獨立的可執行文件jar,併爲此目的使用了tomcat7-maven-plugin。最初的Hello World應用程序工作正常,但我努力使它與數據庫一起工作。 這裏是我的pom.xml的樣子:NamingException:無法使用tomcat7-maven-plugin h2/mysql數據庫創建資源實例
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<contextFile>src/main/resources/tomcat/context.xml</contextFile>
<path>/helloworld</path>
<enableNaming>true</enableNaming>
<finalName>standalone.jar</finalName>
<charset>utf-8</charset>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.183</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
的src /主/ web應用/ WEB-INF/web.xml文件:
<web-app>
...
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.mycompany.test.TestServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<resource-ref>
<description>H2 DS</description>
<res-ref-name>jdbc/H2DB</res-ref-name>
<res-type>org.apache.tomcat.jdbc.pool.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>Mysql DS</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>org.apache.tomcat.jdbc.pool.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
...
</web-app>
的src /主/資源/ tomcat的/context.xml
<Context path="/helloworld">
<Resource type="org.apache.tomcat.jdbc.pool.DataSource"
name="jdbc/H2DB"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="org.h2.Driver"
url="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"
username="testuser"
password="testpassword"
initialSize="5"
maxActive="10"
maxIdle="5"
minIdle="2"
/>
<Resource name="jdbc/TestDB" auth="Container" type="org.apache.tomcat.jdbc.pool.DataSource"
url="jdbc:mysql://localhost:3306/testdb"
username="testuser"
password="testpassword"
driverClassName="com.mysql.jdbc.Driver"
initialSize="5"
maxActive="10"
maxIdle="5"
minIdle="2"
/>
</Context>
,並在我的Java代碼我想:
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/TestDB");
或Java:/ comp/env的/ JDBC/H2DB爲H2數據庫 這兩種方式我得到相同的異常當Tomcat啓動我的servlet:
重度:Servlet的/的HelloWorld扔負荷()異常 javax.naming.NamingException:無法創建資源實例 at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:146) at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source) 在org.apache.naming.NamingContext.lookup(NamingContext.java:843) at org .apache.naming.NamingContext.lookup(NamingContext.java:154) at org.apache.naming.NamingContext.lookup(NamingContext.java:831) at org.apache.naming.NamingContext.lookup(NamingContext.java:154 ) 在org.apache.naming.NamingContext.lookup(NamingContext.java:831) ...
我將是任何想法感謝。 在此先感謝。