Java Reference
In-Depth Information
12. <!-- telling container to take care of annotations stuff -->
13. <context:annotation-config />
14.
15. <!-- declaring base package -->
16. <context:component-scan base-package="com.apress.books" />
17.
18.
19. <bean id="dao" class="com.apress.books.dao.BookDAOImpl" >
20. <property name="dataSource" ref="dataSource">
21. </property>
22. </bean>
23.
24. <bean id="service" class="com.apress.books.service.BookServiceImpl">
25. <property name="bookDao" ref="dao">
26. </property>
27. </bean>
28.
29. <bean id="dataSource"
30. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
31. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
32. <property name="url" value="jdbc:mysql://localhost:3306/books" />
33. <property name="username" value="root" />
34. <property name="password" value="password" />
35. </bean>
36. </beans>
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<Book> bookList= bookService.getAllBooks();
 
Search WWH ::




Custom Search