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. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值