隨筆-21  評論-29  文章-0  trackbacks-0
          搭建與測試Spring的開發(fā)環(huán)境
          使用版本為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");
                      }
          }

          新建一個業(yè)務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();

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


          修改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,再調用業(yè)務bean的save方法

          執(zhí)行SpringTest文件 觀察控制臺輸出



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

          代碼參考 /Files/luckygino/spring.rar
          posted on 2009-05-06 10:25 特立獨行 閱讀(469) 評論(0)  編輯  收藏 所屬分類: Spring框架
          主站蜘蛛池模板: 虹口区| 洛浦县| 木兰县| 元氏县| 手机| 汶上县| 丰都县| 冀州市| 观塘区| 韶山市| 剑阁县| 贵阳市| 全椒县| 松江区| 遵义市| 兴化市| 甘泉县| 山东省| 广西| 白山市| 景洪市| 贵溪市| 三河市| 大冶市| 鄂温| 来宾市| 铜梁县| 北安市| 辽宁省| 积石山| 柏乡县| 寿阳县| 通河县| 建始县| 乌兰县| 揭西县| 玉环县| 浮梁县| 霍邱县| 扎鲁特旗| 吉林市|