---
title: "Spring xml 方式配制的小demo"
author: "huihai"
date: "2010-12-30T17:19:29Z"
tags: ["Bean", "Java", "Spring", "XML", "junit"]
categories: ["企业架构"]
slug: "spring-xml-demo"
draft: false
lastmod: "2010-12-30T17:31:33Z"
id: 856112
url: "https://huihai.iteye.com/blog/856112"
---

1、新建一个Web程序 在程序中引入Spring所需要的jar包，我以前有弄好的包，包含了以下几个jar包 \[img]http://dl.iteye.com/upload/picture/pic/78836/a0cf5992-f5a9-3e0b-bdb4-1b31892fe1d2.jpg\[/img] 2、在src下新建Person.java的实例bean. \[code="java"]package cn.ehoo.bean; /\** \*@author whp \*@Email whp@ehoo.cn \*@Dec 30, 2010 * \*/ public class Person { private Long id; private String userName; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } } \[/code] 3、再建立数据层的PersonDao接口与PersonDaoBean的实现bean PersonDao.java \[code="java"]package cn.ehoo.dao; import cn.ehoo.bean.Person; /\** \*@author whp \*@Email whp@ehoo.cn \*@Dec 30, 2010 * \*/ public interface PersonDao { public void save(Person person); }\[/code] PersonDaoBean.java \[code="java"]package cn.ehoo.dao.impl; import org.springframework.stereotype.Repository; import cn.ehoo.bean.Person; import cn.ehoo.dao.PersonDao; /\** \*@author whp \*@Email whp@ehoo.cn \*@Dec 30, 2010 * \*/ public class PersonDaoBean implements PersonDao { public void save(Person person){ System.out.println("执行 PersonDaoBean 里的save方法"); } } \[/code] 4 建立服务层的PersonService.java与实现类PersonServiceBean.java PersonService.java \[code="java"]package cn.ehoo.service; import cn.ehoo.bean.Person; /\** \*@author whp \*@Email whp@ehoo.cn \*@Dec 30, 2010 * \*/ public interface PersonService { public void save(Person person); }\[/code] 实现类PersonServiceBean.java \[code="java"]package cn.ehoo.service.impl; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import org.springframework.stereotype.Service; import cn.ehoo.bean.Person; import cn.ehoo.dao.PersonDao; import cn.ehoo.service.PersonService; /\** \*@author whp \*@Email whp@ehoo.cn \*@Dec 30, 2010 * \*/ public class PersonServiceBean implements PersonService { private PersonDao personDao; public void save(Person person){ System.out.println("执行 PersonServiceBean 里的save方法"); personDao.save(person); } public void init(){ System.out.println("初始化资源文件"); } public void destroy(){ System.out.println("关闭资源性文件"); } } \[/code] 5 在程序的classpath路径下面(也就是src的根目录下)建立beans.xml文件 \[code="java"] - - \[/code] 6 junit进行测试 \[code="java"]package junit.test; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.ehoo.bean.Person; import cn.ehoo.service.PersonService; /\** \*@author whp \*@Email whp@ehoo.cn \*@Dec 30, 2010 * \*/ public class SpringTest { public static PersonService personService; @BeforeClass public static void setUpBeforeClass() throws Exception { AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); personService =(PersonService)cxt.getBean("personServiceBean"); } @Test public void save(){ Person person = new Person(); personService.save(person); } public static void main(String\[] args) { try { setUpBeforeClass(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /\** \*@author whp \*@Email whp@ehoo.cn \*@Dec 30, 2010 * \*/ @AfterClass public static void tearDownAfterClass() throws Exception { } } \[/code]