zoukankan      html  css  js  c++  java
  • Spring自学教程-注解的使用(三)

    一、java中的注解

    定义注解

    下面是一个定义注解的实例。

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    public @interface Description {
     String value();
    }

    其中的@interface是一个关键字,在设计annotations的时候必须把一个类型定义为@interface,而不能用class或interface关键字。所有的注解类都隐式继承于java.lang.annotation.Annotation,注解不允许显式继承于其他的接口。

    一个注解可以拥有多个成员,成员声明和接口方法声明类似,这里,我们仅定义了一个成员,成员的声明有以下几点限制:

    a)   成员以无入参无抛出异常的方式声明,如boolean value(String str)、boolean value() throws Exception等方式是非法的;

    b)   可以通过default为成员指定一个默认值,如String level() default "LOW_LEVEL"、int high() default 2是合法的,当然也可以不指定默认值;

    c)   成员类型是受限的,合法的类型包括原始类型及其封装类、String、Class、enums、注解类型,以及上述类型的数组类型。如ForumService value()、List foo()是非法的。

    d)   如果注解只有一个成员,则成员名必须取名为value(),在使用时可以忽略成员名和赋值号(=),如@Description("使用注解的实例")。注解类拥有多个成员时,如果仅对value成员进行赋值则也可不使用赋值号,如果同时对多个成员进行赋值,则必须使用赋值号,如@DeclareParents (value = "NaiveWaiter", defaultImpl = SmartSeller.class)。

    e)   注解类可以没有成员,没有成员的注解称为标识注解,解释程序以标识注解存在与否进行相应的处理;

    注解定义包含四个元注解,分别为@Target,@Retention,@Documented,@Inherited。各元注解的作用如下:

    1)  @Target

    表示该注解用于什么地方,可能的 ElemenetType 参数包括:

    Ø  ElemenetType.CONSTRUCTOR 构造器声明。

    Ø  ElemenetType.FIELD 域声明(包括 enum 实例)。

    Ø  ElemenetType.LOCAL_VARIABLE 局部变量声明。

    Ø  ElemenetType.METHOD 方法声明。

    Ø  ElemenetType.PACKAGE 包声明。

    Ø  ElemenetType.PARAMETER 参数声明。

    Ø  ElemenetType.TYPE 类,接口(包括注解类型)或enum声明。

    2)  @Retention

    表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:

    Ø  RetentionPolicy.SOURCE 注解将被编译器丢弃。

    Ø  RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃。

    Ø  RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。

    举一个例子,如@Override里面的Retention设为SOURCE,编译成功了就不要这一些检查的信息,相反,@Deprecated里面的Retention设为RUNTIME,表示除了在编译时会警告我们使用了哪个被Deprecated的方法,在执行的时候也可以查出该方法是否被Deprecated。

    3)  @Documented

    将此注解包含在 javadoc 中

    4)  @Inherited

    允许子类继承父类中的注解

    二、spring中注解的使用

    1、使用Spring注解来注入属性

        @Resource默认按照名称(name="test")进行装配,名称可以通过@resource的name属性设定,当找不到与名称匹配的bean才会按类型装配
        
        注意:如果没有指定name属性,并且安装默认的名称依然找不到依赖对象时,@Resource会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。
    那么在类中怎样使用呢?

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    >
            <context:annotation-config />
    </beans>
     
    在配置文件中加上<context:annotationconfig />将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor注解解析器,这样的话我们就不许要在对象中使用set方法了,更方便开发了。。。有木有

    2、使用Spring注解完成Bean的定义

     以上我们介绍了通过@Autowired或@Resource来实现在Bean中自动注入的功能,下面我们将介绍如何注解Bean,从而从XML配置文件中完全移除Bean定义的配置。

    配置文件
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    > 
        <context:component-scan base-package="com.kedacom.ksoa" /> 
    </beans> 
     
    @Component(不推荐使用)、@Repository、@Service、@Controller
    只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:

    Java代码
    @Component
    public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
        ...
    }

    @Component
    public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
            ...
    }

    使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是userDaoImpl。你也可以指定Bean的名称:
    @Component("userDao")
    @Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:
    @Repository、@Service、@Controller,它们分别对应
    存储层Bean,业务层Bean,和展示层Bean。
    目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替代@Component。

    <context:component-scan />的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
    <context:component-scan />还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:

    过滤器类型 表达式范例 说明
    注解 org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来
    类名指定 org.example.SomeClass 过滤指定的类
    正则表达式 com.kedacom.spring.annotation.web..* 通过正则表达式过滤一些类
    AspectJ表达式 org.example..*Service+ 通过AspectJ表达式过滤一些类

    以正则表达式为例,我列举一个应用实例:

    Java代码
    <context:component-scan base-package="com.casheen.spring.annotation"> 
        <context:exclude-filter type="regex" expression="com.casheen.spring.annotation.web..*" /> 
    </context:component-scan> 

            <context:component-scan base-package="com.casheen.spring.annotation">
                    <context:exclude-filter type="regex" expression="com.casheen.spring.annotation.web..*" />
            </context:component-scan>

    值得注意的是<context:component-scan />配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用<context:component-scan />后,就可以将<context:annotation-config />移除了。 

    总结:
     

     










  • 相关阅读:
    西安.NET俱乐部群 推广代码
    跟我学Makefile(六)
    跟我学Makefile(五)
    跟我学Makefile(四)
    跟我学Makefile(三)
    跟我学Makefile(二)
    Kconfig文件说明2
    Kconfig文件说明
    kernel内核配置说明
    makefile中ifeq与ifneq dev/null和dev/zero简介 dd命令
  • 原文地址:https://www.cnblogs.com/wang3680/p/0f4eea023d8eb01b097c732fddba5725.html
Copyright © 2011-2022 走看看