Learn Java for Web Development

(Tina Meador) #1

226 CHAPTER 5: Building Java Web Applications with Spring Web MVC

















































  1. <bean id="dataSource"

  2. class="org.springframework.jdbc.datasource.DriverManagerDataSource">




















   Line 20: Configures dao with the data source

In this way, the Spring Framework eliminates the boilerplate code. Now with a stand-alone Java
application you can query the new data access layer that we built using the Spring Framework.
Listing 5-30 illustrates the stand-alone Java application that queries the data access through the
service-layer component BookService.


Listing 5-30. Stand-Alone Application


package com.apress.books.client;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.apress.books.model.Book;
import com.apress.books.service.BookService;


public class BookApp {


public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
BookService bookService = (BookService)context.getBean("service");
// List all books
System.err.println("Listing all Books:");
List bookList= bookService.getAllBooks();

Free download pdf