一、背景
使用tomcat 7
参考链接:https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
jndi:Java 命名与目录接口(Java Naming and Directory Interface)。
数据源:derby
容器:servlet 容器
使用监听器 进行初始化
二、好处
- 使用jndi 直接在 j2ee容器中配置数据源,程序员无需在代码层面关注 数据源的配置。只需要在对应的配置按照 模板(上参考链接)配置接口。
- 通俗讲就是根据命名目录进行依赖查找,进行对应的初始化配置。
三、应用
1.pom文件
<dependencies>
<dependency>
<groupId>org.geekbang.projects</groupId>
<artifactId>my-web-mvc</artifactId>
<version>${revision}</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Tomcat Maven 插件用于构建可执行 war -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<!-- <dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>${derby.version}</version>
</dependency>
</dependencies>-->
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<!-- 最终打包成可执行的jar包 -->
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<!-- ServletContext 路径 -->
<path>/</path>
<enableNaming>true</enableNaming>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.创建监听器
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@WebListener
public class DBConnectionInitializerListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
getConnection();
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
protected Connection getConnection() {
Context initCtx;
Context envCtx;
DataSource ds;
Connection conn = null;
try {
initCtx = new InitialContext();
/**
将InitialContext其配置为最初部署的Web应用程序,并可供Web应用程序组件使用(用于只读访问)。所有已配置的条目和资源都放置在java:comp/envJNDI名称空间的一部分中,
**/
envCtx = (Context) initCtx.lookup("java:comp/env");
//依赖查找
//jdbc/UserPlatformDB 资源名称。需要在随后的文件中 配置,在此进行引用。
ds = (DataSource)
envCtx.lookup("jdbc/UserPlatformDB");
conn = ds.getConnection();
System.out.println("创建连接成功" + conn.toString());
} catch (NamingException | SQLException e) {
e.printStackTrace();
}
return conn;
}
}
3.声明您的资源需求.接下来,修改您的Web应用程序部署描述符(/WEB-INF/web.xml
),以声明JNDI名称,在该名称下您将请求该bean的新实例。最简单的方法是使用<resource-env-ref>
元素
<!--
建议:监听器放在最上面。servlet 其次,过滤器最后-->
<listener>
<listener-class>监听器 全路径</listener-class>
</listener>
<!--
参考链接:https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
声明您的资源需求-->
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/UserPlatformDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4.配置Tomcat的资源工厂.要配置Tomcat的资源工厂,请<Context>
在此Web应用程序的元素中添加类似这样的 元素。
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- 实际进行配置的 数据链接 -->
<Resource name="jdbc/UserPlatformDB"
type="javax.sql.DataSource" auth="Container"
description="Derby database for User Platform"
maxActive="100" maxIdle="30" maxWait="10000"
username="" password=""
driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
url="jdbc:derby:db/user-platform;create=true"/>
</Context>
注意此文件的路径:在 src/main/webapp/META-INF/context.xml,
5.最后编译打包,使用 tomcat 进行启动