java基础之一注释

来源:互联网 发布:淘宝店铺标题图片 编辑:程序博客网 时间:2024/06/07 08:49

java注释:详见点击打开链接

自定义注释:使用@interface关键字
@Target(ElementType.TYPE)public @interface Table {    /**     * 数据表名称注解,默认值为类名称     * @return     */    public String tableName() default "className";}

以下为spring提供的@order和@value的注解示范
第一步:定义注解
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Value {    String value();}
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})public @interface Order {    int value() default 2147483647;}
其中:
@Target:表示该注解使用的场景,方法,字段属性或者其让
@Retention:表示该注解在什么情况下使用,例如runtime时使用
第二步:配置该注解
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">      <property name="location" value="hanlp.properties" />  </bean>
第三步:使用该注解
 @Value("${user.answer.timeout}")    private Integer answerTimeout;
配置文件中会有:user.answer.timeout=3000
这样
answerTimeout值为3000.





原创粉丝点击