Spring Development First Step (iii) Spring and JDBC

Source: Internet
Author: User
Tags aop

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.