SlideShare a Scribd company logo
1 of 35
Download to read offline
。式方用使
的中   Spring   在等務服程排與務服件郵、務服程遠紹介將中節章個這
                         。單簡加更時務服件元或 些一用    API
使 在 您 讓,式 方 的 致 一 且 化 簡 了 供 提,下 裝 封 的 樣 這 在,裝 封 象 抽 層
一 供 提 是 而,案 方 決 解 的 接 直 供 提 不     , 務服業企些一於對
                            API S pring
                                          API 封裝
     9
Spring 2.0    良信林(冊手術技          – http://openhome.cc      )

9.1 遠程
務服地本用使同如就,時務服端服伺用使在件物的端地本讓務服程遠
,術技程遠的同不種各於對,節細等結連路網到觸接需不端戶客,樣一
相不各術技務服程遠的用採所層底使即,式方用使的致一了供提
Spring
下一紹介來將節小個這,的致一是卻式方的們它用運中
            Spring           在,同
RMI       。用使的
         H essian   及以   、
                     B urlap 、  Http Invoker



9.1.1 RMI
, 的現出就始開
       RMI   從是)           (
              R emote Method Invocation         JDK 1.1                  API
然,樣一件物地本用使同如就,時務服的供提件物端遠用使在端戶客讓它
承繼須必時義定在面介務服是像,續手的串連一有須必時用使在
      RMI                      而
        承繼須必時作實在
java.rmi.Remote     務服、面介       Server                        java.rmi.Unicast-
上定設,等    與 生產令指 用使須必、別類
RemoteObject                       rmic          stub     skeleton
                          。雜繁續手的
                   過透中    在以可您
                     Spring          org.springframework.remoting.rmi.Rmi-
在   解了,子例看看際實來,續手的 用使化簡
ServiceExporter                RMI                                   Spring
務服個一義定後然,寫撰的端服伺 下一看先首,化簡與用使的上
RMI                                    RMI
                          :面介的件物
     RMIServerDemo                                        ISomeService.java
    package onlyfun.caterpillar;

    public interface ISomeService {
        public String doSomeService(String some);
        public int doOtherService(int other);
    }




2
Chapter 9 API   裝封

承繼用 不面介的件物 務服               j ava.rmi.Remote作實在而,面介        I SomeService
        承繼用不也時
             java.rmi.UnicastRemoteObject    :如例,別類
    RMIServerDemo                                 SomeServiceImpl.java
package onlyfun.caterpillar;

public class SomeServiceImpl implements ISomeService {
    public String doSomeService(String some) {
        return some + " is processed";
    }

     public int doOtherService(int other) {
         return ++other;
     }
}



的過改修經已個一回傳是只都法方個兩,範示的單簡個是只作實個這
, 例 實 成 生、理 管 讓,義定中檔義定 在要只來下接,值
                 Bean                    Spring             Bean
               :如例,務服 動啟、冊註可即此如
                    RMI


    RMIServerDemo                                       rmi-server.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

     <bean id="someService"
           class="onlyfun.caterpillar.SomeServiceImpl"/>

     <bean id="serviceExporter"
           class="org.springframework.remoting.
                    →  rmi.RmiServiceExporter">
         <property name="service" ref="someService"/>
         <property name="serviceName" value="SomeService"/>
         <property name="serviceInterface"
                   value="onlyfun.caterpillar.ISomeService"/>



                                                                           3
Spring 2.0      良信林(冊手術技          – http://openhome.cc        )

        </bean>

    </beans>



                                      訴告要只
                    org.springframework.remoting.rmi.RmiServiceExporter
)         為定設上性屬              在意注(稱名、件物務服
                               "serviceName"                      "SomeService"
務服 , 後 例 實 成 生 並 檔 義 定 取 讀      後之 , 面 介 的 理 代 要 與
                            Spring                     Bean             R MI
  :務服 動啟以,別類               的 單 簡個一 寫 撰 來 , 動 啟 會 就
                                      RMIServer               RMI


     RMIServerDemo                                                RMIServer.java
    package onlyfun.caterpillar;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.
           support.ClassPathXmlApplicationContext;
    import org.springframework.remoting.rmi.RmiServiceExporter;

    public class RMIServer {
        public static void main(String[] args)
                               throws IOException {
          ApplicationContext context =
              new ClassPathXmlApplicationContext("rmi-server.xml");

               System.out.println("    動啟RMI Server..");
               System.out.println("   入輸請  exit   閉關Server: ");

               BufferedReader reader =
                   new BufferedReader(new InputStreamReader(System.in));

               while(true) {
                   if(reader.readLine().equals("exit")) {
                       break;
                   }
               }




4
Chapter 9 API     裝封

           RmiServiceExporter rmiServiceExporter =
              (RmiServiceExporter) context.getBean("serviceExporter");
           rmiServiceExporter.destroy();
      }
}



一另用使動自會     , 動 啟 會 就 務 服 ,後 之 式 程 的 面 上 行 運 在
                                R MI              S pring
入輸以可您,題問理處的緒行執心關用不以所,務服 行執來緒行執個
                  RMI
  向以端戶客 個一作實何如,下一看來著接,式程開離接直
"exit"                                              RMI               RMI
是就也,面介的象抽於賴依是端戶客,是的得記要先首,務服求要器服伺
     。份一有端戶客在須必也檔 之面介
          ISomeService         .class       的前先
                    過透要只,時務服 要需端戶客在
                    R MI                o rg.springframework.remoting.
的定設前先至應對( 的務服知告並,
rmi.RmiProxyFactoryBean                       URL
地本用使在像好就時式程寫撰在,可即面介的理代、)稱名
"SomeService"
         : 寫 撰 下 如 以 可 檔 義 定 如例, 樣 一 務 服 的 理 管 端
                             Bean


    RMIClientDemo                                           rmi-client.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

      <bean id="someServiceProxy"
            class="org.springframework.remoting.
                        rmi.RmiProxyFactoryBean">
                         →
          <property name="serviceUrl"
                    value="rmi://localhost/SomeService"/>
          <property name="serviceInterface"
                    value="onlyfun.caterpillar.ISomeService"/>
      </bean>

</beans>
Spring 2.0     良信林(冊手術技           – http://openhome.cc       )

伺定指著接,頭開  以是它,定設的性屬       到意注
                "serviceUrl"                      "rmi://"
的上器服伺 用使以式程端戶客的單簡個寫撰來,稱名務服與址位器服                                  RMI
                            :務服
        RMIClientDemo                                            RMIClient.java
    package onlyfun.caterpillar;

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

    public class RMIClient {
        public static void main(String[] args) {
            ApplicationContext context =
                new ClassPathXmlApplicationContext(
                        "rmi-client.xml");

              ISomeService service =
                  (ISomeService) context.getBean("someServiceProxy");

              String result1 = service.doSomeService("Some request");
              System.out.println(result1);

              int result2 = service.doOtherService(1);
              System.out.println(result2);
          }
    }



理代,節細種種的結連務服關有到理處要需不全完,中作實的式程在
得取在正到意注會不本根,看來式程從單,節細些這成完您幫動自會件物
            :示 所 下 如 果 結 的 行 執,務 服 的 上 器 服 伺 端 遠



                        圖   9.1 RMIClientDemo   果結行執的案專
6
Chapter 9 API       裝封

9.1.2 Hessian、Burlap
      Hessian      B urlap           由是        、
                                 Caucho Technology   (   h ttp://www.caucho.com/   )
                   HTTP     。務服程遠的現實            過透,出提所
若不而,送傳行進
      Hessian       用使 息 訊 位 進 二 的 性 中 以 件 物 將 是         HTTP
某於限受不此因,的性中是息訊位進二於由,式格化列序的 用使
RMI         Java
較寬頻的需所時輸傳在料資位進二,端服伺或端戶客的現實所言語式程種
                                                。點優其是小
的高較有且件文 ,送傳行進式格件文 以件物將是則
      Burlap                    XML                         X ML
當 ,息訊 的 收 接 所 讀 解 以 可 就 件 文     析 剖 以 可式程 用 應 要 只 , 性 讀 可
                                       XML
                  。 端服伺 與 端 戶 客 的 現 實 所言語 種 某 於 限 受 不 也 然
、      於由,的似類是法方的
          Spring                及
                             Hessian     用使中
                                       Burlap       在                    Hessian
就也,架框
Burlap             配搭要 時 用 使 在 以 所 , 送 傳
                   HTTP                         過透是       Spring Web
以,       用使何如範示來子例的際實個舉,
          DispatcherServlet                         用使是                Hessian
、
9.1.1       的好寫撰已用使接直以可,說來子 例 的紹介中
                    RMI                                            ISomeService
   :
SomeServiceImpl   置配中          在要端服伺而, web.xml            DispatcherServlet


  HessianServerDemo                                                       web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>

        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>



                                                                                       7
Spring 2.0    良信林(冊手術技         – http://openhome.cc   )

           <servlet-class>
              org.springframework.web.servlet.DispatcherServlet
           </servlet-class>
           <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/service-config.xml</param-value>
           </init-param>
           <load-on-startup>1</load-on-startup>
       </servlet>

        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>*.service</url-pattern>
        </servlet-mapping>
    </web-app>



       在   Hessian       用使,邊這端服伺的   org.springframework.remoting.cau-
                     :務服佈發來
cho.HessianServiceExporter


     HessianServerDemo                               service-config.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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

       <bean id="urlMapping"
             class="org.springframework.web.servlet.
                        → handler.SimpleUrlHandlerMapping">
           <property name="mappings">
               <props>
                   <prop key="/some.service">serviceExporter</prop>
               </props>
           </property>
       </bean>

       <bean id="someService"
             class="onlyfun.caterpillar.SomeServiceImpl"/>

       <bean id="serviceExporter"



8
Chapter 9 API     裝封

            class="org.springframework.remoting.
                         caucho.HessianServiceExporter">
                           →
          <property name="service" ref="someService"/>
          <property name="serviceInterface"
                    value="onlyfun.caterpillar.ISomeService"/>
      </bean>

</beans>



       求請,上置設的                      在到意注
                      SimpleUrlHandlerMapping                   " some.service"
有沒是就也,稱名務服冊註用不邊這在,
                " serviceExporter"    給配分會的
則,    是的用使果如,要需不
"serviceName"              、   ,性屬
                           H essian   B urlap                      Burlap
       用改要只性屬              的
               "serviceExporter"       在上定設
                                        "class"                    org.spring-
   。可即別類
framework. remoting.caucho.BurlapServiceExporter

,後之式程用應 的計設上以入載,器容              動啟要只來下接
                           Servlet                       Web
的需所     入加中錄目 在要得記,了動啟會就端服伺
  Hessian                                      則  lib           Hessian
目
API  下錄目 中案檔載下 .jar       用使以可這,案檔 之別類  Spring            lib        caucho
     入加得記要,話的          用使果如,案檔
      hessian-3.0.20.jar                     的錄
                                            Burlap                  Burlap API
        .jar                     。案檔 的需所
來案專   Hessian的寫撰前先用使以可則寫撰的端戶客                           RMIClientDemo
         : 如 例 , 可 即 檔 義 定 下 一改修 要 只 上 實 事 , 寫 改
                                 Bean


  HessianClientDemo                                       hessian-client.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

      <bean id="someServiceProxy"
            class="org.springframework.remoting.
                        caucho.HessianProxyFactoryBean">
                           →
          <property name="serviceUrl" value="http://localhost:8080/
Spring 2.0      良信林(冊手術技          – http://openhome.cc      )

                          → HessianServerDemo/some.service"/>
              <property name="serviceInterface"
                  value="onlyfun.caterpillar.ISomeService"/>
          </bean>

    </beans>



   的準標 個 是 它 , 定 設 的 性 屬
                 "serviceUrl"      到意注                     HTTP   來,址位求請
:務服的上器服伺           用 使 以 式程端 戶 客 的 單 簡 個 寫 撰
                                     Hessian


        HessianClientDemo                                  HessianClient.java
    package onlyfun.caterpillar;

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

    public class HessianClient {
        public static void main(String[] args) {
            ApplicationContext context =
                new ClassPathXmlApplicationContext(
                        "hessian-client.xml");

               ISomeService service =
                   (ISomeService) context.getBean("someServiceProxy");
               String result1 = service.doSomeService("Some request");
               System.out.println(result1);

               int result2 = service.doOtherService(1);
               System.out.println(result2);
          }
    }



果結的行執,案檔 的                 括 包 入加要 , 得 記 須 必 的 樣 同
                                       Hessian API     .jar
用 使 要 果 如 , 面 畫 的 圖考參 以 可 , 的 同 相 是 案 專
        RMIClientDemo                              與 9.1
Burlap                        用改要只上定設則,
                                     org.springframework.remoting.caucho.




1
Chapter 9 API      裝封

BurlapProxyFactoryBean       入加須必得記要然當,可即                 Burlap API   需所
的   .jar   。案檔
9.1.3 Http Invoker
來制機化列序的 用使時送傳,件物送傳
      Http Invoker      用使HTTP                      Java
來架框      配搭要時們它用使在以所,送傳
                 HTTP        過透於由,送傳                  Spring Web
       的 寫 改 以 可,        到 用 使 是 就 也,用 使
                      DispatcherServlet           9.1.2    HessianServer-
Demo   :了以可就         下一改修要只,案專
                             service-config.xml


    HttpInvokerDemo                                 service-config.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

      <bean id="urlMapping"
            class="org.springframework.web.servlet.
                      → handler.SimpleUrlHandlerMapping">
          <property name="mappings">
              <props>
                  <prop key="/some.service">serviceExporter</prop>
              </props>
          </property>
      </bean>

      <bean id="someService"
            class="onlyfun.caterpillar.SomeServiceImpl"/>

      <bean id="serviceExporter"
            class="org.springframework.remoting.
                      → httpinvoker.HttpInvokerServiceExporter">
          <property name="service" ref="someService"/>
          <property name="serviceInterface"
                 value="onlyfun.caterpillar.ISomeService"/>
      </bean>



                                                                        11
Spring 2.0   良信林(冊手術技        – http://openhome.cc   )

 </beans>



寫 改 以 可,份 部 的 端 戶 客 來 下 接            HessianClientDemo   修,容內的案專
      :容內的檔義定 下一改
       Bean


  InvokerClientDemo                               invoker-client.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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

     <bean id="someServiceProxy"
           class="org.springframework.remoting.
                       → httpinvoker.HttpInvokerProxyFactoryBean">
         <property name="serviceUrl" value="http://localhost:8080/
                       → HttpInvokerDemo/some.service"/>
         <property name="serviceInterface"
             value="onlyfun.caterpillar.ISomeService"/>
     </bean>

 </beans>



撰來,址位求請   的準 標 是 它 , 定 設 的 性 屬
             "serviceUrl"                到意注     HTTP
      :務服的上器服伺               用 使以式 程 端 戶 客 的 單 簡 寫
                            Http Invoker


  InvokerClientDemo                               InvokerClient.java
 package onlyfun.caterpillar;

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

 public class InvokerClient {
     public static void main(String[] args) {
         ApplicationContext context =



12
Chapter 9 API   裝封

           new ClassPathXmlApplicationContext(
                   "invoker-client.xml");

        ISomeService service =
            (ISomeService) context.getBean("someServiceProxy");

        String result1 = service.doSomeService("Some request");
        System.out.println(result1);

        int result2 = service.doOtherService(1);
        System.out.println(result2);
    }
}



    與果結的行執      RMIClientDemo   。面畫的 圖考參以可,的同相是案專         9.1




                                                                    13
Spring 2.0   良信林(冊手術技         – http://openhome.cc   )

9.2 郵件
             的       由 是 援支的 務 服 送 發 件 郵 於 對
                                    Spring   org.springframework.mail.
MailSender  :別類作實個兩有它,義定所面介                  o rg.springframework.mail.
cos.CosMailSenderImpl    與      org.springframework.mail.javamail.Java-
       的它在(
MailSenderImpl           對為者前,Jason Hunter's COS              Java Servlet
     。援支之
Programming 對為 者 後 , 援 支 之 ) 的 供 提 中 書        Java Mail



9.2.1 簡單郵件

服件郵     供提何如   範示,用使的    以將邊這在
                  Java Mail             Spring            Java Mail
範示的式程,作實何如送傳之件郵字文純的單簡最個看來先首,裝封的務
                             :下如
  SimpleMailDemo                                   SimpleMailDemo.java
 package onlyfun.caterpillar;

 import org.springframework.mail.javamail.JavaMailSenderImpl;
 import org.springframework.mail.SimpleMailMessage;

 public class SimpleMailDemo {
     public static void main(String[] args) throws Exception {
         JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
         //  定設 Mail Server
         senderImpl.setHost("your_mail_server.com");

        //   息訊件郵立建
        SimpleMailMessage mailMessage = new SimpleMailMessage();

        //   文內與題主、人件寄、人件收定設
        mailMessage.setTo("xxx@your_mail_server.com");
        mailMessage.setFrom("xxx@your_mail_server.com");
        mailMessage.setSubject("Test");
        mailMessage.setText("This is a test!!!");



14
Chapter 9 API   裝封

         // 件郵送傳
         senderImpl.send(mailMessage);

         System.out.println("    送傳件郵   OK..");
     }
}


郵      的您果如,通溝作 埠接連與會設預
     JavaMailSenderImple                         25                        SMTP
以行 執 , 埠 接 連 定 設 來25      用使以可,埠接連 用使是不器服伺件    setPort()
     是 的 用 使 所 邊 這 在 , 案 檔 的 需 所 關相入 加 得 記 , 時 式 程 上
                           API          .jar                                 Spring
出送傳,案檔     lib    與    j2ee  的錄目 下錄目 中案檔載下
                                   activation.jar          mail.jar
                               : 示 所 下如後 到 收 接 在 件 郵 的 去



                   圖   9.2 SimpleMailDemo       果結行執的案專

9.2.2 HTML 郵件

     用使要果如       HTML     用使以可,話的件郵                   Spring   的   org.springframe-
                        立建來
work.mail.javamail.MimeMessageHelper                       HTML     用使接直,件郵
:範示作來例實
    HTMLMailDemo                                                HTMLMailDemo.java
package onlyfun.caterpillar;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.MimeMessageHelper;



                                                                                      1
Spring 2.0    良信林(冊手術技         – http://openhome.cc   )

 public class HTMLMailDemo {
     public static void main(String[] args) throws Exception {
         JavaMailSenderImpl senderImpl =
             new JavaMailSenderImpl();

            // 定設  Mail Server
            senderImpl.setHost("your_mail_server.com");

            // 息訊件郵立建
            MimeMessage mailMessage =
                senderImpl.createMimeMessage();
            MimeMessageHelper messageHelper =
                new MimeMessageHelper(mailMessage);

            // 文內與題主、人件寄、人件收定設
            messageHelper.setTo("xxx@your_mail_server.com");
            messageHelper.setFrom("xxx@your_mail_server.com");
            messageHelper.setSubject("Test");
            messageHelper.setText(
                    "<html><head></head><body><h1>Hello! Spring!"
                    + "</h1></body></html>", true);

            // 件郵送傳
            senderImpl.send(mailMessage);

            System.out.println("   送傳件郵   OK...");
       }
 }



             在
           MimeMessageHelper 為定設數參    的 上法方
                                     setText() 的別類    boolean
true   用啟要示表,時        HTML   :容內件郵的後去出送傳下一看來,件郵的式格




                      圖   9.3 HTMLMailDemo   果結行執的案專
16
Chapter 9 API   裝封

9.2.3 內嵌圖片或附檔

個一定給要時片圖嵌內在,片圖嵌內中件郵在以可也您                                  "cid"   直,值
                 :範示的式程看來接
 AttachedImageDemo                            AttachedImageDemo.java
package onlyfun.caterpillar;

import   org.springframework.mail.javamail.JavaMailSenderImpl;
import   javax.mail.internet.MimeMessage;
import   org.springframework.core.io.ClassPathResource;
import   org.springframework.mail.javamail.MimeMessageHelper;

public class AttachedImageDemo {
    public static void main(String[] args) throws Exception {
        JavaMailSenderImpl senderImpl =
                    new JavaMailSenderImpl();

         // 定設  Mail Server
         senderImpl.setHost("your_mail_server.com");

         // 息訊件郵立建
         MimeMessage mailMessage =
             senderImpl.createMimeMessage();
         MimeMessageHelper messageHelper =
             new MimeMessageHelper(mailMessage, true);

         // 文內與題主、人件寄、人件收定設
         messageHelper.setTo("xxx@your_mail_server.com");
         messageHelper.setFrom("xxx@your_mail_server.com");
         messageHelper.setSubject("Test");
         messageHelper.setText(
             "<html><head></head><body><h1>Hello! Spring!"
             + "</h1><img src="cid:caterpillar">"
             + "</body></html>", true);

         ClassPathResource img =
                 new ClassPathResource("caterpillar.jpg");
         messageHelper.addInline("wish", img);




                                                                    17
18
             。 況 情 的 片 圖 示 顯 法無生 發 會 中 體 軟 端 戶 客
些某在能可,題問有會援支的片圖嵌內於對端戶客件郵的有是處壞,片圖
的結連所得取以可時件郵取讀者用使讓以,案檔供提上器服伺在須必,式
方的結連定給像用不是處好,壞有好有中件郵入加案檔像影將接直
              果結行執的案專        9.4 AttachedImageDemo      圖
                                                   :面畫
之 件郵的 後 送 傳 下 一 看 來 ,中件 郵 至 入 加 案 檔 像 影 將 以 可 法 方               addInline()
,應對定指值        "cid"
                 的中法方             addInline()
                                    與定設個這,                     "caterpillar"
為值  "cid"
        定指法方          setText()
                         在,式模               multipart
                                           用啟要示表,                        true
為值  boolean
          的定給所時例實的別類                    MimeMessageHelper
                                                 構建在
                                                                          }
                                                                     }
                        ...");    功成送傳件郵   System.out.println("
                                  senderImpl.send(mailMessage);
                                  //                    件郵送傳
              )   – http://openhome.cc      良信林(冊手術技            Spring 2.0
Chapter 9 API   裝封

    (案檔加附用使中件郵在以可也您                  A ttachment file      用使要只,)Mime-
中其,法方
MessageHelper   的 addAttachment()               "cid"   加附示顯來用是值
        : 下 如例實 個 一 , 稱 名 的 案 檔
  AttachedFileDemo                              AttachedFileDemo.java
package onlyfun.caterpillar;

import   org.springframework.mail.javamail.JavaMailSenderImpl;
import   javax.mail.internet.MimeMessage;
import   org.springframework.core.io.ClassPathResource;
import   org.springframework.mail.javamail.MimeMessageHelper;

public class AttachedFileDemo {
    public static void main(String[] args) throws Exception {
        JavaMailSenderImpl senderImpl =
                      new JavaMailSenderImpl();

         // 定設  Mail Server
         senderImpl.setHost("your_mail_server.com");

         // 息訊件郵立建
         MimeMessage mailMessage =
             senderImpl.createMimeMessage();
         MimeMessageHelper messageHelper =
             new MimeMessageHelper(mailMessage, true);

         // 文內與題主、人件寄、人件收定設
         messageHelper.setTo("xxx@your_mail_server.com");
         messageHelper.setFrom("xxx@your_mail_server.com");
         messageHelper.setSubject("Test");
         messageHelper.setText(
             "<html><head></head><body><h1>Hello! Spring!"
             + "</h1></body></html>", true);

         ClassPathResource file =
                 new ClassPathResource("caterpillar.zip");
         messageHelper.addAttachment("caterpillar.zip", file);

         // 件郵送傳
         senderImpl.send(mailMessage);



                                                                         1
Spring 2.0   良信林(冊手術技           – http://openhome.cc   )

            System.out.println("   功成送傳件郵   ...");
        }
    }



        : 面 畫件郵 的 後 送 傳 下 一 看 來




                     圖   9.5 AttachedFileDemo   果結行執的案專




2
Chapter 9 API     裝封

9.3 排程
與           有 供 提 準 標 的 , 作工的 行 執 時 定 程 排 於 對
                                   J DK      API        java.util.Timer
用善以可您讓,裝封象抽了供提它對則
java.util.TimerTask        S pring ,別類
間之務任 與 務 任 定 指 能 只 , 限 有 能 功
Spring                           而然,能功理管器容的
                               Timer
   用使以可您,務任行執時定點間時個某定指法無,) (期週的
         P eriod                                                     Quartz
,能功程排的多更了供提它,)
  h ttp://www.opensymphony.com/quartz/        (
  Spring      。 便 方加更 上 用 使 在 , 裝 封 了 行 進
             Quartz                       對   而

9.3.1 使用 TimerTask

(務任程排個一義定要                 T ask   承繼以可,)      java.util.TimerTask   ,別類
         :如例
  TimerTaskDemo                                           DemoTask.java
package onlyfun.caterpillar;

import java.util.TimerTask;

public class DemoTask extends TimerTask {
    public void run() {
        System.out.println("Task is executed.");
    }
}



                      的
                      Spring  用使以可著接
                                   org.springframework.scheduling.timer.
    : 如 例 ,期週 行 執 的 務 任 義 定 來
ScheduledTimerTask




                                                                          21
Spring 2.0   良信林(冊手術技          – http://openhome.cc        )

  TimerTaskDemo                                            beans-config.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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

     <bean id="demoTask" class="onlyfun.caterpillar.DemoTask"/>

     <bean id="scheduledTimerTask"
           class="org.springframework.scheduling.
                        timer.ScheduledTimerTask">
                        →
         <property name="timerTask" ref="demoTask"/>
         <property name="period" value="600000"/>
         <property name="delay" value="10000"/>
     </bean>

     <bean id="timerFactoryBean"
           class="org.springframework.scheduling.
                        timer.TimerFactoryBean">
                        →
         <property name="scheduledTimerTasks">
             <list>
                 <ref bean="scheduledTimerTask"/>
             </list>
         </property>
     </bean>

 </beans>



,秒毫是位單的義定,中性屬
         ScheduledTimerTask    的別類      "period"      在
任 的義定 所 次 一 行 執 ) 秒 毫       ( 鐘 分 每 將,義 定 的 上 以 據 根 此 因
                              10          6 00000
少 多遲延 要 前 務 任 行 執 次 一 第 , 後 動 啟
         "delay"              Timer 了義定性屬             而,務
                                                      。秒毫
                       用使要
                   ScheduledTimerTask          的好義定org.springframework.sche-
要只來下接,務任程排的有所入加來別類
duling.timer.TimerFactoryBean
   :如 例 , 務 任 的 定 排 所行進 始 開 會 就 , 檔 義 定 完 取 讀 動 啟 器 容
Spring



22
Chapter 9 API   裝封

    TimerTaskDemo                                   TimerTaskDemo.java
package onlyfun.caterpillar;

import    java.io.BufferedReader;
import    java.io.IOException;
import    java.io.InputStreamReader;
import    java.util.Timer;
import    org.springframework.context.ApplicationContext;
import    org.springframework.context.
         support.ClassPathXmlApplicationContext;

public class TimerTaskDemo {
    public static void main(String[] args) throws IOException {
      ApplicationContext context =
            new ClassPathXmlApplicationContext("beans-config.xml");

          System.out.println("     Task..");
                                  動啟
          System.out.println("   入輸請 exit 閉關   Task: ");

          BufferedReader reader =
              new BufferedReader(
                      new InputStreamReader(System.in));

          while(true) {
              if(reader.readLine().equals("exit")) {
                  break;
              }
          }

          Timer timer = (Timer) context.getBean("timerFactoryBean");
          timer.cancel();
     }
}



後動啟在式程個這,容內的檔義定
           Bean   據根                               10   任次一第行執會秒
       。務任次一行執鐘分 每後之,務
             10




                                                                       23
Spring 2.0   良信林(冊手術技         – http://openhome.cc     )

9.3.2 使用 MethodInvokingTimerTaskFactoryBean

用使         Spring   承 繼 要 定 一 不 並,時    TimerTask   ,務 任 個 一 義 定 來   S pring
  供提     org.springframework.scheduling.timer.MethodInvokingTimerTask-
FactoryBean   下一寫改以可如例,法方的件物個某行執定指接直以可,
TimerTaskDemo :別類    承繼用不次這,別類   的中案專
                            DemoTask                       TimerTask


  TimerTaskDemo2                                            DemoTask.java
 package onlyfun.caterpillar;

 public class DemoTask {
     public void execute() {
         System.out.println("Task is executed.");
     }
 }



      在要只著接         Bean   用使中檔義定      MethodInvokingTimerTaskFactory-
Bean :如例,可即
  TimerTaskDemo2                                       beans-config.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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

       <bean id="demoTask" class="onlyfun.caterpillar.DemoTask"/>

       <bean id="timerTaskBean"
             class="org.springframework.scheduling.
                    →timer.MethodInvokingTimerTaskFactoryBean">
           <property name="targetObject" ref="demoTask"/>
           <property name="targetMethod" value="execute"/>
       </bean>




24
Chapter 9 API   裝封

<bean id="scheduledTimerTask"




                                                     2
Spring 2.0    良信林(冊手術技          – http://openhome.cc   )

             class="org.springframework.scheduling.
                     timer.ScheduledTimerTask">
                     →
           <property name="timerTask" ref="timerTaskBean"/>
           <property name="period" value="5000"/>
           <property name="delay" value="1000"/>
       </bean>

       <bean id="timerFactoryBean"
             class="org.springframework.scheduling.
                     timer.TimerFactoryBean">
                     →
           <property name="scheduledTimerTasks">
               <list>
                   <ref bean="scheduledTimerTask"/>
               </list>
           </property>
       </bean>

 </beans>



            用使接直以可時行執        TimerTaskDemo       的中案專 TimerTaskDemo   類
                     ,層底在,別                  立建動自會
              MethodInvokingTimerTaskFactoryBean              TimerTask
。 法 方 定 指 的 上件物 標 目 行 執 以 例 實 的

9.3.3 使用 Quartz

間之務任與務任定指能只,限有能功
      JDK           API    的供提所中 準標
                                 Timer
   用使以可您,務任行執時定來點間時個某定指法無,
          P eriod                  ) ( 週的
                                       期                          Quartz
,能功程排的多更了供提它,)
  h ttp://www.opensymphony.com/quartz/   (
       。 便 方 加更上 用 使 在 , 裝 封 了 行 進
     Spring         Quartz          對則   而
                                    承繼以可
                     org.springframework.scheduling.quartz.QuartzJobBean
              Job           :如例,別類 個一作實來


26
Chapter 9 API   裝封

    QuartzDemo                                              DemoJob.java
package onlyfun.caterpillar;

import org.quartz.JobExecutionContext;
import org.springframework.scheduling.
                           quartz.QuartzJobBean;

public class DemoJob extends QuartzJobBean {
    private JobData jobData;

     public void executeInternal(
                   JobExecutionContext context) {
         System.out.println(
                 jobData.getData() + " is executed.");
     }

     public void setJobData(JobData jobData) {
         this.jobData = jobData;
     }

     public JobData getJobData() {
         return jobData;
     }
}



執被 程排出看能了為,別類範示的件物料資 個一是只
     JobData         Job                                         Job
時回傳的料資需所時 行執示表,件物 個一回傳它,性期週的時行
                           Date                  Job
                          :如例,間
    QuartzDemo                                              JobData.java
package onlyfun.caterpillar;

import java.util.Date;

public class JobData {
    public String getData() {
        return "Data from "
               + new Date().toString();
    }
}




                                                                        27
Spring 2.0   良信林(冊手術技       – http://openhome.cc    )

     : 義定何 如 檔 義 定 看 來 接 直
  QuartzDemo                                        beans-config.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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

     <bean id="someData" class="onlyfun.caterpillar.JobData"/>

     <bean id="jobDetailBean"
           class="org.springframework.scheduling.
                        →  quartz.JobDetailBean">
         <property name="jobClass"
             value="onlyfun.caterpillar.DemoJob"/>
         <property name="jobDataAsMap">
             <map>
                 <entry key="jobData" value-ref="someData"/>
             </map>
         </property>
     </bean>

     <bean id="simpleTriggerBean"
           class="org.springframework.scheduling.
                        →  quartz.SimpleTriggerBean">
         <property name="jobDetail" ref="jobDetailBean"/>
         <property name="repeatInterval" value="5000"/>
         <property name="startDelay" value="1000"/>
     </bean>

     <bean id="schedulerFactoryBean"
           class="org.springframework.scheduling.
                        →  quartz.SchedulerFactoryBean">
         <property name="triggers">
             <list>
                 <ref bean="simpleTriggerBean"/>
             </list>
         </property>
     </bean>

 </beans>




28
Chapter 9 API    裝封

                        ,是的意注要別特中定設上以在 o rg.springframework.scheduling.
不而,稱名別類的 供提須必性屬
quartz.JobDetailBean               的
                          "jobClass"               Job
中性屬
  Job     Bean    在以可料資的需所 而,例實 的 是
                        Job                    "jobDataAsMap"
                                                。供提來
                         用使,上定指期週的務任程排在org.springframework.scheduling.
式方定指的中程排            與點這,定指來
quartz.SimpleTriggerBean                   TimerTask
是 的 用 使 所 , 時 定 排 而,位 單 為 作 秒 毫 以 是 也樣同 間 時 的 定 指 , 似 類
                                                    Job
    。
org.springframework.scheduling.quartz.SchedulerFactoryBean

,行進會就務任程排,後檔義定取讀並               動啟要只,後之定設成完
                              Spring
                          : 別 類 動 啟 務任的 單 簡 個 一 寫 撰 如 例
  QuartzDemo                                             QuartzDemo.java
package onlyfun.caterpillar;

import   java.io.BufferedReader;
import   java.io.IOException;
import   java.io.InputStreamReader;
import   org.quartz.Scheduler;
import   org.quartz.SchedulerException;
import   org.springframework.context.ApplicationContext;
import   org.springframework.context.
                support.ClassPathXmlApplicationContext;

public class QuartzDemo {
    public static void main(String[] args)
                    throws IOException, SchedulerException {
      ApplicationContext context =
            new ClassPathXmlApplicationContext("beans-config.xml");
        System.out.println("    Task..");
                                動啟
        System.out.println("   入輸請 exit    Task: ");
                                          閉關
         BufferedReader reader =
             new BufferedReader(
                     new InputStreamReader(System.in));



                                                                           2
Spring 2.0         良信林(冊手術技            – http://openhome.cc    )

                  while(true) {
                      if(reader.readLine().equals("exit")) {
                          break;
                      }
                  }

                  Scheduler scheduler =
                       (Scheduler) context.getBean("schedulerFactoryBean");
                  scheduler.shutdown();
           }
    }



執的務任個每到看以可中當,面畫考參個一的時行執式程個這看看來
                            :間時行




                              圖   9.6 QuartzDemo   果結行執的案專
期週的行執間之 與 的單簡作能只            用使
                   SimpleTriggerBean                 Job   Job
      用使以可則,定指的點間時作接直要果如,定指) (
        P eriod                                                     org.spring-
       :如例,
framework.scheduling.quartz.CronTriggerBean


        QuartzDemo2                                            beans-config.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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans




3
Chapter 9 API      裝封

  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="someData" class="onlyfun.caterpillar.JobData"/>

    <bean id="jobDetailBean"
          class="org.springframework.scheduling.
                    →  quartz.JobDetailBean">
        <property name="jobClass"
            value="onlyfun.caterpillar.DemoJob"/>
        <property name="jobDataAsMap">
            <map>
                <entry key="jobData" value-ref="someData"/>
            </map>
        </property>
    </bean>

    <bean id="cronTriggerBean"
          class="org.springframework.scheduling.
                    →  quartz.CronTriggerBean">
        <property name="jobDetail" ref="jobDetailBean"/>
        <property name="cronExpression" value="0 0 19 * * ?"/>
    </bean>

    <bean id="schedulerFactoryBean"
          class="org.springframework.scheduling.
                    →  quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTriggerBean"/>
            </list>
        </property>
    </bean>

</beans>



間時個六少至是式格的定指,定指的性屬        於在點重
             "cronExpression"
,次一 行執要時 的天每是定指的面上如例,素元間時個七多最,素元                19            Job
               :下如式格的定指性屬
"cronExpression"




                                                                       31
Spring 2.0        良信林(冊手術技                    – http://openhome.cc             )

             (秒
            0 -59   )
             (分
            0 -59   )
          ) (時小  0 -23

       ) (天幾第月每            1 -31

     )     或 (月
            1 -12        JAN-DEC

      或 (天幾第期星每                1 -7        SUN-SAT   )
        )    (年
            1 970-2099

一定設能只個兩,的斥互是」天幾第期星每「與」天幾第月每「中其
例 , 號 符 用 使 以 可 , 點 間時個 幾 好 有 果 如 , 寫 撰 號 符 以 的 定 設 不 , 個
                           ?                                                          ,
對; 行執要時 、時 、時 的天每示表」
        0 0 10,12,14 * * ?                          「:如  10          12       14          Job
的月每示表」                    「 如 例 , 號 符 用使 以 可 間 時 的 續 連 於
                                       -                 0 0 10,12,14 1-15 * ?
可 有可定 指 年 的 中 式 格 間 時 , 行 執 要 時 、 時 、 時 每 日 到
1     15            10           12          14               Job
、 時 的一期星每年 示表」       0 0 10,12,14 ? * MON 2006「:如例,無                  2006                10
12          14                   Job 。 行執要時 、時

9.3.4 使用 MethodInvokingJobDetailFactoryBean

             承繼要定一不並,話的    用使果如
                         Spring                                     QuartzJobBean   一義定來別類
      Job          供提     ,別類 個
                           Spring                 org.springframework.scheduling.quartz.
的件物個某行執定指接直以可,
MethodInvokingJobDetailFactoryBean
      :別類    的中案專       下一寫改如例,法方  QuartzDemo                       DemoJob


     QuartzDemo3                                                                    DemoJob.java
 package onlyfun.caterpillar;

 public class DemoJob {
     private JobData jobData;

       public void execute() {
           System.out.println(



32
Chapter 9 API   裝封

                  jobData.getData() + " is executed.");
     }

     public void setJobData(JobData jobData) {
         this.jobData = jobData;
     }

     public JobData getJobData() {
         return jobData;
     }
}



   用使中檔義定 在要只著接Bean                  MethodInvokingJobDetailFactoryBean
的中案專     下一寫改如例,可即    QuartzDemo            beans-config.xml   :
    QuartzDemo3                                        beans-config.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

     <bean id="someData" class="onlyfun.caterpillar.JobData"/>

     <bean id="someJob"
           class="onlyfun.caterpillar.DemoJob">
         <property name="jobData" ref="someData"/>
     </bean>

     <bean id="jobDetailBean"
           class="org.springframework.scheduling.
                    →  quartz.MethodInvokingJobDetailFactoryBean">
         <property name="targetObject" ref="someJob"/>
         <property name="targetMethod" value="execute"/>
     </bean>

     <bean id="cronTriggerBean"
           class="org.springframework.scheduling.
                    →  quartz.CronTriggerBean">
         <property name="jobDetail" ref="jobDetailBean"/>




                                                                       33
Spring 2.0   良信林(冊手術技       – http://openhome.cc       )

         <property name="cronExpression" value="0 30 18 * * ?"/>
     </bean>

     <bean id="schedulerFactoryBean"
           class="org.springframework.scheduling.
                    →  quartz.SchedulerFactoryBean">
         <property name="triggers">
             <list>
                 <ref bean="cronTriggerBean"/>
             </list>
         </property>
     </bean>

 </beans>



,層 底 在 , 務 任 的 定 指 行 執 會 都 分 時 的天每 , 中 定 指 的 面 上 在
                             18   30
執以,例實的             立建動自會
MethodInvokingJobDetailFactoryBean              JobDetail
                                 。法方 定 指 的 件 物 標 目 行




34
3
                         感快發開的   Spring   驗體   9.7   圖
。 手上快 很 以 可 都 能 功 它 其 的     Spring
                        於 對 信 相 , 照對的 件 文 考 參 上 加 礎 基
的書本了有,件文考參的供提所                   Spring
                          考參以可您議建,紹介加詳都個一
每法無,限有圍範的獵涉所人個與幅篇唯,多許有還能功的                      於關上      Spring
實事,落段一告先紹介的能功別個                    Spring
                            於對書本,止為節章個這到
                                                9.4 接下來的主題
    裝封   Chapter 9 API

More Related Content

What's hot

Spring 2.0 技術手冊第四章 - Spring AOP
Spring 2.0 技術手冊第四章 - Spring AOPSpring 2.0 技術手冊第四章 - Spring AOP
Spring 2.0 技術手冊第四章 - Spring AOPJustin Lin
 
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 ServletServlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 ServletJustin Lin
 
Spring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 SpringSpring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 SpringJustin Lin
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记yiditushe
 
Spring 2.0 技術手冊第三章 - IoC 容器
Spring 2.0 技術手冊第三章 - IoC 容器Spring 2.0 技術手冊第三章 - IoC 容器
Spring 2.0 技術手冊第三章 - IoC 容器Justin Lin
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing江華 奚
 
Spring中的object xml映射详解
Spring中的object xml映射详解Spring中的object xml映射详解
Spring中的object xml映射详解leeley2000
 
Servlet & JSP 教學手冊第二版 - 第 5 章:Servlet 進階 API、過濾器與傾聽器
Servlet & JSP 教學手冊第二版 - 第 5 章:Servlet 進階 API、過濾器與傾聽器Servlet & JSP 教學手冊第二版 - 第 5 章:Servlet 進階 API、過濾器與傾聽器
Servlet & JSP 教學手冊第二版 - 第 5 章:Servlet 進階 API、過濾器與傾聽器Justin Lin
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程appollo0312
 
J2ee面试知识
J2ee面试知识J2ee面试知识
J2ee面试知识yiditushe
 
Spring入门纲要
Spring入门纲要Spring入门纲要
Spring入门纲要yiditushe
 
Rest Ruby On Rails
Rest Ruby On RailsRest Ruby On Rails
Rest Ruby On Railsshaokun
 
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSPServlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSPJustin Lin
 
OpenEJB - 另一個選擇
OpenEJB - 另一個選擇OpenEJB - 另一個選擇
OpenEJB - 另一個選擇Justin Lin
 
中远公司 Java培训资料
中远公司  Java培训资料中远公司  Java培训资料
中远公司 Java培训资料yiditushe
 
六步教你学会简单Rmi
六步教你学会简单Rmi六步教你学会简单Rmi
六步教你学会简单Rmiyiditushe
 
Java SE 7 技術手冊 - 課後練習解答
Java SE 7 技術手冊 - 課後練習解答Java SE 7 技術手冊 - 課後練習解答
Java SE 7 技術手冊 - 課後練習解答Justin Lin
 
Apache服务器配置全攻略
Apache服务器配置全攻略Apache服务器配置全攻略
Apache服务器配置全攻略Yiwei Ma
 
Struts快速学习指南
Struts快速学习指南Struts快速学习指南
Struts快速学习指南yiditushe
 

What's hot (20)

Spring 2.0 技術手冊第四章 - Spring AOP
Spring 2.0 技術手冊第四章 - Spring AOPSpring 2.0 技術手冊第四章 - Spring AOP
Spring 2.0 技術手冊第四章 - Spring AOP
 
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 ServletServlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
 
Spring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 SpringSpring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 Spring
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记
 
Spring 2.0 技術手冊第三章 - IoC 容器
Spring 2.0 技術手冊第三章 - IoC 容器Spring 2.0 技術手冊第三章 - IoC 容器
Spring 2.0 技術手冊第三章 - IoC 容器
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
 
Spring中的object xml映射详解
Spring中的object xml映射详解Spring中的object xml映射详解
Spring中的object xml映射详解
 
Servlet & JSP 教學手冊第二版 - 第 5 章:Servlet 進階 API、過濾器與傾聽器
Servlet & JSP 教學手冊第二版 - 第 5 章:Servlet 進階 API、過濾器與傾聽器Servlet & JSP 教學手冊第二版 - 第 5 章:Servlet 進階 API、過濾器與傾聽器
Servlet & JSP 教學手冊第二版 - 第 5 章:Servlet 進階 API、過濾器與傾聽器
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程
 
J2ee面试知识
J2ee面试知识J2ee面试知识
J2ee面试知识
 
Spring入门纲要
Spring入门纲要Spring入门纲要
Spring入门纲要
 
Rest Ruby On Rails
Rest Ruby On RailsRest Ruby On Rails
Rest Ruby On Rails
 
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSPServlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
Servlet & JSP 教學手冊第二版 - 第 6 章:使用 JSP
 
OpenEJB - 另一個選擇
OpenEJB - 另一個選擇OpenEJB - 另一個選擇
OpenEJB - 另一個選擇
 
Soap
SoapSoap
Soap
 
中远公司 Java培训资料
中远公司  Java培训资料中远公司  Java培训资料
中远公司 Java培训资料
 
六步教你学会简单Rmi
六步教你学会简单Rmi六步教你学会简单Rmi
六步教你学会简单Rmi
 
Java SE 7 技術手冊 - 課後練習解答
Java SE 7 技術手冊 - 課後練習解答Java SE 7 技術手冊 - 課後練習解答
Java SE 7 技術手冊 - 課後練習解答
 
Apache服务器配置全攻略
Apache服务器配置全攻略Apache服务器配置全攻略
Apache服务器配置全攻略
 
Struts快速学习指南
Struts快速学习指南Struts快速学习指南
Struts快速学习指南
 

Viewers also liked

Spring 2.0 技術手冊目錄
Spring 2.0 技術手冊目錄Spring 2.0 技術手冊目錄
Spring 2.0 技術手冊目錄Justin Lin
 
Spring 2.0 技術手冊再版序
Spring 2.0 技術手冊再版序Spring 2.0 技術手冊再版序
Spring 2.0 技術手冊再版序Justin Lin
 
Spring 2.0 技術手冊書名頁
Spring 2.0 技術手冊書名頁Spring 2.0 技術手冊書名頁
Spring 2.0 技術手冊書名頁Justin Lin
 
Servlet & JSP 教學手冊第二版 - 第 11 章:簡介 JavaMail
Servlet & JSP 教學手冊第二版 - 第 11 章:簡介 JavaMailServlet & JSP 教學手冊第二版 - 第 11 章:簡介 JavaMail
Servlet & JSP 教學手冊第二版 - 第 11 章:簡介 JavaMailJustin Lin
 
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫Justin Lin
 
Servlet & JSP 教學手冊第二版 - 第 8 章:自訂標籤
Servlet & JSP 教學手冊第二版 - 第 8 章:自訂標籤Servlet & JSP 教學手冊第二版 - 第 8 章:自訂標籤
Servlet & JSP 教學手冊第二版 - 第 8 章:自訂標籤Justin Lin
 
Servlet & JSP 教學手冊第二版 - 第 10 章:Web 容器安全管理
Servlet & JSP 教學手冊第二版 - 第 10 章:Web 容器安全管理Servlet & JSP 教學手冊第二版 - 第 10 章:Web 容器安全管理
Servlet & JSP 教學手冊第二版 - 第 10 章:Web 容器安全管理Justin Lin
 
Servlet & JSP 教學手冊第二版 - 第 12 章:從模式到框架
Servlet & JSP 教學手冊第二版 - 第 12 章:從模式到框架Servlet & JSP 教學手冊第二版 - 第 12 章:從模式到框架
Servlet & JSP 教學手冊第二版 - 第 12 章:從模式到框架Justin Lin
 
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理Justin Lin
 
Servlet & JSP 教學手冊第二版 - 第 2 章:撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版 - 第 2 章:撰寫與設定 ServletServlet & JSP 教學手冊第二版 - 第 2 章:撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版 - 第 2 章:撰寫與設定 ServletJustin Lin
 
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應Justin Lin
 
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式Justin Lin
 
類別的繼承
類別的繼承類別的繼承
類別的繼承Justin Lin
 
除錯、測試與效能
除錯、測試與效能除錯、測試與效能
除錯、測試與效能Justin Lin
 
並行與平行
並行與平行並行與平行
並行與平行Justin Lin
 
Python 起步走
Python 起步走Python 起步走
Python 起步走Justin Lin
 

Viewers also liked (17)

Spring 2.0 技術手冊目錄
Spring 2.0 技術手冊目錄Spring 2.0 技術手冊目錄
Spring 2.0 技術手冊目錄
 
Spring 2.0 技術手冊再版序
Spring 2.0 技術手冊再版序Spring 2.0 技術手冊再版序
Spring 2.0 技術手冊再版序
 
Spring 2.0 技術手冊書名頁
Spring 2.0 技術手冊書名頁Spring 2.0 技術手冊書名頁
Spring 2.0 技術手冊書名頁
 
進階主題
進階主題進階主題
進階主題
 
Servlet & JSP 教學手冊第二版 - 第 11 章:簡介 JavaMail
Servlet & JSP 教學手冊第二版 - 第 11 章:簡介 JavaMailServlet & JSP 教學手冊第二版 - 第 11 章:簡介 JavaMail
Servlet & JSP 教學手冊第二版 - 第 11 章:簡介 JavaMail
 
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
Servlet & JSP 教學手冊第二版 - 第 9 章:整合資料庫
 
Servlet & JSP 教學手冊第二版 - 第 8 章:自訂標籤
Servlet & JSP 教學手冊第二版 - 第 8 章:自訂標籤Servlet & JSP 教學手冊第二版 - 第 8 章:自訂標籤
Servlet & JSP 教學手冊第二版 - 第 8 章:自訂標籤
 
Servlet & JSP 教學手冊第二版 - 第 10 章:Web 容器安全管理
Servlet & JSP 教學手冊第二版 - 第 10 章:Web 容器安全管理Servlet & JSP 教學手冊第二版 - 第 10 章:Web 容器安全管理
Servlet & JSP 教學手冊第二版 - 第 10 章:Web 容器安全管理
 
Servlet & JSP 教學手冊第二版 - 第 12 章:從模式到框架
Servlet & JSP 教學手冊第二版 - 第 12 章:從模式到框架Servlet & JSP 教學手冊第二版 - 第 12 章:從模式到框架
Servlet & JSP 教學手冊第二版 - 第 12 章:從模式到框架
 
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
Servlet & JSP 教學手冊第二版 - 第 4 章:會話管理
 
Servlet & JSP 教學手冊第二版 - 第 2 章:撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版 - 第 2 章:撰寫與設定 ServletServlet & JSP 教學手冊第二版 - 第 2 章:撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版 - 第 2 章:撰寫與設定 Servlet
 
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
Servlet & JSP 教學手冊第二版 - 第 3 章:請求與回應
 
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
Servlet & JSP 教學手冊第二版 - 第 1 章:簡介Web應用程式
 
類別的繼承
類別的繼承類別的繼承
類別的繼承
 
除錯、測試與效能
除錯、測試與效能除錯、測試與效能
除錯、測試與效能
 
並行與平行
並行與平行並行與平行
並行與平行
 
Python 起步走
Python 起步走Python 起步走
Python 起步走
 

Similar to Spring 2.0 技術手冊第九章 - API 封裝

用JAX-RS和Jersey完成RESTful Web Services
用JAX-RS和Jersey完成RESTful Web Services用JAX-RS和Jersey完成RESTful Web Services
用JAX-RS和Jersey完成RESTful Web Servicesjavatwo2011
 
用 Ruby 開發 IoT 應用 - 以 RubyConf.tw 打卡系統為例
用 Ruby 開發 IoT 應用 - 以 RubyConf.tw 打卡系統為例用 Ruby 開發 IoT 應用 - 以 RubyConf.tw 打卡系統為例
用 Ruby 開發 IoT 應用 - 以 RubyConf.tw 打卡系統為例亮齊 曾
 
高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕ideawu
 
Rpc原理与实现
Rpc原理与实现Rpc原理与实现
Rpc原理与实现wavefly
 
Alibaba Service Framework Practice
Alibaba Service Framework  PracticeAlibaba Service Framework  Practice
Alibaba Service Framework PracticeShawn Qian
 
异步编程与浏览器执行模型
异步编程与浏览器执行模型异步编程与浏览器执行模型
异步编程与浏览器执行模型keelii
 
使用NodeJS构建静态资源管理系统
使用NodeJS构建静态资源管理系统使用NodeJS构建静态资源管理系统
使用NodeJS构建静态资源管理系统Frank Xu
 
Lamp高性能设计
Lamp高性能设计Lamp高性能设计
Lamp高性能设计锐 张
 
Python xmlrpc-odoo
Python xmlrpc-odooPython xmlrpc-odoo
Python xmlrpc-odoorobin yang
 
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型裕波 周
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析Justin Lin
 
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile ServicesKuo-Chun Su
 
Java华为面试题
Java华为面试题Java华为面试题
Java华为面试题yiditushe
 
Ejb工作原理学习笔记
Ejb工作原理学习笔记Ejb工作原理学习笔记
Ejb工作原理学习笔记yiditushe
 
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型Jackson Tian
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践yiditushe
 
test
testtest
testxieyq
 

Similar to Spring 2.0 技術手冊第九章 - API 封裝 (20)

用JAX-RS和Jersey完成RESTful Web Services
用JAX-RS和Jersey完成RESTful Web Services用JAX-RS和Jersey完成RESTful Web Services
用JAX-RS和Jersey完成RESTful Web Services
 
Exodus2 大局观
Exodus2 大局观Exodus2 大局观
Exodus2 大局观
 
用 Ruby 開發 IoT 應用 - 以 RubyConf.tw 打卡系統為例
用 Ruby 開發 IoT 應用 - 以 RubyConf.tw 打卡系統為例用 Ruby 開發 IoT 應用 - 以 RubyConf.tw 打卡系統為例
用 Ruby 開發 IoT 應用 - 以 RubyConf.tw 打卡系統為例
 
高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕
 
Rpc原理与实现
Rpc原理与实现Rpc原理与实现
Rpc原理与实现
 
Alibaba Service Framework Practice
Alibaba Service Framework  PracticeAlibaba Service Framework  Practice
Alibaba Service Framework Practice
 
异步编程与浏览器执行模型
异步编程与浏览器执行模型异步编程与浏览器执行模型
异步编程与浏览器执行模型
 
使用NodeJS构建静态资源管理系统
使用NodeJS构建静态资源管理系统使用NodeJS构建静态资源管理系统
使用NodeJS构建静态资源管理系统
 
Lamp高性能设计
Lamp高性能设计Lamp高性能设计
Lamp高性能设计
 
Python xmlrpc-odoo
Python xmlrpc-odooPython xmlrpc-odoo
Python xmlrpc-odoo
 
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
 
深入淺出 Web 容器 - Tomcat 原始碼分析
深入淺出 Web 容器  - Tomcat 原始碼分析深入淺出 Web 容器  - Tomcat 原始碼分析
深入淺出 Web 容器 - Tomcat 原始碼分析
 
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
 
Asp net原理
Asp net原理Asp net原理
Asp net原理
 
Java华为面试题
Java华为面试题Java华为面试题
Java华为面试题
 
Ejb工作原理学习笔记
Ejb工作原理学习笔记Ejb工作原理学习笔记
Ejb工作原理学习笔记
 
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
 
Osgi Intro
Osgi IntroOsgi Intro
Osgi Intro
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践
 
test
testtest
test
 

More from Justin Lin

Ch14 簡介 Spring Boot
Ch14 簡介 Spring BootCh14 簡介 Spring Boot
Ch14 簡介 Spring BootJustin Lin
 
Ch13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityCh13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityJustin Lin
 
Ch12 Spring 起步走
Ch12 Spring 起步走Ch12 Spring 起步走
Ch12 Spring 起步走Justin Lin
 
Ch11 簡介 JavaMail
Ch11 簡介 JavaMailCh11 簡介 JavaMail
Ch11 簡介 JavaMailJustin Lin
 
Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Justin Lin
 
Ch09 整合資料庫
Ch09 整合資料庫Ch09 整合資料庫
Ch09 整合資料庫Justin Lin
 
Ch08 自訂標籤
Ch08 自訂標籤Ch08 自訂標籤
Ch08 自訂標籤Justin Lin
 
Ch07 使用 JSTL
Ch07 使用 JSTLCh07 使用 JSTL
Ch07 使用 JSTLJustin Lin
 
Ch06 使用 JSP
Ch06 使用 JSPCh06 使用 JSP
Ch06 使用 JSPJustin Lin
 
Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Justin Lin
 
Ch04 會話管理
Ch04 會話管理Ch04 會話管理
Ch04 會話管理Justin Lin
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應Justin Lin
 
Ch02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletCh02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletJustin Lin
 
CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式Justin Lin
 
14. 進階主題
14. 進階主題14. 進階主題
14. 進階主題Justin Lin
 
13.並行、平行與非同步
13.並行、平行與非同步13.並行、平行與非同步
13.並行、平行與非同步Justin Lin
 
12. 除錯、測試與效能
12. 除錯、測試與效能12. 除錯、測試與效能
12. 除錯、測試與效能Justin Lin
 
11. 常用內建模組
11. 常用內建模組11. 常用內建模組
11. 常用內建模組Justin Lin
 
10. 資料永續與交換
10. 資料永續與交換10. 資料永續與交換
10. 資料永續與交換Justin Lin
 
9. 資料結構
9. 資料結構9. 資料結構
9. 資料結構Justin Lin
 

More from Justin Lin (20)

Ch14 簡介 Spring Boot
Ch14 簡介 Spring BootCh14 簡介 Spring Boot
Ch14 簡介 Spring Boot
 
Ch13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/SecurityCh13 整合 Spring MVC/Security
Ch13 整合 Spring MVC/Security
 
Ch12 Spring 起步走
Ch12 Spring 起步走Ch12 Spring 起步走
Ch12 Spring 起步走
 
Ch11 簡介 JavaMail
Ch11 簡介 JavaMailCh11 簡介 JavaMail
Ch11 簡介 JavaMail
 
Ch10 Web 容器安全管理
Ch10 Web 容器安全管理Ch10 Web 容器安全管理
Ch10 Web 容器安全管理
 
Ch09 整合資料庫
Ch09 整合資料庫Ch09 整合資料庫
Ch09 整合資料庫
 
Ch08 自訂標籤
Ch08 自訂標籤Ch08 自訂標籤
Ch08 自訂標籤
 
Ch07 使用 JSTL
Ch07 使用 JSTLCh07 使用 JSTL
Ch07 使用 JSTL
 
Ch06 使用 JSP
Ch06 使用 JSPCh06 使用 JSP
Ch06 使用 JSP
 
Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器Ch05 Servlet 進階 API、過濾器與傾聽器
Ch05 Servlet 進階 API、過濾器與傾聽器
 
Ch04 會話管理
Ch04 會話管理Ch04 會話管理
Ch04 會話管理
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應
 
Ch02 撰寫與設定 Servlet
Ch02 撰寫與設定 ServletCh02 撰寫與設定 Servlet
Ch02 撰寫與設定 Servlet
 
CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式CH1. 簡介 Web 應用程式
CH1. 簡介 Web 應用程式
 
14. 進階主題
14. 進階主題14. 進階主題
14. 進階主題
 
13.並行、平行與非同步
13.並行、平行與非同步13.並行、平行與非同步
13.並行、平行與非同步
 
12. 除錯、測試與效能
12. 除錯、測試與效能12. 除錯、測試與效能
12. 除錯、測試與效能
 
11. 常用內建模組
11. 常用內建模組11. 常用內建模組
11. 常用內建模組
 
10. 資料永續與交換
10. 資料永續與交換10. 資料永續與交換
10. 資料永續與交換
 
9. 資料結構
9. 資料結構9. 資料結構
9. 資料結構
 

Spring 2.0 技術手冊第九章 - API 封裝

  • 1. 。式方用使 的中 Spring 在等務服程排與務服件郵、務服程遠紹介將中節章個這 。單簡加更時務服件元或 些一用 API 使 在 您 讓,式 方 的 致 一 且 化 簡 了 供 提,下 裝 封 的 樣 這 在,裝 封 象 抽 層 一 供 提 是 而,案 方 決 解 的 接 直 供 提 不 , 務服業企些一於對 API S pring API 封裝 9
  • 2. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) 9.1 遠程 務服地本用使同如就,時務服端服伺用使在件物的端地本讓務服程遠 ,術技程遠的同不種各於對,節細等結連路網到觸接需不端戶客,樣一 相不各術技務服程遠的用採所層底使即,式方用使的致一了供提 Spring 下一紹介來將節小個這,的致一是卻式方的們它用運中 Spring 在,同 RMI 。用使的 H essian 及以 、 B urlap 、 Http Invoker 9.1.1 RMI , 的現出就始開 RMI 從是) ( R emote Method Invocation JDK 1.1 API 然,樣一件物地本用使同如就,時務服的供提件物端遠用使在端戶客讓它 承繼須必時義定在面介務服是像,續手的串連一有須必時用使在 RMI 而 承繼須必時作實在 java.rmi.Remote 務服、面介 Server java.rmi.Unicast- 上定設,等 與 生產令指 用使須必、別類 RemoteObject rmic stub skeleton 。雜繁續手的 過透中 在以可您 Spring org.springframework.remoting.rmi.Rmi- 在 解了,子例看看際實來,續手的 用使化簡 ServiceExporter RMI Spring 務服個一義定後然,寫撰的端服伺 下一看先首,化簡與用使的上 RMI RMI :面介的件物 RMIServerDemo ISomeService.java package onlyfun.caterpillar; public interface ISomeService { public String doSomeService(String some); public int doOtherService(int other); } 2
  • 3. Chapter 9 API 裝封 承繼用 不面介的件物 務服 j ava.rmi.Remote作實在而,面介 I SomeService 承繼用不也時 java.rmi.UnicastRemoteObject :如例,別類 RMIServerDemo SomeServiceImpl.java package onlyfun.caterpillar; public class SomeServiceImpl implements ISomeService { public String doSomeService(String some) { return some + " is processed"; } public int doOtherService(int other) { return ++other; } } 的過改修經已個一回傳是只都法方個兩,範示的單簡個是只作實個這 , 例 實 成 生、理 管 讓,義定中檔義定 在要只來下接,值 Bean Spring Bean :如例,務服 動啟、冊註可即此如 RMI RMIServerDemo rmi-server.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="someService" class="onlyfun.caterpillar.SomeServiceImpl"/> <bean id="serviceExporter" class="org.springframework.remoting. → rmi.RmiServiceExporter"> <property name="service" ref="someService"/> <property name="serviceName" value="SomeService"/> <property name="serviceInterface" value="onlyfun.caterpillar.ISomeService"/> 3
  • 4. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) </bean> </beans> 訴告要只 org.springframework.remoting.rmi.RmiServiceExporter ) 為定設上性屬 在意注(稱名、件物務服 "serviceName" "SomeService" 務服 , 後 例 實 成 生 並 檔 義 定 取 讀 後之 , 面 介 的 理 代 要 與 Spring Bean R MI :務服 動啟以,別類 的 單 簡個一 寫 撰 來 , 動 啟 會 就 RMIServer RMI RMIServerDemo RMIServer.java package onlyfun.caterpillar; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.springframework.context.ApplicationContext; import org.springframework.context. support.ClassPathXmlApplicationContext; import org.springframework.remoting.rmi.RmiServiceExporter; public class RMIServer { public static void main(String[] args) throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("rmi-server.xml"); System.out.println(" 動啟RMI Server.."); System.out.println(" 入輸請 exit 閉關Server: "); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while(true) { if(reader.readLine().equals("exit")) { break; } } 4
  • 5. Chapter 9 API 裝封 RmiServiceExporter rmiServiceExporter = (RmiServiceExporter) context.getBean("serviceExporter"); rmiServiceExporter.destroy(); } } 一另用使動自會 , 動 啟 會 就 務 服 ,後 之 式 程 的 面 上 行 運 在 R MI S pring 入輸以可您,題問理處的緒行執心關用不以所,務服 行執來緒行執個 RMI 向以端戶客 個一作實何如,下一看來著接,式程開離接直 "exit" RMI RMI 是就也,面介的象抽於賴依是端戶客,是的得記要先首,務服求要器服伺 。份一有端戶客在須必也檔 之面介 ISomeService .class 的前先 過透要只,時務服 要需端戶客在 R MI o rg.springframework.remoting. 的定設前先至應對( 的務服知告並, rmi.RmiProxyFactoryBean URL 地本用使在像好就時式程寫撰在,可即面介的理代、)稱名 "SomeService" : 寫 撰 下 如 以 可 檔 義 定 如例, 樣 一 務 服 的 理 管 端 Bean RMIClientDemo rmi-client.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="someServiceProxy" class="org.springframework.remoting. rmi.RmiProxyFactoryBean"> → <property name="serviceUrl" value="rmi://localhost/SomeService"/> <property name="serviceInterface" value="onlyfun.caterpillar.ISomeService"/> </bean> </beans>
  • 6. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) 伺定指著接,頭開 以是它,定設的性屬 到意注 "serviceUrl" "rmi://" 的上器服伺 用使以式程端戶客的單簡個寫撰來,稱名務服與址位器服 RMI :務服 RMIClientDemo RMIClient.java package onlyfun.caterpillar; import org.springframework.context.ApplicationContext; import org.springframework.context. support.ClassPathXmlApplicationContext; public class RMIClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "rmi-client.xml"); ISomeService service = (ISomeService) context.getBean("someServiceProxy"); String result1 = service.doSomeService("Some request"); System.out.println(result1); int result2 = service.doOtherService(1); System.out.println(result2); } } 理代,節細種種的結連務服關有到理處要需不全完,中作實的式程在 得取在正到意注會不本根,看來式程從單,節細些這成完您幫動自會件物 :示 所 下 如 果 結 的 行 執,務 服 的 上 器 服 伺 端 遠 圖 9.1 RMIClientDemo 果結行執的案專 6
  • 7. Chapter 9 API 裝封 9.1.2 Hessian、Burlap Hessian B urlap 由是 、 Caucho Technology ( h ttp://www.caucho.com/ ) HTTP 。務服程遠的現實 過透,出提所 若不而,送傳行進 Hessian 用使 息 訊 位 進 二 的 性 中 以 件 物 將 是 HTTP 某於限受不此因,的性中是息訊位進二於由,式格化列序的 用使 RMI Java 較寬頻的需所時輸傳在料資位進二,端服伺或端戶客的現實所言語式程種 。點優其是小 的高較有且件文 ,送傳行進式格件文 以件物將是則 Burlap XML X ML 當 ,息訊 的 收 接 所 讀 解 以 可 就 件 文 析 剖 以 可式程 用 應 要 只 , 性 讀 可 XML 。 端服伺 與 端 戶 客 的 現 實 所言語 種 某 於 限 受 不 也 然 、 於由,的似類是法方的 Spring 及 Hessian 用使中 Burlap 在 Hessian 就也,架框 Burlap 配搭要 時 用 使 在 以 所 , 送 傳 HTTP 過透是 Spring Web 以, 用使何如範示來子例的際實個舉, DispatcherServlet 用使是 Hessian 、 9.1.1 的好寫撰已用使接直以可,說來子 例 的紹介中 RMI ISomeService : SomeServiceImpl 置配中 在要端服伺而, web.xml DispatcherServlet HessianServerDemo web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <session-config> <session-timeout> 30 </session-timeout> </session-config> <servlet> <servlet-name>dispatcherServlet</servlet-name> 7
  • 8. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/service-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.service</url-pattern> </servlet-mapping> </web-app> 在 Hessian 用使,邊這端服伺的 org.springframework.remoting.cau- :務服佈發來 cho.HessianServiceExporter HessianServerDemo service-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="urlMapping" class="org.springframework.web.servlet. → handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/some.service">serviceExporter</prop> </props> </property> </bean> <bean id="someService" class="onlyfun.caterpillar.SomeServiceImpl"/> <bean id="serviceExporter" 8
  • 9. Chapter 9 API 裝封 class="org.springframework.remoting. caucho.HessianServiceExporter"> → <property name="service" ref="someService"/> <property name="serviceInterface" value="onlyfun.caterpillar.ISomeService"/> </bean> </beans> 求請,上置設的 在到意注 SimpleUrlHandlerMapping " some.service" 有沒是就也,稱名務服冊註用不邊這在, " serviceExporter" 給配分會的 則, 是的用使果如,要需不 "serviceName" 、 ,性屬 H essian B urlap Burlap 用改要只性屬 的 "serviceExporter" 在上定設 "class" org.spring- 。可即別類 framework. remoting.caucho.BurlapServiceExporter ,後之式程用應 的計設上以入載,器容 動啟要只來下接 Servlet Web 的需所 入加中錄目 在要得記,了動啟會就端服伺 Hessian 則 lib Hessian 目 API 下錄目 中案檔載下 .jar 用使以可這,案檔 之別類 Spring lib caucho 入加得記要,話的 用使果如,案檔 hessian-3.0.20.jar 的錄 Burlap Burlap API .jar 。案檔 的需所 來案專 Hessian的寫撰前先用使以可則寫撰的端戶客 RMIClientDemo : 如 例 , 可 即 檔 義 定 下 一改修 要 只 上 實 事 , 寫 改 Bean HessianClientDemo hessian-client.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="someServiceProxy" class="org.springframework.remoting. caucho.HessianProxyFactoryBean"> → <property name="serviceUrl" value="http://localhost:8080/
  • 10. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) → HessianServerDemo/some.service"/> <property name="serviceInterface" value="onlyfun.caterpillar.ISomeService"/> </bean> </beans> 的準標 個 是 它 , 定 設 的 性 屬 "serviceUrl" 到意注 HTTP 來,址位求請 :務服的上器服伺 用 使 以 式程端 戶 客 的 單 簡 個 寫 撰 Hessian HessianClientDemo HessianClient.java package onlyfun.caterpillar; import org.springframework.context.ApplicationContext; import org.springframework.context. support.ClassPathXmlApplicationContext; public class HessianClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "hessian-client.xml"); ISomeService service = (ISomeService) context.getBean("someServiceProxy"); String result1 = service.doSomeService("Some request"); System.out.println(result1); int result2 = service.doOtherService(1); System.out.println(result2); } } 果結的行執,案檔 的 括 包 入加要 , 得 記 須 必 的 樣 同 Hessian API .jar 用 使 要 果 如 , 面 畫 的 圖考參 以 可 , 的 同 相 是 案 專 RMIClientDemo 與 9.1 Burlap 用改要只上定設則, org.springframework.remoting.caucho. 1
  • 11. Chapter 9 API 裝封 BurlapProxyFactoryBean 入加須必得記要然當,可即 Burlap API 需所 的 .jar 。案檔 9.1.3 Http Invoker 來制機化列序的 用使時送傳,件物送傳 Http Invoker 用使HTTP Java 來架框 配搭要時們它用使在以所,送傳 HTTP 過透於由,送傳 Spring Web 的 寫 改 以 可, 到 用 使 是 就 也,用 使 DispatcherServlet 9.1.2 HessianServer- Demo :了以可就 下一改修要只,案專 service-config.xml HttpInvokerDemo service-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="urlMapping" class="org.springframework.web.servlet. → handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/some.service">serviceExporter</prop> </props> </property> </bean> <bean id="someService" class="onlyfun.caterpillar.SomeServiceImpl"/> <bean id="serviceExporter" class="org.springframework.remoting. → httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="someService"/> <property name="serviceInterface" value="onlyfun.caterpillar.ISomeService"/> </bean> 11
  • 12. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) </beans> 寫 改 以 可,份 部 的 端 戶 客 來 下 接 HessianClientDemo 修,容內的案專 :容內的檔義定 下一改 Bean InvokerClientDemo invoker-client.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="someServiceProxy" class="org.springframework.remoting. → httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/ → HttpInvokerDemo/some.service"/> <property name="serviceInterface" value="onlyfun.caterpillar.ISomeService"/> </bean> </beans> 撰來,址位求請 的準 標 是 它 , 定 設 的 性 屬 "serviceUrl" 到意注 HTTP :務服的上器服伺 用 使以式 程 端 戶 客 的 單 簡 寫 Http Invoker InvokerClientDemo InvokerClient.java package onlyfun.caterpillar; import org.springframework.context.ApplicationContext; import org.springframework.context. support.ClassPathXmlApplicationContext; public class InvokerClient { public static void main(String[] args) { ApplicationContext context = 12
  • 13. Chapter 9 API 裝封 new ClassPathXmlApplicationContext( "invoker-client.xml"); ISomeService service = (ISomeService) context.getBean("someServiceProxy"); String result1 = service.doSomeService("Some request"); System.out.println(result1); int result2 = service.doOtherService(1); System.out.println(result2); } } 與果結的行執 RMIClientDemo 。面畫的 圖考參以可,的同相是案專 9.1 13
  • 14. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) 9.2 郵件 的 由 是 援支的 務 服 送 發 件 郵 於 對 Spring org.springframework.mail. MailSender :別類作實個兩有它,義定所面介 o rg.springframework.mail. cos.CosMailSenderImpl 與 org.springframework.mail.javamail.Java- 的它在( MailSenderImpl 對為者前,Jason Hunter's COS Java Servlet 。援支之 Programming 對為 者 後 , 援 支 之 ) 的 供 提 中 書 Java Mail 9.2.1 簡單郵件 服件郵 供提何如 範示,用使的 以將邊這在 Java Mail Spring Java Mail 範示的式程,作實何如送傳之件郵字文純的單簡最個看來先首,裝封的務 :下如 SimpleMailDemo SimpleMailDemo.java package onlyfun.caterpillar; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.SimpleMailMessage; public class SimpleMailDemo { public static void main(String[] args) throws Exception { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 定設 Mail Server senderImpl.setHost("your_mail_server.com"); // 息訊件郵立建 SimpleMailMessage mailMessage = new SimpleMailMessage(); // 文內與題主、人件寄、人件收定設 mailMessage.setTo("xxx@your_mail_server.com"); mailMessage.setFrom("xxx@your_mail_server.com"); mailMessage.setSubject("Test"); mailMessage.setText("This is a test!!!"); 14
  • 15. Chapter 9 API 裝封 // 件郵送傳 senderImpl.send(mailMessage); System.out.println(" 送傳件郵 OK.."); } } 郵 的您果如,通溝作 埠接連與會設預 JavaMailSenderImple 25 SMTP 以行 執 , 埠 接 連 定 設 來25 用使以可,埠接連 用使是不器服伺件 setPort() 是 的 用 使 所 邊 這 在 , 案 檔 的 需 所 關相入 加 得 記 , 時 式 程 上 API .jar Spring 出送傳,案檔 lib 與 j2ee 的錄目 下錄目 中案檔載下 activation.jar mail.jar : 示 所 下如後 到 收 接 在 件 郵 的 去 圖 9.2 SimpleMailDemo 果結行執的案專 9.2.2 HTML 郵件 用使要果如 HTML 用使以可,話的件郵 Spring 的 org.springframe- 立建來 work.mail.javamail.MimeMessageHelper HTML 用使接直,件郵 :範示作來例實 HTMLMailDemo HTMLMailDemo.java package onlyfun.caterpillar; import org.springframework.mail.javamail.JavaMailSenderImpl; import javax.mail.internet.MimeMessage; import org.springframework.mail.javamail.MimeMessageHelper; 1
  • 16. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) public class HTMLMailDemo { public static void main(String[] args) throws Exception { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 定設 Mail Server senderImpl.setHost("your_mail_server.com"); // 息訊件郵立建 MimeMessage mailMessage = senderImpl.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); // 文內與題主、人件寄、人件收定設 messageHelper.setTo("xxx@your_mail_server.com"); messageHelper.setFrom("xxx@your_mail_server.com"); messageHelper.setSubject("Test"); messageHelper.setText( "<html><head></head><body><h1>Hello! Spring!" + "</h1></body></html>", true); // 件郵送傳 senderImpl.send(mailMessage); System.out.println(" 送傳件郵 OK..."); } } 在 MimeMessageHelper 為定設數參 的 上法方 setText() 的別類 boolean true 用啟要示表,時 HTML :容內件郵的後去出送傳下一看來,件郵的式格 圖 9.3 HTMLMailDemo 果結行執的案專 16
  • 17. Chapter 9 API 裝封 9.2.3 內嵌圖片或附檔 個一定給要時片圖嵌內在,片圖嵌內中件郵在以可也您 "cid" 直,值 :範示的式程看來接 AttachedImageDemo AttachedImageDemo.java package onlyfun.caterpillar; import org.springframework.mail.javamail.JavaMailSenderImpl; import javax.mail.internet.MimeMessage; import org.springframework.core.io.ClassPathResource; import org.springframework.mail.javamail.MimeMessageHelper; public class AttachedImageDemo { public static void main(String[] args) throws Exception { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 定設 Mail Server senderImpl.setHost("your_mail_server.com"); // 息訊件郵立建 MimeMessage mailMessage = senderImpl.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true); // 文內與題主、人件寄、人件收定設 messageHelper.setTo("xxx@your_mail_server.com"); messageHelper.setFrom("xxx@your_mail_server.com"); messageHelper.setSubject("Test"); messageHelper.setText( "<html><head></head><body><h1>Hello! Spring!" + "</h1><img src="cid:caterpillar">" + "</body></html>", true); ClassPathResource img = new ClassPathResource("caterpillar.jpg"); messageHelper.addInline("wish", img); 17
  • 18. 18 。 況 情 的 片 圖 示 顯 法無生 發 會 中 體 軟 端 戶 客 些某在能可,題問有會援支的片圖嵌內於對端戶客件郵的有是處壞,片圖 的結連所得取以可時件郵取讀者用使讓以,案檔供提上器服伺在須必,式 方的結連定給像用不是處好,壞有好有中件郵入加案檔像影將接直 果結行執的案專 9.4 AttachedImageDemo 圖 :面畫 之 件郵的 後 送 傳 下 一 看 來 ,中件 郵 至 入 加 案 檔 像 影 將 以 可 法 方 addInline() ,應對定指值 "cid" 的中法方 addInline() 與定設個這, "caterpillar" 為值 "cid" 定指法方 setText() 在,式模 multipart 用啟要示表, true 為值 boolean 的定給所時例實的別類 MimeMessageHelper 構建在 } } ..."); 功成送傳件郵 System.out.println(" senderImpl.send(mailMessage); // 件郵送傳 ) – http://openhome.cc 良信林(冊手術技 Spring 2.0
  • 19. Chapter 9 API 裝封 (案檔加附用使中件郵在以可也您 A ttachment file 用使要只,)Mime- 中其,法方 MessageHelper 的 addAttachment() "cid" 加附示顯來用是值 : 下 如例實 個 一 , 稱 名 的 案 檔 AttachedFileDemo AttachedFileDemo.java package onlyfun.caterpillar; import org.springframework.mail.javamail.JavaMailSenderImpl; import javax.mail.internet.MimeMessage; import org.springframework.core.io.ClassPathResource; import org.springframework.mail.javamail.MimeMessageHelper; public class AttachedFileDemo { public static void main(String[] args) throws Exception { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 定設 Mail Server senderImpl.setHost("your_mail_server.com"); // 息訊件郵立建 MimeMessage mailMessage = senderImpl.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true); // 文內與題主、人件寄、人件收定設 messageHelper.setTo("xxx@your_mail_server.com"); messageHelper.setFrom("xxx@your_mail_server.com"); messageHelper.setSubject("Test"); messageHelper.setText( "<html><head></head><body><h1>Hello! Spring!" + "</h1></body></html>", true); ClassPathResource file = new ClassPathResource("caterpillar.zip"); messageHelper.addAttachment("caterpillar.zip", file); // 件郵送傳 senderImpl.send(mailMessage); 1
  • 20. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) System.out.println(" 功成送傳件郵 ..."); } } : 面 畫件郵 的 後 送 傳 下 一 看 來 圖 9.5 AttachedFileDemo 果結行執的案專 2
  • 21. Chapter 9 API 裝封 9.3 排程 與 有 供 提 準 標 的 , 作工的 行 執 時 定 程 排 於 對 J DK API java.util.Timer 用善以可您讓,裝封象抽了供提它對則 java.util.TimerTask S pring ,別類 間之務任 與 務 任 定 指 能 只 , 限 有 能 功 Spring 而然,能功理管器容的 Timer 用使以可您,務任行執時定點間時個某定指法無,) (期週的 P eriod Quartz ,能功程排的多更了供提它,) h ttp://www.opensymphony.com/quartz/ ( Spring 。 便 方加更 上 用 使 在 , 裝 封 了 行 進 Quartz 對 而 9.3.1 使用 TimerTask (務任程排個一義定要 T ask 承繼以可,) java.util.TimerTask ,別類 :如例 TimerTaskDemo DemoTask.java package onlyfun.caterpillar; import java.util.TimerTask; public class DemoTask extends TimerTask { public void run() { System.out.println("Task is executed."); } } 的 Spring 用使以可著接 org.springframework.scheduling.timer. : 如 例 ,期週 行 執 的 務 任 義 定 來 ScheduledTimerTask 21
  • 22. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) TimerTaskDemo beans-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="demoTask" class="onlyfun.caterpillar.DemoTask"/> <bean id="scheduledTimerTask" class="org.springframework.scheduling. timer.ScheduledTimerTask"> → <property name="timerTask" ref="demoTask"/> <property name="period" value="600000"/> <property name="delay" value="10000"/> </bean> <bean id="timerFactoryBean" class="org.springframework.scheduling. timer.TimerFactoryBean"> → <property name="scheduledTimerTasks"> <list> <ref bean="scheduledTimerTask"/> </list> </property> </bean> </beans> ,秒毫是位單的義定,中性屬 ScheduledTimerTask 的別類 "period" 在 任 的義定 所 次 一 行 執 ) 秒 毫 ( 鐘 分 每 將,義 定 的 上 以 據 根 此 因 10 6 00000 少 多遲延 要 前 務 任 行 執 次 一 第 , 後 動 啟 "delay" Timer 了義定性屬 而,務 。秒毫 用使要 ScheduledTimerTask 的好義定org.springframework.sche- 要只來下接,務任程排的有所入加來別類 duling.timer.TimerFactoryBean :如 例 , 務 任 的 定 排 所行進 始 開 會 就 , 檔 義 定 完 取 讀 動 啟 器 容 Spring 22
  • 23. Chapter 9 API 裝封 TimerTaskDemo TimerTaskDemo.java package onlyfun.caterpillar; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Timer; import org.springframework.context.ApplicationContext; import org.springframework.context. support.ClassPathXmlApplicationContext; public class TimerTaskDemo { public static void main(String[] args) throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml"); System.out.println(" Task.."); 動啟 System.out.println(" 入輸請 exit 閉關 Task: "); BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); while(true) { if(reader.readLine().equals("exit")) { break; } } Timer timer = (Timer) context.getBean("timerFactoryBean"); timer.cancel(); } } 後動啟在式程個這,容內的檔義定 Bean 據根 10 任次一第行執會秒 。務任次一行執鐘分 每後之,務 10 23
  • 24. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) 9.3.2 使用 MethodInvokingTimerTaskFactoryBean 用使 Spring 承 繼 要 定 一 不 並,時 TimerTask ,務 任 個 一 義 定 來 S pring 供提 org.springframework.scheduling.timer.MethodInvokingTimerTask- FactoryBean 下一寫改以可如例,法方的件物個某行執定指接直以可, TimerTaskDemo :別類 承繼用不次這,別類 的中案專 DemoTask TimerTask TimerTaskDemo2 DemoTask.java package onlyfun.caterpillar; public class DemoTask { public void execute() { System.out.println("Task is executed."); } } 在要只著接 Bean 用使中檔義定 MethodInvokingTimerTaskFactory- Bean :如例,可即 TimerTaskDemo2 beans-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="demoTask" class="onlyfun.caterpillar.DemoTask"/> <bean id="timerTaskBean" class="org.springframework.scheduling. →timer.MethodInvokingTimerTaskFactoryBean"> <property name="targetObject" ref="demoTask"/> <property name="targetMethod" value="execute"/> </bean> 24
  • 25. Chapter 9 API 裝封 <bean id="scheduledTimerTask" 2
  • 26. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) class="org.springframework.scheduling. timer.ScheduledTimerTask"> → <property name="timerTask" ref="timerTaskBean"/> <property name="period" value="5000"/> <property name="delay" value="1000"/> </bean> <bean id="timerFactoryBean" class="org.springframework.scheduling. timer.TimerFactoryBean"> → <property name="scheduledTimerTasks"> <list> <ref bean="scheduledTimerTask"/> </list> </property> </bean> </beans> 用使接直以可時行執 TimerTaskDemo 的中案專 TimerTaskDemo 類 ,層底在,別 立建動自會 MethodInvokingTimerTaskFactoryBean TimerTask 。 法 方 定 指 的 上件物 標 目 行 執 以 例 實 的 9.3.3 使用 Quartz 間之務任與務任定指能只,限有能功 JDK API 的供提所中 準標 Timer 用使以可您,務任行執時定來點間時個某定指法無, P eriod ) ( 週的 期 Quartz ,能功程排的多更了供提它,) h ttp://www.opensymphony.com/quartz/ ( 。 便 方 加更上 用 使 在 , 裝 封 了 行 進 Spring Quartz 對則 而 承繼以可 org.springframework.scheduling.quartz.QuartzJobBean Job :如例,別類 個一作實來 26
  • 27. Chapter 9 API 裝封 QuartzDemo DemoJob.java package onlyfun.caterpillar; import org.quartz.JobExecutionContext; import org.springframework.scheduling. quartz.QuartzJobBean; public class DemoJob extends QuartzJobBean { private JobData jobData; public void executeInternal( JobExecutionContext context) { System.out.println( jobData.getData() + " is executed."); } public void setJobData(JobData jobData) { this.jobData = jobData; } public JobData getJobData() { return jobData; } } 執被 程排出看能了為,別類範示的件物料資 個一是只 JobData Job Job 時回傳的料資需所時 行執示表,件物 個一回傳它,性期週的時行 Date Job :如例,間 QuartzDemo JobData.java package onlyfun.caterpillar; import java.util.Date; public class JobData { public String getData() { return "Data from " + new Date().toString(); } } 27
  • 28. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) : 義定何 如 檔 義 定 看 來 接 直 QuartzDemo beans-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="someData" class="onlyfun.caterpillar.JobData"/> <bean id="jobDetailBean" class="org.springframework.scheduling. → quartz.JobDetailBean"> <property name="jobClass" value="onlyfun.caterpillar.DemoJob"/> <property name="jobDataAsMap"> <map> <entry key="jobData" value-ref="someData"/> </map> </property> </bean> <bean id="simpleTriggerBean" class="org.springframework.scheduling. → quartz.SimpleTriggerBean"> <property name="jobDetail" ref="jobDetailBean"/> <property name="repeatInterval" value="5000"/> <property name="startDelay" value="1000"/> </bean> <bean id="schedulerFactoryBean" class="org.springframework.scheduling. → quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleTriggerBean"/> </list> </property> </bean> </beans> 28
  • 29. Chapter 9 API 裝封 ,是的意注要別特中定設上以在 o rg.springframework.scheduling. 不而,稱名別類的 供提須必性屬 quartz.JobDetailBean 的 "jobClass" Job 中性屬 Job Bean 在以可料資的需所 而,例實 的 是 Job "jobDataAsMap" 。供提來 用使,上定指期週的務任程排在org.springframework.scheduling. 式方定指的中程排 與點這,定指來 quartz.SimpleTriggerBean TimerTask 是 的 用 使 所 , 時 定 排 而,位 單 為 作 秒 毫 以 是 也樣同 間 時 的 定 指 , 似 類 Job 。 org.springframework.scheduling.quartz.SchedulerFactoryBean ,行進會就務任程排,後檔義定取讀並 動啟要只,後之定設成完 Spring : 別 類 動 啟 務任的 單 簡 個 一 寫 撰 如 例 QuartzDemo QuartzDemo.java package onlyfun.caterpillar; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.springframework.context.ApplicationContext; import org.springframework.context. support.ClassPathXmlApplicationContext; public class QuartzDemo { public static void main(String[] args) throws IOException, SchedulerException { ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml"); System.out.println(" Task.."); 動啟 System.out.println(" 入輸請 exit Task: "); 閉關 BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); 2
  • 30. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) while(true) { if(reader.readLine().equals("exit")) { break; } } Scheduler scheduler = (Scheduler) context.getBean("schedulerFactoryBean"); scheduler.shutdown(); } } 執的務任個每到看以可中當,面畫考參個一的時行執式程個這看看來 :間時行 圖 9.6 QuartzDemo 果結行執的案專 期週的行執間之 與 的單簡作能只 用使 SimpleTriggerBean Job Job 用使以可則,定指的點間時作接直要果如,定指) ( P eriod org.spring- :如例, framework.scheduling.quartz.CronTriggerBean QuartzDemo2 beans-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans 3
  • 31. Chapter 9 API 裝封 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="someData" class="onlyfun.caterpillar.JobData"/> <bean id="jobDetailBean" class="org.springframework.scheduling. → quartz.JobDetailBean"> <property name="jobClass" value="onlyfun.caterpillar.DemoJob"/> <property name="jobDataAsMap"> <map> <entry key="jobData" value-ref="someData"/> </map> </property> </bean> <bean id="cronTriggerBean" class="org.springframework.scheduling. → quartz.CronTriggerBean"> <property name="jobDetail" ref="jobDetailBean"/> <property name="cronExpression" value="0 0 19 * * ?"/> </bean> <bean id="schedulerFactoryBean" class="org.springframework.scheduling. → quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTriggerBean"/> </list> </property> </bean> </beans> 間時個六少至是式格的定指,定指的性屬 於在點重 "cronExpression" ,次一 行執要時 的天每是定指的面上如例,素元間時個七多最,素元 19 Job :下如式格的定指性屬 "cronExpression" 31
  • 32. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) (秒 0 -59 ) (分 0 -59 ) ) (時小 0 -23 ) (天幾第月每 1 -31 ) 或 (月 1 -12 JAN-DEC 或 (天幾第期星每 1 -7 SUN-SAT ) ) (年 1 970-2099 一定設能只個兩,的斥互是」天幾第期星每「與」天幾第月每「中其 例 , 號 符 用 使 以 可 , 點 間時個 幾 好 有 果 如 , 寫 撰 號 符 以 的 定 設 不 , 個 ? , 對; 行執要時 、時 、時 的天每示表」 0 0 10,12,14 * * ? 「:如 10 12 14 Job 的月每示表」 「 如 例 , 號 符 用使 以 可 間 時 的 續 連 於 - 0 0 10,12,14 1-15 * ? 可 有可定 指 年 的 中 式 格 間 時 , 行 執 要 時 、 時 、 時 每 日 到 1 15 10 12 14 Job 、 時 的一期星每年 示表」 0 0 10,12,14 ? * MON 2006「:如例,無 2006 10 12 14 Job 。 行執要時 、時 9.3.4 使用 MethodInvokingJobDetailFactoryBean 承繼要定一不並,話的 用使果如 Spring QuartzJobBean 一義定來別類 Job 供提 ,別類 個 Spring org.springframework.scheduling.quartz. 的件物個某行執定指接直以可, MethodInvokingJobDetailFactoryBean :別類 的中案專 下一寫改如例,法方 QuartzDemo DemoJob QuartzDemo3 DemoJob.java package onlyfun.caterpillar; public class DemoJob { private JobData jobData; public void execute() { System.out.println( 32
  • 33. Chapter 9 API 裝封 jobData.getData() + " is executed."); } public void setJobData(JobData jobData) { this.jobData = jobData; } public JobData getJobData() { return jobData; } } 用使中檔義定 在要只著接Bean MethodInvokingJobDetailFactoryBean 的中案專 下一寫改如例,可即 QuartzDemo beans-config.xml : QuartzDemo3 beans-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="someData" class="onlyfun.caterpillar.JobData"/> <bean id="someJob" class="onlyfun.caterpillar.DemoJob"> <property name="jobData" ref="someData"/> </bean> <bean id="jobDetailBean" class="org.springframework.scheduling. → quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="someJob"/> <property name="targetMethod" value="execute"/> </bean> <bean id="cronTriggerBean" class="org.springframework.scheduling. → quartz.CronTriggerBean"> <property name="jobDetail" ref="jobDetailBean"/> 33
  • 34. Spring 2.0 良信林(冊手術技 – http://openhome.cc ) <property name="cronExpression" value="0 30 18 * * ?"/> </bean> <bean id="schedulerFactoryBean" class="org.springframework.scheduling. → quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTriggerBean"/> </list> </property> </bean> </beans> ,層 底 在 , 務 任 的 定 指 行 執 會 都 分 時 的天每 , 中 定 指 的 面 上 在 18 30 執以,例實的 立建動自會 MethodInvokingJobDetailFactoryBean JobDetail 。法方 定 指 的 件 物 標 目 行 34
  • 35. 3 感快發開的 Spring 驗體 9.7 圖 。 手上快 很 以 可 都 能 功 它 其 的 Spring 於 對 信 相 , 照對的 件 文 考 參 上 加 礎 基 的書本了有,件文考參的供提所 Spring 考參以可您議建,紹介加詳都個一 每法無,限有圍範的獵涉所人個與幅篇唯,多許有還能功的 於關上 Spring 實事,落段一告先紹介的能功別個 Spring 於對書本,止為節章個這到 9.4 接下來的主題 裝封 Chapter 9 API