码迷,mamicode.com
首页 > 编程语言 > 详细

spring mvc 注解

时间:2015-04-18 23:47:13      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:spring mvc

    从Spring 2.5 开始提供了完全基于注释配置 Bean、装配 Bean 的功能。我们可以使用基于注释的 Spring IoC 替换原来基于 XML 的配置。以前做项目使用的是xml配置来实现的,这几天在做一个小项目,使用了springmvc的注解,跟大家分享一下。

   使用注解

   Spring 2.5 提供了 AutowiredAnnotationBeanPostProcessor、
CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor 和 RequiredAnnotationBeanPostProcessor 这四个主要的关于 Annotation 的 BeanPostProcessor。
我们可以使用 <context:annotation-config /> 来方便地、一次性的声明者四个 BeanPostProcessor。
     1.Autowired... 提供对 Spring 特有的 Autowired 和 Qualifier 注释。

     2.CommonAnotation... 用于支持 JSR 250 的注释
     3.Persistence... 用于 JPA 的 PersistenceUnit 和PersistenceContext 注释

     4.Required... 用于检查被 Required 注释标记的属性是否被设定。


    原来我们配置xml
    在使用注释配置之前,先来回顾一下传统上是如何配置 Bean 并完成 Bean 之间依赖关系的建立。下面是 3 个类,它们分别是 Office、Car 和 Boss,这 3 个类需要在 Spring 容器中配置为 Bean:


   

package com.baobaotao; public class Office { 
    private String officeNo =”001”;     //省略 get/setter     @Override 
    public String toString() { 
        return "officeNo:" + officeNo;     } 
} 
public class Car {     private String brand;     private double price;     // 省略 get/setter     @Override 
    public String toString() { 
        return "brand:" + brand + "," + "price:" + price;     
} }
package com.baobaotao; public class Boss {     private Car car;     private Office office;     // 省略 get/setter     @Override 
    public String toString() { 
        return "car:" + car + "\n" + "office:" + office;     
} }

   我们在Spring容器中将Office和Car声明为Bean,并注入到Boss Bean中:下面是使用传统 XML 完成这个工作的配置文件beans.xml.
  

<?xml version="1.0" encoding="UTF-8" ?> 
<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">     
<bean id="boss" class="com.baobaotao.Boss">         
<property name="car" ref="car"/>         
<property name="office" ref="office" />     
</bean> 
<bean id="office" class="com.baobaotao.Office">         
<property name="officeNo" value="002"/>     
</bean> 
<bean id="car" class="com.baobaotao.Car" scope="singleton">         
<property name="brand" value=" 红旗 CA72"/>         
<property name="price" value="2000"/>     
</bean> 
</beans>

  

  使用@autowired


   Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。来看一下使用 @Autowired 进行成员变量自动注入的代码。

import org.springframework.beans.factory.annotation.Autowired; 
public class Boss {     
@Autowired     
private Car car;     
@Autowired     
private Office office;
}

   Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明AutowiredAnnotationBeanPostProcessor Bean。
   让@Autowired工作起来

<?xml version="1.0" encoding="UTF-8" ?> 
<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
    <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --> 
    <bean class="org.springframework.beans.factory.annotation.         
AutowiredAnnotationBeanPostProcessor"/>    
 <!-- 移除 boss Bean 的属性注入配置的信息 -->     
<bean id="boss" class="com.baobaotao.Boss"/>
<bean id="office" class="com.baobaotao.Office">         
<property name="officeNo" value="001"/>     
</bean> 
    <bean id="car" class="com.baobaotao.Car" scope="singleton">         
<property name="brand" value=" 红旗 CA72"/>         
<property name="price" value="2000"/>     
</bean> 
</beans> 

   

这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。
按照上面的配置,Spring 将直接采用 Java 反射机制对 Boss 中的 car 和 office 这两个私有成员变量进行自动注入。


    注释配置相对于 XML 配置的对比


1、它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作。如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO 的属性名、类型等信息,如果关系表字段和 PO 属性名、类型都一致,您甚至无需编写任务属性映射信息——因为这些信息都可以通过 Java 反射机制获取。


2、注释和 Java 代码位于一个文件中,而 XML 配置采用独立的配置文件,大多数配置信息在程序开发完成后都不会调整,如果配置信息和 Java 代码放在一起,有助于增强程序的内聚性。而采用独立的 XML 配置文件,程序员在编写一个功能时,往往需要在程序文件和配置文件中不停切换,这种思维上的不连贯会降低开发效率。
因此在很多情况下,注释配置比 XML 配置更受欢迎,注释配置有进一步流行的趋势。Spring 2.5 的一大增强就是引入了很多注释类,现在您已经可以使用注释配置完成大部分 XML 配置的功能。在这篇文章里,我们将向您讲述使用注释进行 Bean 定义和依赖注入的内容。

 

   具体问题具体分析

   总结:以上的对比是理论上的对比,做项目实践后认识到,每个项目到底xml配置还是使用注解来配置,跟项目的大小,需求有莫大的关系,不是“一句注解比XML简单”就要使用的,还是需要具体问题具体分析的。



spring mvc 注解

标签:spring mvc

原文地址:http://blog.csdn.net/wangyongxia921/article/details/45117501

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!