EricGu's Java-Record-Space

          專注 學(xué)習(xí) 實(shí)踐 創(chuàng)新

          Spring 2.5 標(biāo)注開發(fā)的簡單例子

          研究了很久新出的 Spring 2.5, 總算大致明白了如何用標(biāo)注定義 Bean, 但是如何定義和注入類型為 java.lang.String 的 bean 仍然未解決, 希望得到高人幫助.

          總的來看 Java EE 5 的標(biāo)注開發(fā)方式開來是得到了大家的認(rèn)可了.

          @Service 相當(dāng)于定義 bean, 自動(dòng)根據(jù) bean 的類名生成一個(gè)首字母小寫的 bean

          @Autowired 則是自動(dòng)注入依賴的類, 它會(huì)在類路徑中找成員對(duì)應(yīng)的類/接口的實(shí)現(xiàn)類, 如果找到多個(gè), 需要用 @Qualifier("chineseMan") 來指定對(duì)應(yīng)的 bean 的 ID.

          一定程度上大大簡化了代碼的編寫, 例如一對(duì)一的 bean 映射現(xiàn)在完全不需要寫任何額外的 bean 定義了.

          下面是代碼的運(yùn)行結(jié)果:

          man.sayHello()=你好
          SimpleMan said: Hi
          org.example.EnglishMan@12bcd4b said: Hello

           

          代碼:

          beans.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:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-2.5.xsd">
          <context:annotation-config/>
          <context:component-scan base-package="org.example"/>
          </beans>

          測(cè)試類:

          import org.example.IMan;
          import org.example.SimpleMan;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          public class SpringTest {
              public static void main(String[] args) {
                  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
                  SimpleMan dao = (SimpleMan) ctx.getBean("simpleMan");
                  System.out.println(dao.hello());
                  IMan man = (IMan) ctx.getBean("usMan");
                  System.out.println(man.sayHello());
              }
          }

          自動(dòng)探測(cè)和注入bean的類:

          package org.example;

          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.beans.factory.annotation.Qualifier;
          import org.springframework.stereotype.Service;

          @Service
          public class SimpleMan {
              // 自動(dòng)注入名稱為 Man 的 Bean
              @Autowired(required = false)
              @Qualifier("chineseMan")
              //@Qualifier("usMan")
              private IMan man;   

              /**
               * @return the man
               */
              public IMan getMan() {
                  return man;
              }

              /**
               * @param man the man to set
               */
              public void setMan(IMan man) {
                  this.man = man;
              }

              public String hello() {
                  System.out.println("man.sayHello()=" + man.sayHello());
                  return "SimpleMan said: Hi";
              }
          }

           

          一個(gè)接口和兩個(gè)實(shí)現(xiàn)類:

          package org.example;

          /**
          * 抽象的人接口.
          * @author BeanSoft
          * @version 1.0
          */
          public interface IMan {
              /**
               * 打招呼的抽象定義.
               * @return 招呼的內(nèi)容字符串
               */
              public String sayHello();
          }

           

          package org.example;

          import org.springframework.stereotype.Service;

          /**
          * 中國人的實(shí)現(xiàn).
          * @author BeanSoft
          */
          @Service
          public class ChineseMan implements IMan {

              public String sayHello() {
                  return "你好";
              }

          }

           

          package org.example;

          import org.springframework.stereotype.Service;

          /**
          * @author BeanSoft
          *
          */
          @Service("usMan")
          // 這里定義了一個(gè) id 為 usMan 的 Bean, 標(biāo)注里面的屬性是 bean 的 id
          public class EnglishMan implements IMan {

              public String sayHello() {
                  return this + " said: Hello!";
              }

          }

          posted on 2009-03-11 16:56 Eric Gu 閱讀(221) 評(píng)論(0)  編輯  收藏 所屬分類: Spring

          導(dǎo)航

          <2009年3月>
          22232425262728
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 万宁市| 共和县| 光山县| 元阳县| 延津县| 东乌珠穆沁旗| 天祝| 安化县| 本溪| 新民市| 绥芬河市| 巴青县| 南华县| 麟游县| 哈巴河县| 兰坪| 如皋市| 江山市| 南溪县| 博罗县| 北流市| 宿迁市| 土默特左旗| 崇文区| 嘉黎县| 海南省| 巴彦淖尔市| 宿迁市| 玛沁县| 龙川县| 榆社县| 郴州市| 遵化市| 铜梁县| 涿鹿县| 新野县| 新田县| 新竹县| 白山市| 衡阳市| 旬邑县|