Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
thông báo rằng mùa xuân 2.0 vẫn hoàn toàn tương thích ngược với Spring 1.2. Hơn nữa, Spring 2.0 làm việc với Java 1.3 và xa hơn nữa và với J2EE 1.2 và xa hơn nữa. Điều này có nghĩa là bạn có thể thả mùa xuân 2.0 JAR trong các dự án hiện tại của bạn, và bạn sẽ không có một sự phụ thuộc duy nhất bị hỏng. | 56 CHAPTER 2 THE CORE CONTAINER Implementing Factory Objects We ve already discussed the advantage of factory objects compared to factory methods they allow for an extra layer of configuration. Bean definitions that call a method on a factory object use two attributes the factory-bean attribute which refers to the factory object and the factory-method which indicates the method to call on the factory object. Listing 2-42 demonstrates configuring the java.text.SimpleDateFormat class as a factory object. Listing 2-42. Configuring SimpleDateFormat As a Factory Object in the Container xml version 1.0 encoding UTF-8 DOCTYPE beans PUBLIC - SPRING DTD BEAN EN http www.springframework.org dtd spring-beans.dtd beans -- 1 -- bean id socketFactory class javax.net.SocketFactory factory-method getDefault bean bean id localhost factory-bean socketFactory factory-method createSocket constructor-arg value localhost constructor-arg value 80 bean bean id apress.com factory-bean socketFactory factory-method createSocket constructor-arg value www.apress.com constructor-arg value 80 bean beans In Listing 2-42 we first configure the javax.net.SocketFactory class using the factory-method attribute which creates beans from a static factory method in this case getDefault . Next we use the socketFactory bean as a factory object in the two subsequent bean definitions where we call the createSocket method and provide it with two arguments. The configuration in Listing 2-42 is typical for factory objects where one bean definition configures the factory object and one or more other bean definitions call methods on the factory object. In fact this method of object construction is not just for factories. It provides a generic mechanism for object construction. Listing 2-43 shows the integration test for this factory object configuration. Listing 2-43. Obtaining the Sockets Created Using the Factory Cbject package com.apress.springbook.chapter02 import junit.framework.TestCase import .