Alex刺客

          Dancing fingers, damage world. -- 舞動手指,破壞世界.

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            57 隨筆 :: 0 文章 :: 76 評論 :: 0 Trackbacks
          Spring 下載地址: http://www.springsource.org/download

          最少依賴包 commons-logging-1.1.1.jar spring.jar (我增加了log4j讓執行步驟輸出更詳細).

          1. Hello.java
          1?package?alex.study.spring.hello;
          2?
          3?public?interface?Hello?{
          4?????void?salute();
          5?}

          2. HelloImp.java
          ?1?package?alex.study.spring.hello;
          ?2?
          ?3?public?class?HelloImp?implements?Hello?{
          ?4?
          ?5?????private?String?name;
          ?6?????private?String?send;
          ?7?????
          ?8?????public?HelloImp(String?name,?String?send)?{
          ?9?????????this.name?=?name;
          10?????????this.send?=?send;
          11?????}
          12?
          13?????public?void?setName(String?name)?{
          14?????????this.name?=?name;
          15?????}
          16?
          17?????public?void?setSend(String?send)?{
          18?????????this.send?=?send;
          19?????}
          20?
          21?????@Override
          22?????public?void?salute()?{
          23?????????System.out.println(name?+?"?:?"?+?send);
          24?????}
          25?
          26?}
          27?

          3. spring-hello.xml (跟以上類放在同一包中)
          ?1?<?xml?version="1.0"?encoding="UTF-8"?>
          ?2?<beans?xmlns="http://www.springframework.org/schema/beans"
          ?3?????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ?4?????xsi:schemaLocation="http://www.springframework.org/schema/beans?
          ?5?????http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
          ?6?
          ?7?????<!--?構造方法注入Bean?-->
          ?8?????<bean?id="helloimp"?class="alex.study.spring.hello.HelloImp">
          ?9?????????<!--?告訴Spring通過構造函數給Bean屬性賦值?-->
          10?????????<constructor-arg?value="alex"?/>
          11?????????<constructor-arg?value="Hello?World!"/>
          12?????</bean>
          13?</beans>
          14?

          4. log4j.properties (src目錄下)
          1?log4j.appender.stdout=org.apache.log4j.ConsoleAppender
          2?log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
          3?log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss}?[%5p]?%c{1}?-?%m%n
          4?
          5?log4j.rootLogger=DEBUG,?stdout
          6?
          7?log4j.category.com.springinaction=DEBUG
          8?log4j.category.org.springframework=DEBUG
          9?

          5. HelloApp.java
          ?1?package?alex.study.spring.hello;
          ?2?
          ?3?import?org.springframework.beans.factory.BeanFactory;
          ?4?import?org.springframework.beans.factory.xml.XmlBeanFactory;
          ?5?import?org.springframework.core.io.ClassPathResource;
          ?6?
          ?7?public?class?HelloApp?{
          ?8?????public?static?void?main(String[]?args)?{
          ?9?????????//初始化Bean工廠
          10?????????BeanFactory?factory?=?new?XmlBeanFactory(new?ClassPathResource("alex/study/spring/hello/spring-hello.xml"));
          11?????????Hello?hello?=?(Hello)?factory.getBean("helloimp");??//從Bean工廠獲取xml文件配置的ID為helloimp?Bean.
          12
          13?????????hello.salute();
          14?????}
          15?}
          16?

          6. 輸出結果
          1?
          2?14:45:38?[DEBUG]?DefaultBeanDefinitionDocumentReader?-?Loading?bean?definitions
          3?14:45:38?[DEBUG]?XmlBeanFactory?-?Creating?shared?instance?of?singleton?bean?'helloimp'
          4?14:45:38?[DEBUG]?XmlBeanFactory?-?Creating?instance?of?bean?'helloimp'
          5?14:45:38?[DEBUG]?XmlBeanFactory?-?Eagerly?caching?bean?'helloimp'?to?allow?for?resolving?potential?circular?references
          6?14:45:38?[DEBUG]?XmlBeanFactory?-?Finished?creating?instance?of?bean?'helloimp'
          7?alex?:?Hello?World!

          posted on 2010-07-02 23:10 Alex刺客 閱讀(445) 評論(0)  編輯  收藏 所屬分類: Spring Study Notes

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


          網站導航:
           
          主站蜘蛛池模板: 乌兰浩特市| 阜南县| 虎林市| 海原县| 东乌珠穆沁旗| 乌拉特前旗| 永泰县| 蒙山县| 德庆县| 洛隆县| 泰州市| 南和县| 方城县| 田阳县| 芦山县| 咸宁市| 黄平县| 镇巴县| 宜宾市| 永善县| 丰顺县| 闸北区| 绥中县| 新和县| 万盛区| 阜南县| 墨脱县| 双峰县| 枣强县| 甘肃省| 利津县| 米泉市| 郸城县| 麦盖提县| 湖南省| 怀柔区| 个旧市| 屏东县| 将乐县| 东方市| 宜都市|