首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Spring xml 形式配制的小demo

2012-09-21 
Spring xml 方式配制的小demo1、新建一个Web程序在程序中引入Spring所需要的jar包,我以前有弄好的包,包含了

Spring xml 方式配制的小demo
1、新建一个Web程序
   在程序中引入Spring所需要的jar包,我以前有弄好的包,包含了以下几个jar包

2、在src下新建Person.java的实例bean.

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;}}


3、再建立数据层的PersonDao接口与PersonDaoBean的实现bean

PersonDao.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);}


PersonDaoBean.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方法");}}


4 建立服务层的PersonService.java与实现类PersonServiceBean.java

PersonService.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);}


实现类PersonServiceBean.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("关闭资源性文件");}}


5 在程序的classpath路径下面(也就是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">  <bean name="personDao" /> - <bean name="personService" ref="personDao" />   </bean>  </beans>


6 junit进行测试
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;@BeforeClasspublic static void setUpBeforeClass() throws Exception {AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");personService =(PersonService)cxt.getBean("personServiceBean");}@Testpublic void save(){Person person = new Person();personService.save(person);}public static void main(String[] args) {try {setUpBeforeClass();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** *@author whp *@Email whp@ehoo.cn *@Dec 30, 2010 * */@AfterClasspublic static void tearDownAfterClass() throws Exception {}}







热点排行