建立网站ppt,做酒店网站所用到的算法,网站做框架,wordpress好用中文插件一、java中的注解定义注解下面是一个定义注解的实例。Target(ElementType.TYPE)Retention(RetentionPolicy.RUNTIME)DocumentedInheritedpublic interface Description { String value();}其中的interface是一个关键字#xff0c;在设计annotations的时候必须把一个类型定义为…一、java中的注解定义注解下面是一个定义注解的实例。Target(ElementType.TYPE)Retention(RetentionPolicy.RUNTIME)DocumentedInheritedpublic 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默认按照名称(nametest)进行装配,名称可以通过resource的name属性设定当找不到与名称匹配的bean才会按类型装配 注意如果没有指定name属性并且安装默认的名称依然找不到依赖对象时Resource会回退到按类型装配。但一旦指定了name属性就只能按名称装配了。那么在类中怎样使用呢beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://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 xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://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-packagecom.kedacom.ksoa / /beans Component不推荐使用、Repository、Service、Controller 只需要在对应的类上加上一个Component注解就将该类定义为一个Bean了Java代码 Component public class UserDaoImpl extends HibernateDaoSupport implements UserDao { ... } Componentpublic 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-packagecom.casheen.spring.annotation context:exclude-filter typeregex expressioncom\.casheen\.spring\.annotation\.web\..* / /context:component-scan context:component-scan base-packagecom.casheen.spring.annotation context:exclude-filter typeregex expressioncom\.casheen\.spring\.annotation\.web\..* / /context:component-scan值得注意的是context:component-scan /配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能同时还启用了注释驱动自动注入的功能即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor因此当使用context:component-scan /后就可以将context:annotation-config /移除了。 总结 来自为知笔记(Wiz)转载于:https://www.cnblogs.com/wang3680/p/0f4eea023d8eb01b097c732fddba5725.html