`

Spring自动扫描和管理Bean

阅读更多

在使用beans.xml文件配置容器管理的bean时,即使使用了注解方式来对bean属性进行装配的情况下,如果容器需要管理的bean太多,也会造成beans.xml文件的臃肿,所以spring提供了自动扫描及管理bean的方法。

 

要使用自动扫描功能,需要在配置文件中加入以下代码:

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>     
  2. <beans xmlns="http://www.springframework.org/schema/beans"     
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  4.     <!-- 引入 context 命名空间 -->     
  5.     xmlns:context = "http://www.springframework.org/schema/context"     
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      
  8.     <!-- 加入 context scheme文件 -->     
  9.     http://www.springframework.org/schema/context      
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd">     
  11.        <!-- 打开自动扫描功能 自动扫描功能已经提供了注解功能所需要的处理器,所以不再需要配置打开注解功能(<context:annotation-config/>) -->   
  12.        <context:component-scan base-package="com.risetek"/>    
  13.        ......      
  14. </beans>  
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    <!-- 引入 context 命名空间 -->  
    xmlns:context = "http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    <!-- 加入 context scheme文件 -->  
    http://www.springframework.org/schema/context   
http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
       <!-- 打开自动扫描功能 自动扫描功能已经提供了注解功能所需要的处理器,所以不再需要配置打开注解功能(<context:annotation-config/>) -->
       <context:component-scan base-package="com.risetek"/> 
       ......   
</beans>

 经过上述配置后,在spring容器实例化的时候,容器会自动扫描“com.risetek”包下的所有类,我们只要在需要被容器管理的类上加上一个注解,容器就会将该类纳入容器进行管理了,而表示该类需要被容器管理的注解有以下几种:

   @Component :表示该类是一个通用的Bean
   @Service :表示该类是一个服务层的Bean
   @Controller :表示该类是一个控制层的Bean
   @Repository :表示该类是一个数据访问层的Bean

以上四种形式的注解都将会告诉spring容器,该类需要被容器管理,目前版本的spring对四种注解类型并没有做特殊的处理,四种注解的使用效果都是一样

 

下面以@Componet为例,如何使用注解标识需要被管理的类:

    PersonDaoImpl.java

Java代码 复制代码 收藏代码
  1. //括号中的字符串表明该类在容器中的名称,如果不指定名称,则容器中的名称则为该类的简单名称(类名第一个字母小写)   
  2. @Component(“personDao”)   
  3. public class PersonDaoImpl implements PersonDao {   
  4.     ....   
  5. }  
//括号中的字符串表明该类在容器中的名称,如果不指定名称,则容器中的名称则为该类的简单名称(类名第一个字母小写)
@Component(“personDao”)
public class PersonDaoImpl implements PersonDao {
    ....
}

   PersonServiceBean.java

Java代码 复制代码 收藏代码
  1. @Component  
  2. public class PersonServiceBean implements PersonService {   
  3.     //为属性按名称进行装配   
  4.   
  5.     @Resource(name="personDao")   
  6.     private PersonDao personDao;   
  7. }  
@Component
public class PersonServiceBean implements PersonService {
    //为属性按名称进行装配

	@Resource(name="personDao")
	private PersonDao personDao;
}

 经过配置@Component注解,就将PersonServiceBean和PersonDaoImpl类交给了spring容器管理,在从容器中获取PersonServiceBean实例的时候,容器也会自动将PersonServiceBean中的属性按照@Resource注解方式进行装配。

 

同样也可以通过注解方式设置Bean类的作用域以及初始化、销毁方法

Java代码 复制代码 收藏代码
  1. @Component  
  2. @Scope("prototype")   
  3. public class PersonServiceBean implements PersonService {   
  4.   
  5.     @Resource  
  6.     private PersonDao personDao;   
  7.   
  8.     public PersonServiceBean(){   
  9.         System.out.println("PersonServiceBean....");   
  10.     }   
  11.        
  12.     //此方法会在构造方法执行之后执行   
  13.     @PostConstruct  
  14.     public void init() {   
  15.         System.out.println("init...");   
  16.     }   
  17.        
  18.     //此方法会在对象销毁之前执行   
  19.     @PreDestroy  
  20.     public void destory(){   
  21.         System.out.println("destory...");   
  22.     }   
  23. }  
分享到:
评论

相关推荐

    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解决控制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 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方法或...

    基于Spring Boot框架的员工管理系统的设计与实现-初始框架.zip

    2,使编码变得简单,SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值...

    spring+springmvc+mybatis的整合

    2.6 spring-bean 配置mapper自动扫描 MapperScannerConfigurer将扫描basePackage所指定的包下的所有接口类(包括子类), 如果它们在SQL映射文件中定义过,则将它们动态定义为一个Spring Bean, 这样,我们在...

Global site tag (gtag.js) - Google Analytics