applicationContext.xml里面设置
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="default" autowire="default" dependency-check="default">
<property name="jndiName">
<value>JDBC/TEST</value>
</property>
</bean> 或者
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ora"></property>
<property name="username" value="test"></property>
<property name="password" value="123456"></property>
</bean>
通过ServiceLocator.getBean('beanId') 获得"dataSource",ServiceLocator 其实就是自己封装一下,
ApplicationContext ctx;
ctx.getBean("");
通过DataSourceUtils.getConnection(DataSource);就能获取到设置的DataSource 然后获得connection
public static Connection getConnection()
throws SQLException
{
return DataSourceUtils.getConnection((DataSource)ServiceLocator.getBean("dataSource"));
}
posted on 2008-03-05 12:45
Pitey 阅读(134)
评论(0) 编辑 收藏