| spring lab4 備忘記
這次備忘記主要是記錄 Spring course lab4 的過程.
lab4 requirement 的下載位址是:
http://blog.matrix.org.cn/resources/joeyta/spring_lab_04.zip
源程式下載位址是:
http://blog.matrix.org.cn/resources/joeyta/cpttm_spring_lab.zip
要求使用 Spring AOP 裡的 Advisor 及 Auto proxy 實作圖書館系統簡單功能.
包括 FlowControlPointcut 及 Introduction.
開始備忘記:
[1] 開始實作 lab 4:
<!--------------------- beans-config-lab4.xml ------------------>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEA N//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="libraryLogBeforeAdvice" class="mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice" />
<bean id="libraryAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedNames">
<list>
<value>borrowBook</value>
<value>returnBook</value>
</list>
</property>
<property name="advice">
<ref bean="libraryLogBeforeAdvice" />
</property>
</bean>
<bean id="libraryChinaService" class="mo.org.cpttm.spring.lab4.Library">
<property name="libraryId">
<value>china</value>
</property>
</bean>
<bean id="libraryMacauService" class="mo.org.cpttm.spring.lab4.Library">
<property name="libraryId">
<value>macau</value>
</property>
</bean>
<bean id="libraryHKService" class="mo.org.cpttm.spring.lab4.Library">
<property name="libraryId">
<value>hk</value>
</property>
</bean>
<bean id="libraryNetworkService" class="mo.org.cpttm.spring.lab4.LibraryNetwork">
<property name="members">
<list>
<ref bean="libraryChinaService"/>
<ref bean="libraryMacauService"/>
<ref bean="libraryHKService"/>
</list>
</property>
</bean>
<bean id="libraryNetworkFlowControlPointcut"
class="org.springframework.aop.support.ControlFlowPointcut">
<constructor-arg index="0">
<value>mo.org.cpttm.spring.lab4.LibraryNetwork</value>
</constructor-arg>
<constructor-arg index="1">
<value>searchBook</value>
</constructor-arg>
</bean>
<bean id="conferenceCenterIntroduction" class="mo.org.cpttm.spring.lab4.ConferenceCenterIntroduction"></bean>
<bean id="conferenceCenterAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg index="0">
<ref bean="conferenceCenterIntroduction"/>
</constructor-arg>
<constructor-arg index="1">
<value>mo.org.cpttm.spring.lab4.IConferenceCenter</value>
</constructor-arg>
</bean>
<bean id="libraryNetworkAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice">
<ref bean="libraryLogBeforeAdvice"/>
</property>
<property name="pointcut">
<ref bean="libraryNetworkFlowControlPointcut"/>
</property>
</bean>
<bean id="libraryProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>libraryAdvisor</value>
<value>libraryNetworkAdvisor</value>
<value>conferenceCenterAdvisor</value>
</list>
</property>
</bean>
</beans>
<!--------------------- beans-config-lab4.xml ------------------>
/**************** LibraryLogBeforeAdvice.java *******************/
package mo.org.cpttm.spring.lab4;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.aop.MethodBeforeAdvice;
public class LibraryLogBeforeAdvice implements MethodBeforeAdvice {
private Logger logger = Logger.getLogger(this.getClass().getName());
public void before(Method method, Object[] args, Object target)
throws Throwable {
logger.log(Level.INFO, "Before call by : " + method.getName());
if (args != null) {
for (Object object : args) {
logger.log(Level.INFO, "Arguments : " + object.toString());
}
}
}
}
/**************** LibraryLogBeforeAdvice.java *******************/
/**************** Book.java *******************/
package mo.org.cpttm.spring.lab4;
public class Book {
@Override
public String toString() {
return "Book id:" + this.id + " # name:" + this.name + " # status:"
+ this.status;
}
public static final int AVAILABLE = 1;
public static final int UNAVAILABLE = 2;
private String id;
private String name;
private int status;
public Book() {
}
public Book(String id, String name, int status) {
this.id = id;
this.name = name;
this.status = status;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
/**************** Book.java *******************/
/**************** ILibrary.java *******************/
package mo.org.cpttm.spring.lab4;
import java.util.List;
public interface ILibrary {
public String getLibraryId();
public void setLibraryId(String libraryId);
public void borrowBook(String bookId);
public void returnBook(String bookId);
public List<Book> searchBook(String keyword);
}
/**************** ILibrary.java *******************/
/**************** Library.java *******************/
package mo.org.cpttm.spring.lab4;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
public class Library implements ILibrary {
private Map<String,Book> books;
private String libraryId;
public Library() {
books = new TreeMap<String,Book>();
books.put("b1",new Book("b1","springframework", Book.AVAILABLE));
books.put("b2",new Book("b2","ejb3", Book.AVAILABLE));
books.put("b3",new Book("b3","hibernate", Book.AVAILABLE));
books.put("b4",new Book("b4","design pattern", Book.AVAILABLE));
books.put("b5",new Book("b5","tapestry with ajax", Book.AVAILABLE));
books.put("b6",new Book("b6","webwork", Book.AVAILABLE));
books.put("b7",new Book("b7","jsf with ajax", Book.AVAILABLE));
}
public void borrowBook(String bookId) {
books.get(bookId).setStatus(Book.UNAVAILABLE);
}
public String getLibraryId() {
return this.libraryId;
}
public void returnBook(String bookId) {
books.get(bookId).setStatus(Book.AVAILABLE);
}
public List<Book> searchBook(String keyword) {
List<Book> bookResultList = new ArrayList<Book>();
for (Iterator iter = books.entrySet().iterator(); iter.hasNext();) {
Entry entry = (Entry) iter.next();
Book book = (Book)entry.getValue();
if(book.getName().toLowerCase().indexOf(keyword.toLowerCase()) >= 0 ){
bookResultList.add(book);
}
}
return bookResultList;
}
public void setLibraryId(String libraryId) {
this.libraryId = libraryId;
}
}
/**************** Library.java *******************/
/**************** ILibraryNetwork.java *******************/
package mo.org.cpttm.spring.lab4;
import java.util.List;
import java.util.Map;
public interface ILibraryNetwork {
public void setMembers(ILibrary[] members);
public Map<String,List<Book>> searchBook(String keyword);
}
/**************** ILibraryNetwork.java *******************/
/**************** LibraryNetwork.java *******************/
package mo.org.cpttm.spring.lab4;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class LibraryNetwork implements ILibraryNetwork {
ILibrary[] libraries;
public Map<String,List<Book>> searchBook(String keyword) {
Map<String, List<Book>> map = new TreeMap<String, List<Book>>();
for (ILibrary library : libraries) {
map.put(library.getLibraryId(), library.searchBook(keyword));
}
return map;
}
public void setMembers(ILibrary[] members) {
this.libraries = members;
}
}
/**************** LibraryNetwork.java *******************/
/**************** IConferenceCenter.java *******************/
package mo.org.cpttm.spring.lab4;
import java.util.Date;
public interface IConferenceCenter {
public void book(Date date, String person);
}
/**************** IConferenceCenter.java *******************/
/************* ConferenceCenterIntroduction.java ****************/
package mo.org.cpttm.spring.lab4;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
public class ConferenceCenterIntroduction extends
DelegatingIntroductionInterceptor implements IConferenceCenter {
private static final long serialVersionUID = -2727120915723691792L;
private Map<Date, String> map;
public void book(Date date, String person) {
if (map == null) {
map = new TreeMap<Date, String>();
}
map.put(date, person);
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
Entry entry = (Entry) iter.next();
Date bookDate = (Date) entry.getKey();
String bookPerson = (String) entry.getValue();
System.out.println("Conference Book Date:" + bookDate + " # Person:" + bookPerson);
}
}
}
/************* ConferenceCenterIntroduction.java ****************/
/**************** LibraryDemo.java *******************/
package mo.org.cpttm.spring.lab4;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class LibraryDemo {
public static void printBooks(String location,List<Book> list){
for (Iterator<Book> iter = list.iterator(); iter.hasNext();) {
Book book = iter.next();
System.out.println(location +"#" + book);
}
}
@SuppressWarnings("unchecked")
public static void printBooks(Map<String,List<Book>> map){
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
Entry entry = (Entry) iter.next();
String key = (String)entry.getKey();
List<Book> value = (List<Book>) entry.getValue();
printBooks(key,value);
}
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
"mo/org/cpttm/spring/lab4/beans-config-lab4.xml");
ILibrary library = (ILibrary) context.getBean("libraryChinaService");
library.borrowBook("b1");
printBooks("China",library.searchBook("spring"));
library = (ILibrary) context.getBean("libraryMacauService");
library.borrowBook("b1");
library.returnBook("b1");
library.borrowBook("b7");
printBooks("Macau",library.searchBook("ajax"));
library = (ILibrary) context.getBean("libraryHKService");
library.borrowBook("b3");
library.borrowBook("b4");
library.borrowBook("b5");
printBooks("HK",library.searchBook("e"));
System.out.println("************************************************");;
ILibraryNetwork libraryNetwork = (ILibraryNetwork) context.getBean("libraryNetworkService");
printBooks(libraryNetwork.searchBook("e"));
System.out.println("************************************************");;
((IConferenceCenter) libraryNetwork).book(new Date(), "joeyta");
}
}
/**************** LibraryDemo.java *******************/
執行結果如下所示:
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : borrowBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : b1
China#Book id:b1 # name:springframework # status:22006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : borrowBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : b1
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : returnBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : b1
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : borrowBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : b7
Macau#Book id:b5 # name:tapestry with ajax # status:1
Macau#Book id:b7 # name:jsf with ajax # status:2
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : borrowBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : b3
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : borrowBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : b4
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : borrowBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : b5
HK#Book id:b1 # name:springframework # status:1
HK#Book id:b2 # name:ejb3 # status:1
HK#Book id:b3 # name:hibernate # status:2
HK#Book id:b4 # name:design pattern # status:2
HK#Book id:b5 # name:tapestry with ajax # status:2
HK#Book id:b6 # name:webwork # status:1
**********************************************************
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : getLibraryId
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : searchBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : e
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : getLibraryId
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : searchBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : e
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : getLibraryId
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Before call by : searchBook
2006/10/13 上午 10:22:29 mo.org.cpttm.spring.lab4.LibraryLogBeforeAdvice before
資訊: Arguments : e
china#Book id:b1 # name:springframework # status:2
china#Book id:b2 # name:ejb3 # status:1
china#Book id:b3 # name:hibernate # status:1
china#Book id:b4 # name:design pattern # status:1
china#Book id:b5 # name:tapestry with ajax # status:1
china#Book id:b6 # name:webwork # status:1
hk#Book id:b1 # name:springframework # status:1
hk#Book id:b2 # name:ejb3 # status:1
hk#Book id:b3 # name:hibernate # status:2
hk#Book id:b4 # name:design pattern # status:2
hk#Book id:b5 # name:tapestry with ajax # status:2
hk#Book id:b6 # name:webwork # status:1
macau#Book id:b1 # name:springframework # status:1
macau#Book id:b2 # name:ejb3 # status:1
macau#Book id:b3 # name:hibernate # status:1
macau#Book id:b4 # name:design pattern # status:1
macau#Book id:b5 # name:tapestry with ajax # status:1
macau#Book id:b6 # name:webwork # status:1
**********************************************************
Conference Book Date:Fri Oct 13 10:22:29 GMT+08:00 2006 # Person:joeyta
|