如何學好java

          如何學好java,其實很簡單,只要用心體會,慢慢積累!
          posts - 106, comments - 7, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          第一個Spring程序

          Posted on 2011-04-28 12:19 哈希 閱讀(176) 評論(0)  編輯  收藏 所屬分類: java 常用框架
          如下分成5個步驟
          1,建立xml文件
          2,建立bean的接口
          3,建立bean
          4,寫測試程序
          5,測試

          準備工作
          環境配置如下,需要spring.jar和common-logging.jar兩個jar文件

          開始
          1,建立xml文件
          文件名:beans.xml
          文件位置:src目錄下
          文件內容:
          <?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.5.xsd">

            <!-- the application context definition for the springapp DispatcherServlet -->

            <bean id="sayhello" class="test.service.impl.HelloBean"/>
             
          </beans>

          2,建立bean的接口
          文件名:Hello.java
          文件內容:
          package test.service;

          public interface Hello {

              public void sayHello();

          }

          3,建立bean
          文件名:HelloBean.java
          文件內容:
          package test.service.impl;

          import test.service.Hello;

          public class HelloBean implements Hello {
             
              /* (non-Javadoc)
               * @see test.service.impl.Hello#sayHello()
               */
              public void sayHello() {
                  System.out.println("這是一個測試程序");
              }

          }

          4,寫測試程序
          文件名:FirstSpring.java
          文件內容:
          package test.spring;

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

          import test.service.Hello;

          public class FirstSpring {
             
              public static void main(String[] args) {
                  testHello();
              }
             
              public static void testHello() {
                  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
                  Hello hello =(Hello) applicationContext.getBean("sayhello");
                  hello.sayHello();
                 
              }
          }

          5,測試
          運行FirstSpring.java文件,得到輸出結果如下:
          2009-6-30 3:33:58 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
          信息: Loading XML bean definitions from class path resource [beans.xml]
          2009-6-30 3:33:59 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
          信 息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@7259da]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2e7820
          2009-6-30 3:33:59 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
          信 息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2e7820: defining beans [sayhello]; root of factory hierarchy
          這是一個測試程序

          上面紅字是spring輸出的調試信息,藍字是hellobean實際輸入的內容。

          簡單總結:
          1,環境配置中不要忘記了common-logging.jar文件,我最開始忘記了,還是用junit測試的,結果就是不通過。出錯的原因也不明白。后來直接改成普通的main方法測試,才明白原因。

          2,bean的接口和實現的分離在spring中被貫徹執行。同時理解一下IOC(控制反轉)的概念。
          3,spring中的bean,應該指的是執行各種業務的業務bean才是。不同于strut的formbean和對應db表對象的valuebean。
          主站蜘蛛池模板: 星子县| 定襄县| 松滋市| 凤山市| 班戈县| 慈利县| 清水县| 称多县| 古田县| 梁平县| 都兰县| 壶关县| 建昌县| 饶阳县| 图片| 伊吾县| 九龙城区| 科技| 昂仁县| 乐昌市| 平邑县| 黄浦区| 曲麻莱县| 岑溪市| 和林格尔县| 巢湖市| 越西县| 常德市| 丰原市| 富顺县| 多伦县| 昭觉县| 沽源县| 平乐县| 德令哈市| 新民市| 潜山县| 新化县| 南江县| 开远市| 永川市|