Spring Getting Started 11 - DataSource Injection

xiaoxiao2021-03-06  48

Get start 11 - DataSource Injection

For different data inventory requirements, we use JDBC to solve this problem, Spring provides DataSource Injection for Digital Connection Source requirements, and Spring provides DataSource Injection, as long as you modify the configuration in the bean definition file without modifying any line.

In response to different systems, applications may use different sources of sources, but,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Business logic, for this purpose, you can keep a data source injection on the bean that needs to get the source of the connection, allowing the dependent data source to be injected by the interface. For example, let's write a simple bean:

DataBean.java

Package online.

Import javax.sql.datasource;

Import java.sql.connection;

Public class dataBean {

PRIVATE DATASOURCE DATASOURCE;

Public void setDataSource (DataSource Datasource) {

THIS.DATASOURCE = DataSource;

}

Public void testdataserce () {

Try {

Connection connection = DataSource.getConnection ();

IF (Connection! = NULL)

System.out.println ("Test OK!");

}

Catch (Exception E) {

E.PrintStackTrace ();

}

}

}

This is a simple test of Spring DataSource Injection. We use the Javax.Sql.DataSource interface to inject data source, Spring provides org.springframework.jdbc.datasource.driverManagerDataSource to get DataSource, which has javax.sql. DataSource, you will be as a bean, then inject into the DataBean, bean defined files can be written:

com.mysql.jdbc.driver

JDBC: MySQL: // localhost: 3306 / testdb

Caterpillar

123456

If you only use Spring-Core.jar before you only join Spring-Dao.jar, org.springframework.jdbc.datasource.driverManagerDataSource is included in this category library, if you are using Spring.jar, There is already included, there is no need to add any JAR, of course, in order to use JDBC, you must have JAR files for JDBC drivers. You can use the following program simple to test:

Beandefinitionregistry reg = new defaultListableBeanFactory ();

XmlbeandefinitionReader Reader = New XmlbeandefinitionReader (REG);

Reader.LoadBeandefinitions (New ClassPathResource ("Bean.xml") ;;

Beanfactory bf = (beanfactory) REG;

DataBean DataBean = (DATABEAN) BF.GetBean ("DATABEAN");

DATABEAN.TESTDATASOURCE ();

DriverManagerDataSource does not provide a connection to the connection pool, can only make a simple stand-alone connection test, now assume that the connection test is no problem, you want to change the DBCP to get the function of the connection pool, the original program does not have to be more, as long as the change is changed Bean defines the file:

com.mysql.jdbc.driver

JDBC: MySQL: // localhost: 3306 / testdb

Caterpillar

123456

Now we use org.apache.commons.dbcp.basicDataSource as an injected DataSource source, in order to use DBCP features, you must add Commons-Dbcp.jar to classpath, and you also need Commons-pool.jar and Commons. -collections.jar, these can be found in the lib directory in Spring dependent version. Note that we declared Destroy-Method on the DataSource Bean, so that BeanFactory also turned off the BasicDataSource when it was off. If you want to use JDBC directly to perform data storage, use org.springframework.jdbc.datasource.DataSourceUtils to get Connection is a better way:

Connection conn = DataSourceUtils.getConnection (DataSource);

The advantage of this is that all SQLException is packaged by Spring's DataAccessException subclass CannotgetjdbcConnectionException. You can get more information and ensure the transferred value of the storage layer. When you turn off the Connection, you can use the following way:

DataSourceUtils.closeConnectionIfnecessry (Connection, DataSource);

If your servlet container provides JNDI data source, you can also simply replace this data source:

JDBC / TESTDB

In order to use org.springframework.indi.jndiObjectFactoryBean, you must join Spring-Context.jar This category library. JNDINAME actually wants to find the JNDI settings in Tomcat according to the JNDI query name you set. Mode: http://www.caterpillar.onlyfun.net/phpbb2/viewtopic.php?t=1354

转载请注明原文地址:https://www.9cbs.com/read-60913.html

New Post(0)