通过tomcat javadocs后,我已经达到了我的要求。下面给出详细信息:
1.将数据源添加为 GlobalNamingResource
为此,创建一个服务器生命周期监听器,创建一个数据源并将其添加到 JNDI GlobalNamingContext。
public class GlobalDatasourceCreator implements LifecycleListener {
@Override
public void lifecycleEvent(LifecycleEvent event) {
if (event.getSource() instanceof Server) {
Context namingContext = ((Server) event.getSource()).getGlobalNamingContext();
if (Lifecycle.START_EVENT.equals(event.getType())) {
bindDatasources(namingContext);
} else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
unbindDatasources(namingContext);
}
}
}
private void bindDatasources(Context namingContext) {
if (createSubContext(namingContext)) {
try {
DataSource ds = getDatasource(); //TODO: Implement it
namingContext.rebind("jdbc/myds_global", ds);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private boolean createSubContext(Context namingContext) {
try {
namingContext.createSubcontext("jdbc");
} catch (NameAlreadyBoundException e) {
} catch (NamingException e) {
return false;
}
return true;
}
private void unbindDatasources(Context namingContext) {
try {
namingContext.unbind("jdbc/myds_global");
} catch (NamingException e) {
e.printStackTrace();
}
}
然后将这个类添加到conf/server.xml作为监听器
<Listener className="com.test.GlobalDatasourceCreator" />
2。通过 ResourceLink 向所有 Web 应用程序公开数据源
创建一个上下文 LifecycleListener。在 START 事件上,创建一个 ResourceLink 并将其附加到上下文。
注意:由于这是上下文级别的侦听器,因此将为所有应用程序创建 ResourceLink。我的要求是将它公开给所有应用程序,因为它是一个受控环境。如果仅需要为选定的应用程序创建 ResourceLink,则可以应用基于上下文名称的过滤。
public class AppDatasourceLinkCreator implements LifecycleListener {
@Override
public void lifecycleEvent(LifecycleEvent event) {
if (event.getSource() instanceof Context) {
Context ctx = (Context) event.getSource();
if (Lifecycle.START_EVENT.equals(event.getType())) {
addResourceLink(ctx);
} else if (Lifecycle.STOP_EVENT.equals(event.getType())) {
removeResourceLink(ctx);
}
}
}
private void removeResourceLink(Context ctx) {
ctx.getNamingResources().removeResourceLink("jdbc/myds");
}
private void addResourceLink(Context ctx) {
ContextResourceLink resourceLink = new ContextResourceLink();
resourceLink.setGlobal("jdbc/myds_global");
resourceLink.setName("jdbc/myds");
resourceLink.setType("javax.sql.DataSource");
ctx.getNamingResources().addResourceLink(resourceLink);
}
}
然后将这个类添加到conf/context.xml作为监听器
<Listener className="com.test.AppDatasourceLinkCreator" />
创建一个包含这两个类的 jar 并将其放在 /lib 文件夹中。
优势:无需进一步修改 xml 即可添加任意数量的数据源。只需修改java代码添加新的数据源,更新lib文件夹中的jar并重新启动服务器,完全符合我的项目要求。这也解决了在 xml 中以纯文本形式公开数据源凭据的问题(尽管不是 100% 风险证明)。