當前我們有一個應用程序通過在Tomcat context.xml文件中使用數據源來配置。因此,我們可以通過檢索JNDI名稱並獲得連接來成功獲得連接。我想知道如果我們可以通過使用Spring數據源來替換它,並且我們仍然需要context.xml文件中的信息嗎?如果我創建一個Spring數據源,我還需要在Tomcat context.xml中定義數據源嗎?
例「的context.xml」:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/myDataSource"
auth="Container"
type="javax.sql.DataSource"
username="john"
password="doe"
driverClassName="<removed>"
url="<removed>"
maxActive="30"
maxIdle="10"
maxWait="1000"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"/>
</Context>
因此,在我們的代碼中尋找這樣的JNDI上下文:
Context envCtx = (Context) initCtx.lookup(..);
DataSource ds = (DataSource) envCtx.lookup(..);
Connection connection = ds.getConnection();
我在想,如果我們能更好地定義一個Spring數據源,而不是使用這種方法以及我們如何做到這一點?
因此,如果我們定義一個像這樣的bean,我們可以跳過context.xml文件?我現在添加了一些示例代碼。 – Marco 2011-05-09 19:48:54
現在我看到了你的編輯。你可以使用'context.xml'文件,但是像我的第一個例子那樣利用Spring並且通過JNDI查找'dataSource' **或者**使用第二個例子並且完全跳過'context.xml'和JNDI。 – 2011-05-09 19:53:31