Spring3.0注解标签


在传统Spring架构中配置POJOs的基本操作有两种:装配和依赖注入
  1. package test; 
  2. import org.springframework.context.ApplicationContext;   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  4. public class Main {   
  5.     private BasicService service;   
  6.     public BasicService getService() {   
  7.         return service;   
  8.     }   
  9.     public void setService(BasicService service) {   
  10.         this.service = service;   
  11.     }   
  12.     public void print() {   
  13.        service.print();   
  14.     }   
  15.     public static void main(String[] args) {   
  16.         String[] locations = { "beans.xml" };   
  17.         ApplicationContext ctx =   
  18.             new ClassPathXmlApplicationContext(locations);   
  19.         Main main = (Main)ctx.getBean("main");   
  20.         main.print();   
  21.     }   

  1. package test;   
  2. import org.springframework.context.ApplicationContext;   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  4. public class Main {   
  5.     private BasicService service;   
  6.     public BasicService getService() {   
  7.         return service;   
  8.     }   
  9.     public void setService(BasicService service) {   
  10.         this.service = service;   
  11.     }   
  12.     public void print() {   
  13.        service.print();   
  14.     }   
  15.     public static void main(String[] args) {   
  16.   
  17.         String[] locations = { "beans.xml" };   
  18.         ApplicationContext ctx =   
  19.             new ClassPathXmlApplicationContext(locations);   
  20.   
  21.         Main main = (Main)ctx.getBean("main");   
  22.         main.print();   
  23.     }   

 
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">   
  5.   
  6.     <bean id="main" class="test.Main">   
  7.         <property name="service">   
  8.             <ref bean="service" />   
  9.         </property>   
  10.     </bean>   
  11.     <bean id="service" class="test.BasicService"></bean>   
  12. </beans> 
对 于传统装配方式而言,最大的问题在于规模逐渐变大的项目中将会有越来越多的POJOs需要在XML文件中设置。这样一方面无法迅速定位指定的对象,另一方 面难于掌握对象之间的依赖关系。得益于Java5.0的注释功能,到Spring2.5之后,其架构中提供了一系列注释,用于简化装配POJOs的过程。 这种方式大大降低了传统XML配置文件的管理成本,让我们来看一下将上面的例子修改成注释方式的样子。


@Autowired
@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。
@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。要使@Autowired能够工作,还需要在配置文件中加入以下
:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
Spring 将直接采用 Java 反射机制对私有成员变量进行自动注入。所以对成员变量使用 @Autowired 后,您大可将它们的 setter 方法()删除。
Autowired 是根据类型进行自动装配的。
例如,如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出 BeanCreationException异常;
          如果当Spring上下文中不存在UserDao类型的bean,也会抛出 BeanCreationException异常。
          这时我们可以使用@Qualifier配合@Autowired来解决这些问题。如下:
可能存在多个UserDao实例
  • @Autowired       
  • @Qualifier("userServiceImpl")      //用@Qualifier()避免异常  
  • public IUserService userService;
@Resource
推荐使用它来代替Spring专有的@Autowired注解。@Resource的作用相当于@Autowired,
只不过@Autowired按by Type自动注入,而@Resource默认按by Name自动注入
@Resource有两个属性是比较重要的,分别是name和type,Spring将 @Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。 所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定 type属性,这时将通过反射机制使用byName自动注入策略。要使@Autowired能够工作,还需要在配置文件中加入以下:
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
@Resource装配顺序

a.如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

b.如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常

c.如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常

      d.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型
        (UserDao)进行匹配,如果匹配则自动装配;

@Scope

在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作

eg:

   
   
  1. @Scope("session")        
  2. @Component()        
  3. public class UserSessionBean implements Serializable{      
  4.  ... ...     




Spring 2.5 在 @Repository 的基础上增加了功能类似的额外三个注解:@Component、@Service、@Constroller,它们分别用于软件系统的不同层次:

@Compoent  是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
       @Service  通常作用在业务层,但是目前该功能与 @Component 相同。
       @constroller通常作用在控制层,但是目前该功能与 @Component 相同。
通过在类上使用 @Repository、@Component、@Service 和 @Constroller 注解,Spring 会自动创建相应的 BeanDefinition 对象,并注册到 ApplicationContext 中。这些类就成了 Spring 受管组件。这三个注解除了作用于不同软件层次的类,其使用方式与 @Repository 是完全相同的.

//Spring 配置文件中启用注解标签及自动扫描功能
 <context:annotation-config />
 <context:component-scan base-package="com.revenue"></context:component-scan>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值