- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
tl; dr
如何告诉Tomcat 9使用Postgres-specific object factory来生成DataSource
对象以响应JNDI查询?
细节
通过定义一个与上下文相同名称的XML文件,我可以轻松地从DataSource
9中获得一个Apache Tomcat对象。例如,对于名为clepsydra
的网络应用程序,我创建了以下文件:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Domain: DEV, TEST, ACPT, ED, PROD -->
<Environment name = "work.basil.example.deployment-mode"
description = "Signals whether to run this web-app with development, testing, or production settings."
value = "DEV"
type = "java.lang.String"
override = "false"
/>
<Resource
name="jdbc/postgres"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/mydb"
user="myuser"
password="mypasswd"
/>
</Context>
conf
文件夹中,在用引擎名称
Catalina
和主机名
localhost
创建的文件夹中。 Tomcat将设置馈入资源工厂以返回
DataSource
的实例。我可以通过JNDI访问该实例:
Context ctxInitial = new InitialContext();
DataSource dataSource =
( DataSource ) ctxInitial.lookup( "java:comp/env/jdbc/postgres" )
;
postgres
可能是特定于特定应用程序的内容。但是,让我们与
postgres
一起进行相同的演示。
org.postgresql.ds.PGSimpleDataSource
,而不是
org.apache.tomcat.dbcp.dbcp2.BasicDataSource
DataSource
类的基础类是
org.apache.tomcat.dbcp.dbcp2.BasicDataSource
。不幸的是,我不需要该类的
DataSource
。我想要
JDBC driver from The PostgreSQL Global Development Group提供的类的
DataSource
:
org.postgresql.ds.PGSimpleDataSource
。
DataSource
对象使用替代工厂,以代替与Tomcat捆绑在一起的默认工厂实现。听起来像我需要的。
PGObjectFactory
PGObjectFactory
用于简单的JDBC连接。
PGXADataSourceFactory
对于启用
XA的
DataSource
实现,用于分布式事务。
PGDataSourceFactory
中内置了一个类似的工厂。我认为这对Tomcat没有用。
PGObjectFactory
类实现JNDI所需的接口
javax.naming.spi.ObjectFactory
。
spi
意味着对象工厂通过
Java Service Provider Interface (SPI)加载。
META-INF
文件夹中添加了
resources
文件夹,并在其中进一步嵌套了
services
文件夹。因此,在
/resources/META-INF/services
中,我创建了一个名为
javax.naming.spi.ObjectFactory
的文件,其中包含一行文本,即所需对象工厂的名称:
org.postgresql.ds.common.PGObjectFactory
。我什至在Postgres JDBC驱动程序内部检查,以物理方式验证此类的存在和全限定名称。
PGObjectFactory
而不是其默认对象工厂来生成我的
DataSource
对象,以生成与Postgres数据库的连接?
factory
元素上的
<Resource>
属性
factory
元素中添加
factory="org.postgresql.ds.common.PGObjectFactory"
属性(
<Resource>
)一样简单。我从Tomcat页面
The Context Container中得到了这个想法。该页面着重于全局资源,因此非常混乱,但是我不需要或不想全局定义此
DataSource
。我仅对一个Web应用程序需要此
DataSource
。
factory
属性:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Domain: DEV, TEST, ACPT, ED, PROD -->
<Environment name = "work.basil.example.deployment-mode"
description = "Signals whether to run this web-app with development, testing, or production settings."
value = "DEV"
type = "java.lang.String"
override = "false"
/>
<Resource
name="jdbc/postgres"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/mydb"
user="myuser"
password="mypasswd"
factory="org.postgresql.ds.common.PGObjectFactory"
/>
</Context>
DataSource
对象为null失败。
ctxInitial = new InitialContext();
DataSource dataSource = ( DataSource ) ctxInitial.lookup( "java:comp/env/jdbc/postgres" );
System.out.println( "dataSource = " + dataSource );
factory="org.postgresql.ds.common.PGObjectFactory"
属性可解决该异常。但是然后我又回到了Tomcat
BasicDataSource
而不是Postgres
PGSimpleDataSource
的位置。因此,我的问题在这里。
Context
XML已成功加载,因为我可以访问该
Environment
条目的值。
package work.basil.example;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
* The main view contains a button and a click listener.
*/
@Route ( "" )
@PWA ( name = "Project Base for Vaadin", shortName = "Project Base" )
public class MainView extends VerticalLayout
{
public MainView ( )
{
Button button = new Button( "Click me" ,
event -> Notification.show( "Clicked!" ) );
Button lookupButton = new Button( "BASIL - Lookup DataSource" );
lookupButton.addClickListener( ( ClickEvent < Button > buttonClickEvent ) -> {
Notification.show( "BASIL - Starting lookup." );
System.out.println( "BASIL - Starting lookup." );
this.lookupDataSource();
Notification.show( "BASIL - Completed lookup." );
System.out.println( "BASIL - Completed lookup." );
} );
this.add( button );
this.add( lookupButton );
}
private void lookupDataSource ( )
{
Context ctxInitial = null;
try
{
ctxInitial = new InitialContext();
// Environment entry.
String deploymentMode = ( String ) ctxInitial.lookup( "java:comp/env/work.basil.example.deployment-mode" );
Notification.show( "BASIL - deploymentMode: " + deploymentMode );
System.out.println( "BASIL - deploymentMode = " + deploymentMode );
// DataSource resource entry.
DataSource dataSource = ( DataSource ) ctxInitial.lookup( "java:comp/env/jdbc/postgres" );
Notification.show( "BASIL - dataSource: " + dataSource );
System.out.println( "BASIL - dataSource = " + dataSource );
}
catch ( NamingException e )
{
Notification.show( "BASIL - NamingException: " + e );
System.out.println( "BASIL - NamingException: " + e );
e.printStackTrace();
}
}
}
webapps
文件夹。
/lib
文件夹中。我使用BatChmod应用程序来设置Tomcat文件夹的权限。
conf
文件夹中,我创建了
Catalina
和
localhost
文件夹。在其中,我创建了一个名为
datasource-object-factory.xml
的文件,其内容与上述相同。
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Domain: DEV, TEST, ACPT, ED, PROD -->
<Environment name = "work.basil.example.deployment-mode"
description = "Signals whether to run this web-app with development, testing, or production settings."
value = "DEV"
type = "java.lang.String"
override = "false"
/>
<Resource
factory="org.postgresql.ds.common.PGObjectFactory"
name="jdbc/postgres"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/mydb"
user="myuser"
password="mypasswd"
/>
</Context>
datasource-object-factory.war
文件复制到Tomcat中的
webapps
。最后,我运行Tomcat的
/bin/startup.sh
并观察WAR文件爆炸到一个文件夹中。
factory="org.postgresql.ds.common.PGObjectFactory"
元素上具有
Resource
属性时,生成的
DataSource
是
null
。
<Environment>
的值,因此我知道可以找到我的上下文名称XML文件并通过JNDI成功处理。
最佳答案
您的资源配置似乎需要修改。如Tomcat Documentation中所述,
您可以在Web应用程序部署描述符中声明要为JNDI查找和元素返回的资源的特征。您还必须定义所需的资源参数作为Resource元素的属性,以配置要使用的对象工厂(如果Tomcat已经不知道)以及用于配置该对象工厂的属性。
之所以成为null,是因为对象工厂无法确定需要创建的对象类型,请参考PGObjectFactory code
public Object getObjectInstance ( Object obj , Name name , Context nameCtx ,
Hashtable < ?, ? > environment ) throws Exception
{
Reference ref = ( Reference ) obj;
String className = ref.getClassName();
// Old names are here for those who still use them
if (
className.equals( "org.postgresql.ds.PGSimpleDataSource" )
|| className.equals( "org.postgresql.jdbc2.optional.SimpleDataSource" )
|| className.equals( "org.postgresql.jdbc3.Jdbc3SimpleDataSource" )
)
{
return loadSimpleDataSource( ref );
} else if (
className.equals( "org.postgresql.ds.PGConnectionPoolDataSource" )
|| className.equals( "org.postgresql.jdbc2.optional.ConnectionPool" )
|| className.equals( "org.postgresql.jdbc3.Jdbc3ConnectionPool" )
)
{
return loadConnectionPool( ref );
} else if (
className.equals( "org.postgresql.ds.PGPoolingDataSource" )
|| className.equals( "org.postgresql.jdbc2.optional.PoolingDataSource" )
|| className.equals( "org.postgresql.jdbc3.Jdbc3PoolingDataSource" )
)
{
return loadPoolingDataSource( ref );
} else
{
return null;
}
}
private Object loadSimpleDataSource(Reference ref) {
PGSimpleDataSource ds = new PGSimpleDataSource();
return loadBaseDataSource(ds, ref);
}
protected Object loadBaseDataSource(BaseDataSource ds, Reference ref) {
ds.setFromReference(ref);
return ds;
}
loadBaseDataSource
在所有数据源的超类上调用
setFromReference
,请参见:
BaseDataSource
,部分:
public void setFromReference ( Reference ref )
{
databaseName = getReferenceProperty( ref , "databaseName" );
String portNumberString = getReferenceProperty( ref , "portNumber" );
if ( portNumberString != null )
{
String[] ps = portNumberString.split( "," );
int[] ports = new int[ ps.length ];
for ( int i = 0 ; i < ps.length ; i++ )
{
try
{
ports[ i ] = Integer.parseInt( ps[ i ] );
}
catch ( NumberFormatException e )
{
ports[ i ] = 0;
}
}
setPortNumbers( ports );
} else
{
setPortNumbers( null );
}
setServerNames( getReferenceProperty( ref , "serverName" ).split( "," ) );
for ( PGProperty property : PGProperty.values() )
{
setProperty( property , getReferenceProperty( ref , property.getName() ) );
}
}
<Resource
factory="org.postgresql.ds.common.PGObjectFactory"
name="jdbc/postgres"
auth="Application"
type="org.postgresql.ds.PGSimpleDataSource"
serverName="127.0.0.1"
portNumber="5432"
databaseName="mydb"
/>
BaseDataSource
中定义的'userName'和'password'属性。
DataSource
configuration properties defined by the Postgres JDBC driver。
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Domain: DEV, TEST, ACPT, ED, PROD -->
<Environment name = "work.basil.example.deployment-mode"
description = "Signals whether to run this web-app with development, testing, or production settings."
value = "DEV"
type = "java.lang.String"
override = "false"
/>
<!-- `DataSource` object for obtaining database connections to Postgres -->
<Resource
factory="org.postgresql.ds.common.PGObjectFactory"
type="org.postgresql.ds.PGSimpleDataSource"
auth="Container"
driverClassName="org.postgresql.Driver"
name="jdbc/postgres"
serverName="127.0.0.1"
portNumber="5432"
databaseName="myDb"
user="myuser"
password="mypasswd"
ssl="false"
/>
</Context>
关于java - 指定`DataSource`工厂而不是Tomcat的默认工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385528/
我应该执行以下操作: 可能通过服务/工厂,使用 $q(异步)查询 API 以获取大型名称数据集 有另一个服务(也是异步的),它应该只返回上述工厂的元素,如果它们与某个字符串(搜索字段)匹配。目的是缩小
我有一个通用的基类。我有一个实现基类的具体类。 我将如何创建工厂类/方法来交付不同类型的具体类? 举个例子: public class ReceiverBase where T : IInte
我正在查看以下链接中的 Ninject Factory 扩展: http://www.planetgeek.ch/2011/12/31/ninject-extensions-factory-intro
工厂、提供商和服务这三个术语之间有什么区别? 刚刚了解 NHibernate 及其存储库模式(POCO 类等)。 最佳答案 工厂:通过将一堆位组合在一起或基于某种上下文选择类型来组装类 Provide
使用CGLIB我可以做到 final var enhancer = new Enhancer(); enhancer.setUseCache(false); enhancer.setSuperclas
我试图在 Kotlin 中使用伴随对象工厂方法(相当于 Java 中的静态工厂方法)创建一个嵌套内部类。这是我的代码的简化版本。 class OuterClass { var myData:L
我正在为我的大学做一个项目,但遇到了问题。 基本上,该项目由一个客户端-服务器应用程序组成,我想创建一个用于通信的 Packet 类。数据包由 header 和主体组成。现在问题来了。我可以有一些不同
这个问题在这里已经有了答案: Why doesn't polymorphism work without pointers/references? (6 个答案) What is object sl
我正在制作一个套接字工厂。我希望每个外部应用程序都使用 Socket 类的接口(interface),它是几个类(ServerSocketTCP、ClientSocketTCP、ServerSocke
我是 angularjs 的新手,我正在尝试创建一个小型电影数据库。这是我第一次使用工厂,我想确保这是正确的方法,以及如何在另一个功能中使用这个工厂,如下所示? 我希望这个工厂只运行一次,这样我就可以
这个问题在这里已经有了答案: Java inner class and static nested class (28 个答案) 关闭 5 年前。 public class DataFactory
我看过很多关于 C++ 工厂的帖子,但到目前为止我还没有看到解决我的问题的解决方案。 (虽然我可能遗漏了一些东西。) 示例控制台应用程序: #include #include #include
这是一个简单的 C++ 项目,有 2 种设计模式:单例和工厂,sigleton 也是一个模板化类,一个接口(interface) (IHash) 和一个类 (Hash1)。一个简单的工厂类 (Hash
这个问题类似于Factory and generics ,并且可能有相同的答案,但它是不同的。我有一个通用基类,它将由完全独立的 JAR 中的类进行扩展。所述 JAR 应该能够在不更改任何其他代码的情
问题是我需要为传递的类创建一个新实例 有没有办法重写这个函数,让它可以接受任意数量的参数? function createInstance(ofClass, arg1, arg2, arg3, ...
我想用简单的 C++ 语法创建一个简单的工厂方法: void *createObject(const char *str,...) { if(!strcmp("X",str)) retu
经过大约 10 个月的程序化 PHP 学习后,我现在正尝试着手研究基本的 OOP 原则和设计模式。这是一个爱好,我没有那么多时间去追求它,所以请原谅这个问题的水平很低。 我的网站(目前 100% 程序
我有一个简单的问题。 我如何编写一个工厂来定义使用 make() 或 create() 的关系,具体取决于原始调用 make() 还是 create()? 这是我的用例: 我有一个简单的工厂 /**
我正在尝试在延迟加载模块中提供 APP_BASE_HREF 注入(inject) token ,然而,工厂方法根本没有被调用。 在这里https://github.com/MaurizioCascia
我有以下 ast: import { factory as f } from 'typescript' const typeDeclaration = f.createTypeAliasDeclara
我是一名优秀的程序员,十分优秀!