本文主要是介绍Spring获得DataSource,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、从JNDI获得DataSource
1.1、Spring JNDI数据源配置信息
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"><property name="jndiName"><value>java:comp/env/jcptDataSourceJNDI</value></property>
</bean>
jcptDataSourceJNDI是tomcat或者其他应用服务器配置的JNDI
1.2、关于JNDI的配置(Tomcat)
修改tomcat目录conf/context.xml文件:
<Resource name="jcptDataSourceJNDI" auth="Container"type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10" username="tysp" password="12345678"driverClassName="oracle.jdbc.driver.OracleDriver"url="jdbc:oracle:thin:@192.168.1.35:1521:orcl" />
1.3、通过 JNDI 获取 DataSource
Context context = new InitialContext();
DataSource ds =(DataSource)context.lookup("java:comp/env/jcptDataSourceJNDI");
二、从第三方的连接池获得DataSource
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property><property name="url" value="jdbc:oracle:thin:@192.168.1.35:1521:orcl"></property><property name="username" value="or_meal"></property><property name="password" value="or_meal"></property><property name="maxActive" value="100"></property><property name="maxIdle" value="30"></property><property name="maxWait" value="10"></property><property name="defaultAutoCommit" value="false"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop><prop key="show_sql">true</prop><prop key="format_sql">true</prop></props></property><property name="mappingResources"><list></list></property>
</bean>
三、使用DriverManagerDataSource获得DataSource
这篇关于Spring获得DataSource的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!