znLin

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            1 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks
          今天剛剛學習了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 on 2011-03-21 18:26 z_clear_n 閱讀(167) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 霍邱县| 辽宁省| 同德县| 文成县| 凤山市| 玉溪市| 新建县| 高要市| 西乌珠穆沁旗| 沁水县| 潮安县| 古浪县| 城市| 乌兰察布市| 桐梓县| 遵义县| 沙坪坝区| 察雅县| 乐陵市| 周至县| 宣汉县| 通海县| 宁陵县| 华安县| 满城县| 大方县| 永定县| 镇巴县| 罗田县| 平陆县| 龙里县| 营口市| 平果县| 朔州市| 阳东县| 康保县| 宜丰县| 汨罗市| 屯门区| 吴桥县| 河西区|