`

Spring 注解 方式配制的小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
 *
 */
@Repository//代表这个类是数据库层的代码
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
 *
 */
@Service//代表这个类是服务层的代码
public class PersonServiceBean implements PersonService {
	@Resource private PersonDao personDao;
	public void save(Person person){
		System.out.println("执行 PersonServiceBean 里的save方法"); 
		personDao.save(person);
	}
	@PostConstruct//如果在使用这个类之前想对这个类的一些资源进行初始化可以用这个注解
	public void init(){
		System.out.println("初始化资源文件");
	}
	@PreDestroy//如果在使用这个类之后想对这个类的一些资源进行关闭可以用这个注解
	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" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<context:component-scan base-package="cn.ehoo" /><!-- 在打开这个注解后,spring就会自动对java程序里标识了注解的类进行管理 -->
</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;
	@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 {
	}
}







分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics