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 閱讀(168) | 評論 (0)編輯 收藏

          僅列出標題  
          主站蜘蛛池模板: 阳泉市| 大兴区| 邹城市| 墨竹工卡县| 深泽县| 洛隆县| 景德镇市| 易门县| 桃江县| 吉木乃县| 嘉善县| 内乡县| 贵州省| 会东县| 九龙县| 田林县| 师宗县| 青川县| 宣汉县| 江口县| 江门市| 休宁县| 乐清市| 平安县| 申扎县| 罗源县| 黔南| 怀集县| 广宗县| 咸宁市| 吴江市| 海口市| 邮箱| 霍邱县| 平南县| 澄城县| 定州市| 平遥县| 鱼台县| 阳西县| 霍城县|