隨筆-21  評(píng)論-29  文章-0  trackbacks-0
          搭建與測(cè)試Spring的開(kāi)發(fā)環(huán)境
          使用版本為Spring2.5.6

          新建一個(gè)Java Project 命名為spring 并導(dǎo)入相關(guān)的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>

          實(shí)例化Spring容器 建議用方法一

          新建一個(gè)單元測(cè)試SpringTest,并導(dǎo)入測(cè)試所用的包
          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");
                      }
          }

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

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


          修改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();
              }

          }

          在實(shí)例化了容器之后,從容器中取得bean,再調(diào)用業(yè)務(wù)bean的save方法

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



          以上證明本Spring程序運(yùn)行成功!

          代碼參考 /Files/luckygino/spring.rar
          posted on 2009-05-06 10:25 特立獨(dú)行 閱讀(475) 評(píng)論(0)  編輯  收藏 所屬分類: Spring框架
          主站蜘蛛池模板: 永年县| 三门峡市| 农安县| 永德县| 柯坪县| 武城县| 高平市| 福清市| 洛隆县| 邳州市| 柞水县| 梁河县| 邯郸市| 五寨县| 城步| 乌拉特后旗| 淅川县| 镇远县| 永善县| 丁青县| 汝阳县| 即墨市| 柳林县| 措美县| 将乐县| 昔阳县| 雅安市| 鄂托克前旗| 裕民县| 随州市| 长治县| 洪江市| 鄄城县| 西充县| 陵水| 乌拉特中旗| 罗田县| 炉霍县| 若羌县| 凤台县| 昆山市|