`

Spring自动扫描和管理Bean

阅读更多

         在前面使用注解的时候,除了<beans>结点配置增加了名称空间说明,另还增加了<context:annotation-config/>配置,它的作用就是隐式注册了多个对注释进行解析的处理器。通常情况下,我们要使用某个bean实例,总会配置<bean>相关内容。Spring2.5为我们引入了自动扫描机制,他可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理,它的作用和在xml文件中使用bean节点配置是一样的.我们只要在配置文件作如下设置:<context:component-scan base-package="包名"/>便可以自动管理指定包名及子包下标住了@Service用于标注业务层组件、@Controller用于标注控制层组件(如struts中的action)、@Repository用于标注数据访问组件,即DAO组件。而@Component泛指组件(当组件不好归类的时候,我们可以用这个注解进行标注)的类,并把它们作一个实例bean,相当于在beans.xml中配置了<bean>元素。需要说明的是,使用了此配置同时意味着还注册了注解配置的处理器,所以在这些类中用到了注解配置时并不需要再配置<context:annotation-config/>。
       为什么提出自动扫描管理:在一些比较大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,不但配置内容繁琐,而且显然会增加配置文件的体积,查找及维护起来也不太方便。因此spring提出了自动扫描bean,它依据在xml文件中指定的包名和类中标记的component系列注解。

实例讲解

1.配置文件与上一节中的配置文件很相似,由于<context:component-scan base-package="包名"/>注册的对注释的处理器包含了<context:annotation-config/>语句注册的处理器,所以这句话可以省略了:

<?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="com.reiyenkart"/> <!--其中base-package为需要扫描的包(含子包)-->
</beans>

 2.java代码实例

2.1 组件默认注册代码如下所示:

@Service
public class PersonServiceBean{
}

 在Spring容器中获取此组件:

PersonService personService = (PersonService)ctx.getBean(“personServiceBean”)

 注意:如果使用Service这些注解时不指定名称,这些实例bean的名称就是类名(但首字母小写)。

2.2 注册组件时指定名称,代码如下:

@Service(“personService”) //指定调用时的名称,
 public class PersonServiceBean{
}

  在Spring容器中获取此组件:

PersonService personService = (PersonService)ctx.getBean(“personService”)

 2.3 使用注解@Scope配置bean的作用范围

@Service("personService") @Scope("pototype")
 public class PersonServiceBean{
}

 测试代码:

PersonService personService = (PersonService)ctx.getBean("personService")
PersonService personService1= (PersonService)ctx.getBean("personService")
System.out.println(personService == personService1);

 输出结果为false,说明此组件是作用域prototype的。

2.4 @PostConstruct注解指定bean的init方法

@PostConstruct //使用注解指定bean的初始化方法,这个注解不是Spring的注解,而是EJB3的
public void init(){
}

 2.5 @PreDestory注解指定bean的destory方法

@PreDestory //使用注解指定bean的destory方法
public void destroy(){}

  总结:@Scope对应于xml配置文件的scope属性
        @PostConstruct对应于xml配置文件中的init-method属性
        @PreDestroy对于应于xml配置文件中的destroy-method属性

部分注解对应的类如下:

1、@Autowired        org.springframework.beans.factory.annotation.Autowired;
2、@Qualifier        org.springframework.beans.factory.annotation.Qualifier;
3、@Componet        org.springframework.stereotype.Component;
4、@Resource        javax.annotation.Resource;
5、@Scope        org.springframework.context.annotation.Scope;
6、@PostConstruct    javax.annotation.PreDestroy;
7、@PreDestroy        javax.annotation.PreDestroy;
一个完整的实例:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component(value="userService")
@Scope("singleton")
public class UserService {
	@PostConstruct 
	public void init(){System.out.println("现在开始初始化UserService");}	
	private UserDao userDao = new UserDaoImpl();
	public UserDao getUserDao() {return userDao;}
	@Resource
	public void setUserDao(UserDao userDao) {this.userDao = userDao;}
	public void add(User u){	userDao.save(u);}
	@PreDestroy
	public void destroy(){System.out.println("destory");	}
 

 

 

 

 

分享到:
评论
2 楼 YSOLA4 2012-11-11  
请教一下,你的ctx是什么啊?
1 楼 dengjie200 2010-10-11  
为什么我注解定义的BEAN就用CTX获取不到呢?????????????根本不在BeanDefinitionNames里面

相关推荐

    spring自动扫描和管理Bean的示例

    spring自动扫描和管理Bean的示例

    Spring学习笔记(9)----让Spring自动扫描和管理Bean

    NULL 博文链接:https://coolszy.iteye.com/blog/519448

    spring2.5学习PPT 传智博客

    让Spring自动扫描和管理Bean 15.使用JDK中的Proxy技术实现AOP功能 16.使用CGLIB实现AOP功能与AOP概念解释 17.使用Spring的注解方式实现AOP入门 18.使用Spring的注解方式实现AOP的细节 19.使用Spring配置文件...

    Spring自动扫描无法扫描jar包中bean的解决方法

    在日常开发中往往会对公共的模块打包发布,然后调用公共包的内容。...spring却无法扫描到相应的bean,下面这篇文章主要给大家介绍了关于Spring自动扫描时无法扫描jar包中bean的解决方法,需要的朋友可以参考下。

    Spring的自动扫描注入.docx

    在 Spring 2.5 中,引入了组件自动扫描机制,该机制可以在类路径下寻找标注了 @Component、@Service、@Controller、@Repository 注解的类,并将这些类纳入 Spring 容器中管理。 @Component、@Repository、@Service...

    让spring解决控制springboot中bean的加载顺序的问题.docx

    只需要把需要注册进容器的bean声明为@Component即可,spring会自动扫描到这个Bean完成初始化并加载到spring上下文容器。 而当你在项目启动时需要提前做一个业务的初始化工作时,或者你正在开发某个中间件需要完成...

    浅谈Spring装配Bean之组件扫描和自动装配

    本篇文章主要介绍了浅谈Spring装配Bean之组件扫描和自动装配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    从零开始学Spring Boot

    1.36 Spring Boot 监控和管理生产环境 1.37 Spring Boot的启动器Starter详解 1.38 Spring Boot集成Redis实现缓存机制 1.39 Spring Boot Cache理论篇 1.40 Spring Boot集成EHCache实现缓存机制 1.41 Spring Boot...

    基于框架的Web开发-装配Bean自动装配.doc

    自动装配(autowiring):Spring自动满足bean之间的依赖。 1 使用@Component定义bean 在类声明的前面使用@Component对类进行标注,这个类可以被spring容器识别,spring容器将类转换为容器管理的bean。 项目分层之后...

    Spring组件自动扫描详解及实例代码

    Spring组件自动扫描详解及实例代码 问题描述 一个系统往往有成千上万的组件,如果需要手动将所有组件都纳入spring容器中管理,是一个浩大的工程。 解决方案 Spring 提供组件扫描(component scanning)功能。它能...

    springboot编译jar包后无法扫描子jar包中的注解解决方法

    springboot 项目编译后无法扫描加载到子jar包中的注解解决方法

    Spring in action 实战中文版(第4版)目录修复版

    2.2 自动化装配bean 35 2.2.1 创建可被发现的bean 35 2.2.2 为组件扫描的bean命名 38 2.2.3 设置组件扫描的基础包 39 2.2.4 通过为bean添加注解实现自动装配 40 2.2.5 验证自动装配 42 2.3 通过Java代码装配 ...

    Spring.pdf

    1. 隐式创建 Java Bean:@Component 注解标注在类上,Spring 会自动的创建类的对象。 2. 显示创建 Java Bean:@Bean 注解,在配置类中创建 Java 对象,明确使用 new 运算创建了对象。 隐式创建 Java Bean 案例演示...

    华为技术专家整理Spring Boot 注解大全.docx

    @ComponentScan 组件扫描,可自动发现和装配一些 Bean。 @Component 可配合 CommandLineRunner 使用,在程序启动后执行一些基础任务。 @RestController 注解是 @Controller 和 @ResponseBody 的合集, 表示这是个...

    Spring中文帮助文档

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...

    Spring API

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    Spring 3 Reference中文

    4.10 类路径扫描和管理的组件.. 96 4.10.1 @Component和更多典型注解 97 4.10.2 自动检测类和bean 的注册. 97 4.10.3 使用过滤器来自定义扫描 98 4.10.4 使用组件定义bean 的元数据.. 99 ...

    Spring核心注解深入解析:提升开发效率

    @Component 和其派生注解(@Repository、@Service、@Controller)标记类为Spring组件,允许Spring通过类路径扫描自动检测和配置这些类。 @Autowired 注解用于自动注入依赖,它可以放置在字段、构造器、setter方法或...

Global site tag (gtag.js) - Google Analytics