Link With DataSource : DataSource « Spring « Java Tutorial






File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
   <bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">

   <property name="driverClassName">
       <value>sun.jdbc.odbc.JdbcOdbcDriver</value>
   </property>

   <property name="url">
       <value>jdbc:odbc:test</value>
   </property>

   <property name="username">
       <value>root</value>
   </property>

   <property name="password">
       <value>sql</value>
   </property>

   </bean>

   <bean id="datacon" class="Dataconimpl">
      <property name="dataSource"><ref local="dataSource"/></property>
   </bean>
</beans>

File: Main.java

import javax.sql.DataSource;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {

  public static void main(String[] args) throws Exception {
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    Dataconimpl bean1 = (Dataconimpl) factory.getBean("datacon");

  }
}

class Dataconimpl implements Datacon {
  private DataSource dataSource;

  public void setDataSource(DataSource ds) {
    dataSource = ds;
  }

  public DataSource dbcon() {
    return dataSource;
  }
}

interface Datacon {
  public DataSource dbcon();
}
  Download:  Spring-LinkWithDataSource.zip( 2,893 k)








28.18.DataSource
28.18.1.Configure Oracle DataSource
28.18.2.Set up DataSource for MySQL
28.18.3.DataSource for HSQL
28.18.4.JdbcOdbc DataSource
28.18.5.Load Two DataSources
28.18.6.Link With DataSource
28.18.7.Spring lazy init