您的位置:首页 > 编程语言 > Java开发

【j2ee spring】3、通过classpath自动扫描方式吧组件纳入spring容器中管理

2015-03-22 21:22 645 查看

通过classpath自动扫描方式吧组件纳入spring容器中管理

1、几个基本的注解

通过xml文件的<context:component-scan属性

@Service 用于标注业务层组件,交给spring时默认值得到这个实例是类名,第一个字母小写,也可以自己指定后面加(""),

比如:@Service("personService")


@Controller 用于标注控制层组件

@Repository 用于标注数据访问组件,即DAO组件

@Component 泛指组件,当组件不好归类的时候,用这个注解

还有可以用@Scope来指定bean的作用域范围

package cn.itcast.service.impl;

import javax.annotation.PreDestroy;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;

@Service("personService")
public class PersonServiceBean implements PersonService 
{
	private PersonDao personDao;
	private String name = "cutter_point";
	
	
	public PersonServiceBean() 
	{
		// TODO Auto-generated constructor stub
	}

	/**
	 * @author xiaofeng
	 * @param 这个是可以生成的构造器
	 */
	public PersonServiceBean(PersonDao personDao, String name) 
	{
		this.personDao = personDao;
		this.name = name;
	}

	public void save(){
		System.out.println(name);
		personDao.add();
	}
}


2、关于@Scope

简单点说就是用来指定bean的作用域(官方解释:scope用来声明IOC容器中的对象应该处的限定场景或者说该对象的存活空间,

即在IOC容器在对象进入相应的scope之前,生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁这些对象),

其默认作用域是"singleton",如果要换成其他作用区域,直接后面添加类型即可,比如@Scope("prototype") ,注意spring2.0后 又增加了request ,

session和global session 4个作用区域。

1、singleton 作用域

当一个bean的 作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。换言之,当把一个bean定义设置为singleton作用域时,Spring IOC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例,这里要注意的是singleton作用域和GOF设计模式中的单例是完全不同的,单例设计模式表示一个ClassLoader中
只有一个class存在,而这里的singleton则表示一个容器对应一个bean,也就是说当一个bean被标识为singleton时 候,spring的IOC容器中只会存在一个该bean。
配置实例:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="singleton"/>


或者
<bean id="role" class="spring.chapter2.maryGame.Role" singleton="true"/>


2、prototype

prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的 getBean()方 法)都会产生一个新的bean实例,相当与一个new的操作,对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个 prototype bean的整个生命周期负责,容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。
清除prototype作用域的对象并释放任何prototypebean所持有的昂贵资源,都是客户端代码的职责。(让Spring容器释放被singleton作用域bean占用资源的一种可行方式是,通过使用 bean的后置处理器,该处理器持有要被清除的bean的引用。)
配置实例:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="prototype"/>


或者
<beanid="role" class="spring.chapter2.maryGame.Role" singleton="false"/>


package cn.itcast.service.impl;

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;

@Service("personService")  @Scope("prototype")  //这个是非单例模式的
public class PersonServiceBean implements PersonService 
{
	private PersonDao personDao;
	private String name = "cutter_point";
	
	@PostConstruct		//这个用来注解初始化,可用于构造函数
	public void init()
	{
		System.out.println("初始化");
	}
	
	@PreDestroy			//这个注解的作用类似析构函数
	public void destory()
	{
		System.out.println("关闭资源");
	}
	
	public PersonServiceBean() 
	{
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * @author xiaofeng
	 * @param 这个是可以生成的构造器
	 */
	public PersonServiceBean(PersonDao personDao, String name) 
	{
		this.personDao = personDao;
		this.name = name;
	}

	public void save(){
		System.out.println(name);
		personDao.add();
	}
}


3、还可以用于初始化和关闭资源的注解器

@PostConstruct //这个用来注解初始化,可用于构造函数
@PreDestroy //这个注解的作用类似析构函数

public class PersonServiceBean implements PersonService 
{
	private PersonDao personDao;
	private String name = "cutter_point";
	
	@PostConstruct		//这个用来注解初始化,可用于构造函数
	public void init()
	{
		System.out.println("初始化");
	}
	
	@PreDestroy			//这个注解的作用类似析构函数
	public void destory()
	{
		System.out.println("关闭资源");
	}
	
	public PersonServiceBean() 
	{
		// TODO Auto-generated constructor stub
	}
	
	
	/**
	 * @author xiaofeng
	 * @param 这个是可以生成的构造器
	 */
	public PersonServiceBean(PersonDao personDao, String name) 
	{
		this.personDao = personDao;
		this.name = name;
	}

	public void save(){
		System.out.println(name);
		personDao.add();
	}
}


相应的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.xsd 		 http://www.springframework.org/schema/context 		 http://www.springframework.org/schema/context/spring-context.xsd">           
          <context:component-scan base-package="cn.itcast" /><!-- 扫描所有的包,除了junit.test -->
          
</beans>


运行的结果:



加上后面那个类似析构函数的注解器:@PreDestory

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐