znLin

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            1 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks

          2011年3月21日 #

          今天剛剛學習了Spring,做個簡單的例子分享一下。。。

          1、首先,創建接口BookService(包com.Vy.spring.Biz)

          package com.Vy.spring.Biz;

          public interface BookService {
              public boolean buy(String userName,String bookName,double price);
          }
          2、創建接口實現類BookServiceImpl(包com.Vy.spring.BizImpl)

          package com.Vy.spring.BizImpl;

          import com.Vy.spring.Biz.BookService;

          public class BookServiceImpl implements BookService {

              @Override
              public boolean buy(String userName, String bookName, double price) {
                  // TODO Auto-generated method stub
                  System.out.println("業務方法buy開始執行");
                  System.out.println("·"+userName+"購買圖書:"+bookName);
                  System.out.println("·"+userName+"增加積分"+(int)(price/10));
                  System.out.println("·"+"向物流系統下發貨單");
                  System.out.println("業務方法buy結束");
                  return true;
              }
          }
          3、添加spring支持(spring版本為2.5)
               選擇AOP庫和Spring Core支持
               下一步修改xml名字為aop.xml
          4、編寫xml文件
          <?xml version="1.0" encoding="UTF-8"?>
          <beans
              xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:p="http://www.springframework.org/schema/p"
              xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
              <bean id="bookServiceTarget" class="com.Vy.spring.BizImpl.BookServiceImpl"></bean>
              <bean id="logAdvice" class="com.Vy.spring.BizImpl.LogAdvice"></bean>
              <bean id="bookService" class="org.springframework.aop.framework.ProxyFactoryBean">
                  <property name="proxyInterfaces">
                      <value>com.Vy.spring.Biz.BookService</value>
                  </property>
                  <property name="interceptorNames">
                      <list><value>logAdvice</value></list>
                  </property>
                  <property name="target" ref="bookServiceTarget"></property>
              </bean>
          </beans>
          5、創建測試類test(包Test)
          package Test;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          import com.Vy.spring.Biz.BookService;

          public class test {

              /**
               * @param args
               */
              public static void main(String[] args) {
                  // TODO Auto-generated method stub
                  ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
                  BookService bookService=(BookService)context.getBean("bookService");
                  bookService.buy("znLin", "spring in action", 20.00);
              }

          }
          6、運行測試、顯示結果

          [系統日志][2011 年 03 月 21 日 06 時 21 分 28 秒]buy([znLin, spring in action, 20.0])
          業務方法buy開始執行
          ·znLin購買圖書:spring in action
          ·znLin增加積分2
          ·向物流系統下發貨單
          業務方法buy結束
          ------------------------------------------------------
          第一次寫博客,不會設置修改圖片,以后學習,寫的不好,看者見諒!!!



          posted @ 2011-03-21 18:26 z_clear_n 閱讀(167) | 評論 (0)編輯 收藏

          僅列出標題  
          主站蜘蛛池模板: 阿拉善盟| 富宁县| 皮山县| 裕民县| 延寿县| 长岛县| 六安市| 城市| 神农架林区| 获嘉县| 奇台县| 许昌县| 凤山县| 乐都县| 开江县| 梓潼县| 惠东县| 新乡市| 江都市| 安阳县| 黑山县| 集贤县| 库车县| 鹤峰县| 定陶县| 靖江市| 且末县| 淮阳县| 建宁县| 邻水| 义乌市| 宜川县| 台前县| 兰州市| 灵丘县| 苏州市| 京山县| 麻城市| 通江县| 兴宁市| 花垣县|