隨筆-21  評論-29  文章-0  trackbacks-0
          搭建與測試Spring的開發環境
          使用版本為Spring2.5.6

          新建一個Java Project 命名為spring 并導入相關的jar包
          配置Spring配置文件

          在src下新建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"
                 xsi:schemaLocation
          ="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
          >
                    
          </beans>

          實例化Spring容器 建議用方法一

          新建一個單元測試SpringTest,并導入測試所用的包
          package junit.test;
          import org.junit.BeforeClass;
          import org.junit.Test;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          import cn.itcast.service.PersonService;

          public class SpringTest {

              @BeforeClass
              public static void setUpBeforeClass() throws Exception {
              }

              @Test public void instanceSpring(){
                  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
                      }
          }

          新建一個業務Bean,命名為PersonServiceBean;抽取PersonServiceBean的接口。
          package cn.itcast.service.impl;

          import cn.itcast.service.PersonService;

          public class PersonServiceBean implements PersonService {

              
          public void save(){
                  System.out.println(
          "我是save()方法");
              }

          }


          package cn.itcast.service;

          public interface PersonService {

              
          public void save();

          }
          在配置文件中加入如下語句實現
          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
          注意:編寫spring配置文件時,不能出現幫助信息 同通過如下方法解決


          修改SpringTest代碼
          package junit.test;
          import org.junit.BeforeClass;
          import org.junit.Test;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          import cn.itcast.service.PersonService;

          public class SpringTest {

              @BeforeClass
              
          public static void setUpBeforeClass() throws Exception {
              }


              @Test 
          public void instanceSpring(){
                  ApplicationContext ctx 
          = new ClassPathXmlApplicationContext("beans.xml");
                  PersonService personService 
          = (PersonService)ctx.getBean("personService");
                  personService.save();
              }

          }

          在實例化了容器之后,從容器中取得bean,再調用業務bean的save方法

          執行SpringTest文件 觀察控制臺輸出



          以上證明本Spring程序運行成功!

          代碼參考 /Files/luckygino/spring.rar
          posted on 2009-05-06 10:25 特立獨行 閱讀(469) 評論(0)  編輯  收藏 所屬分類: Spring框架
          主站蜘蛛池模板: 北海市| 克山县| 鹤庆县| 新龙县| 苍南县| 宣城市| 巫山县| 理塘县| 云南省| 沧源| 武安市| 龙州县| 新野县| 万州区| 渝北区| 辽阳县| 琼结县| 进贤县| 巴林右旗| 晴隆县| 徐州市| 武胜县| 丹东市| 荆门市| 阜城县| 淮南市| 商水县| 宝兴县| 尼木县| 镇沅| 瓦房店市| 荣成市| 衡东县| 泸溪县| 沅江市| 武冈市| 洪泽县| 忻州市| 出国| 扶风县| 开封县|