Spring IOC--Bean的装配(使用注解定义Bean)

概述

在spring中,不管是使用xml还是使用注解,实质上都是为spring容器提供bean的定义信息。而spring容器能够成功启动的三大重要因素是:Bean定义信息、Bean实现类、以及spring本身。如果采用XML配置Bean,那么Bean实现类和Bean定义信息是分离的,如果是用注解,那么Bean的定义信息和实现类都是在一起的,表现在Bean实现类的注解上。

下面是一个简单的dao层的bean注解配置:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.qing.spring.dao;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4.   
  5. @Component("userDao")  
  6. public class UserDao {  
  7.   
  8. }  
在UserDao中使用@Component注解对其进行标注,它会被Spring容器识别,然后自动转换成能被容器管理的Bean。它等效于一下的XML配置:
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <bean id="userDao" class="cn.qing.spring.dao.UserDao"/>  
除了 @Component注解外,Spring还提供了3个功能和@Component基本等效的注解,它们分别用于对DAO、service、及web层的controller进行注解,也称这些注解为Bean的泛型注解。

@Repository:用于对dao实现类进行标注。
@Service:用于对service层实现类进行标注。
@Controller:用于对web的控制层实现类进行标注。

之所以在@Component注解之外又定义3个注解,是为了让注解类本身的用途清晰化。此外spring将赋予它们特殊的功能,所以推荐使用特别的注解标注特定的Bean.
只是在Bean的实现类上添加上以上注解还是不够的,spring并不会自动扫描出这些带注解的类并将其转换成Bean,所以需要在spring的配置文件中配置spring需要扫描的包,使用下面的<context:component-scan />标签进行配置:
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- 在使用注解时,在此配置spring要扫描加载的bean的base package -->  
  2.     <context:component-scan base-package="cn.qing.spring"/>  

自动装配Bean:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Repository("userDao")  
  2. public class UserDao {  
  3.   
  4.     @Autowired  
  5.     private PersonInfo personInfo;  
  6.       
  7.     public void printPersonInfo()  
  8.     {  
  9.         System.out.println("personInfo:"+personInfo.toString());  
  10.     }  

上面的personInfo对象是使用 @Autowired注解进行注入。
使用@Autowired注解进行bean属性的自动注入,等同于下面的XML配置:
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <property name="personInfo" ref="personInfo"/>  
而且使用@Autowired注解不需要为要注入的属性对象提供setter和getter方法, @Autowired注解是按照类型进行装配的,在运行时spring查找容器中匹配的bean,当有且只有一个匹配的bean时,spring将其注入到由@Autowired配置的变量中。在使用@Autowired注解是可以结合@Qualifier注解一起使用, @Qualifier注解是用来指定注入Bean的名称,如果指定的名称在spring中不存在,spring容器会报错。
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Repository("userDao")  
  2. public class UserDao {  
  3.   
  4.     @Autowired  
  5.     @Qualifier("personInfo")  
  6.     private PersonInfo personInfo;  

使用注解配置bean的作用范围:

通过注解配置的bean和通过<bean>标签配置的Bean一样,默认的作用范围都是singleton,在使用注解配置scope的时候,spring提供的注解是@Scope,它可以显示指定bean的作用范围。
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @Scope("prototype")  
  2. @Repository("userDao")  
  3. public class UserDao {  
  4.   
  5.     @Autowired  
  6.     @Qualifier("personInfo")  
  7.     private PersonInfo personInfo;  

上面的代码使用@Scope注解将bean的作用范围设置为prototype,这样每次spring容器都返回一个新的bean实例。在xml中配置bean时,可以设置bean的init-method属性和destroy-method属性来配置bean的初始化和销毁时执行的方法,spring支持java中提供的注解:@PostConstruct和@PreDestroy来设置初始化和销毁bean时的方法。
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">    </span>@PostConstruct  
  2.     public void initMethod()  
  3.     {  
  4.         System.out.println("this is init method!");  
  5.     }  
  6.       
  7.     @PreDestroy  
  8.     public void destroyMethod()  
  9.     {  
  10.         System.out.println("this is destroy method.");  
  11.     }  
值得说明的是,当使用@PostConstruct和 @PreDestroy注解配置Bean时,可以有多个对应的方法,spring在执行的时候也会把设置过这两个注解的所有方法都执行。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值