如何在Spring中使用WebLogic提供的JNDI数据源?

6
据Spring文档中关于DriverManagerDataSource类的说法,该类非常简单,并推荐使用容器提供的JNDI DataSource。可以通过JndiObjectFactoryBean将此类DataSource公开为Spring ApplicationContext中的bean。
问题是:如何实现呢?
例如,如果我想要一个DataSource bean来访问我的自定义Oracle数据库,我需要什么?在上下文配置中应该写什么?
4个回答

4
要访问JNDI数据源,您需要执行以下操作:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/MyDatabase"/>
</bean>

或者查看Spring的“jee”模式。

数据库连接的详细信息在WebLogic中配置,应用程序通过jndi名称访问数据库。


3
或者使用基于Java的配置,并像这样完成它:
@Bean(destroyMethod = "")
public DataSource dataSource() throws NamingException{
    Context context = new InitialContext();
    return (DataSource)context.lookup("jdbc.mydatasource");
}

1

还有另一个选项:

<jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/MyLocalDB"
        expected-type="javax.sql.DataSource" />

0

您可以使用以下JNDI配置。

<beans:bean id="weblogicDataSource" class="org.springframework.remoting.rmi.JndiRmiProxyFactoryBean">
    <beans:property name="jndiName" value="ConnectionPoolJNDINameAsConfigured"></beans:property>
    <beans:property name="jndiEnvironment">
        <beans:props>
            <beans:prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</beans:prop>
            <beans:prop key="java.naming.provider.url">iiop://localhost:7001</beans:prop>
        </beans:props>
    </beans:property>
    <beans:property name="serviceInterface" value="javax.sql.DataSource"></beans:property>
</beans:bean>

你可以将注入的类文件引用为:

<beans:bean id="xxxx" class="xxxxxxxx">
    <beans:property name="wlDataSource"  ref="weblogicDataSource" />
</beans:bean>

在你的实现类中使用:

import javax.sql.DataSource;

创建一个实例作为 private DataSource wlDataSource;
并相应地设置setter。现在,您可以根据自己的实现思路自由使用JDBCTemplate或SimpleJDBCCall等。
希望这能帮到您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接