The first 4 chapters of spring in action 3rd explain the core of spring, which is DI/IOC and AOP. Starting from the 5th chapter is the application of spring in all aspects of enterprise development. In fact, as the author of the enterprise computing, the most common things related to the Java EE is how to persist, 5th, 6 is the content of this aspect.
Today, we mainly studied spring and JDBC development.
I. Configuring the data source
First we need to configure the data source, after we set up the context.xml, we configure the JDBC data source to spring:
<jee:jndi-lookup id= "DataSource" jndi-name= "/jdbc/spitter" resource-ref= "true"/> Yes, that's a word. And this data source with ID DataSource can be injected into another bean like a bean.
For example, into the dstestbean below.
Packagecom.spitter.test;ImportJavax.sql.DataSource;Importorg.springframework.beans.factory.annotation.Autowired; Public classDstestbean {@AutowiredPrivateDataSource DataSource; Public voidSetdatasource (DataSource DataSource) { This. DataSource =DataSource; } PublicDataSource Getdatasource () {return This. DataSource; }}
The corresponding spring configuration file is as follows
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:jee= "Http://www.springframework.org/schema/jee"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spri Ng-aop-3.0.xsd Http://www.springframework.org/schema/context HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/CONTEXT/SPR Ing-context-3.0.xsd Http://www.springframework.org/schema/jee Http://www.springframework.org/schema/jee/sprin G-jee-3.0.xsd "><!--Beans Declarariona Go here -<Context:annotation-config/><Jee:jndi-lookupID= "DataSource"Jndi-name= "/jdbc/spitter"Resource-ref= "true" /><BeanID= "Dstest"class= "Com.spitter.test.DSTestBean" /></Beans>
Note that since we used annotation injection for the DataSource attribute in Dstestbean @autowired
So in the configuration file to add <context:annotation-config/>, because spring by default is to turn off the annotation assembly mode.
Ii. simplifying the JDBC code using the spring template
Spring Development First Step (iii) Spring and JDBC