go4it

just do it

Spring视频学习(五)组建扫描

一、组建扫描配置

clip_image002

<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="com.persia">
<!-- 开启组件扫描 -->
</context:component-scan>
注意xml配置,前面的命名空间。
bean的配置如下:
import org.springframework.stereotype.Repository;

@Repository
public class PersonDaoBeanScan implements IDaoBean {

	/* (non-Javadoc)
	 * @see com.persia.IDaoBean#add()
	 */
	public void add(){
		System.out.println("调用add方法");
	}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

@Service
public class PersonServiceBeanScan implements IService {

	/* (non-Javadoc)
	 * @see com.persia.IService#save()
	 */
    @Resource
	private IDaoBean personDao;
	private String name;
	private int age;
	private Set<String> sets=new HashSet<String>();
	private List<String> lists=new ArrayList<String>();
	private Properties properties=new Properties();
	private Map<String,String> map=new HashMap<String,String>();
	
	
	
	public PersonServiceBeanScan(String name, IDaoBean personDao) {
		
		this.name = name;
		this.personDao = personDao;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public List<String> getLists() {
		return lists;
	}

	public void setLists(List<String> lists) {
		this.lists = lists;
	}

	public Set<String> getSets() {
		return sets;
	}

	public void setSets(Set<String> sets) {
		this.sets = sets;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public IDaoBean getPersonDao() {
		return personDao;
	}

	public void setPersonDao(IDaoBean personDao) {
		this.personDao = personDao;
	}

	public PersonServiceBeanScan(){
		System.out.println("bean被实例化");
	}
	
	public void init(){
		System.out.println("资源操作");
	}
	public void save(){
		System.out.println("hello");
		personDao.add();
	//	System.out.println("被注入的name的值:"+this.name);
	}
	
	public void destory(){
		System.out.println("资源关闭操作");
	}
}

二、如何指定名称,指定范围,指定init方法,destroy方法

若要指定名称@Repository(“stuDao”)

指定范围@Scope(“prototype”)

指定init方法:@PostConstruct

指定destroy方法:@PreDestroy

posted on 2009-07-20 13:33  cxccbv  阅读(350)  评论(0编辑  收藏  举报

导航