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;}}
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);}
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方法");}}
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);}
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("关闭资源性文件");}}
<?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>
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 {}}