springboot注解--基础--04--注解合并,继承

springboot注解–基础–04–注解合并,继承


1、注解的继承

1.1、注解分为两种情况

  1. 类级别 Type(Class,Interface)
    1. 注解仅在 类Class上,且注解上 含有元注解 @Inherited 时,才会被继承
    2. 在 jdk 8 中,接口Interface无法继承任何Type类型注解
  2. 属性和方法级别(Property,Method)
    1. 注解无论何时都会被子类或子接口继承,除非子类或子接口重写

1.2、测试

 
public class IterInheritedTest {

    @Inherited
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface InheritedAnnotationType {}

    @Inherited
    @Target({ElementType.FIELD,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface ABC {
        String value() default "";
    }

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface UnInheritedAnnotationType {}

    @UnInheritedAnnotationType
    static
    class A {}

    @InheritedAnnotationType
    static
    class B extends A {}

    static class C extends B {}

    @UnInheritedAnnotationType
    interface Z {
        @ABC()
        void he();
    }

    @InheritedAnnotationType
    interface Y extends Z {
        @ABC("hhhh")
        void he();
    }

    interface X extends Y {}

    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
        System.out.println(X.class.getAnnotation(InheritedAnnotationType.class));
        System.out.println(Y.class.getAnnotation(InheritedAnnotationType.class));
        System.out.println(Z.class.getAnnotation(InheritedAnnotationType.class));
        System.out.println("_________________________________");
        System.out.println(X.class.getAnnotation(UnInheritedAnnotationType.class));
        System.out.println(Y.class.getAnnotation(UnInheritedAnnotationType.class));
        System.out.println(Z.class.getAnnotation(UnInheritedAnnotationType.class));
        System.out.println("_________________________________");
        System.out.println(Arrays.toString(Z.class.getMethod("he").getAnnotations()));
        System.out.println(Arrays.toString(Y.class.getMethod("he").getAnnotations()));
        System.out.println(Arrays.toString(X.class.getMethod("he").getAnnotations()));
    }
}

输错

null
@org.pzone.crypto.IterInheritedTest$InheritedAnnotationType()
null
_________________________________
null
null
@org.pzone.crypto.IterInheritedTest$UnInheritedAnnotationType()
_________________________________
[@org.pzone.crypto.IterInheritedTest$ABC(value=)]
[@org.pzone.crypto.IterInheritedTest$ABC(value=hhhh)]
[@org.pzone.crypto.IterInheritedTest$ABC(value=hhhh)]




2、注解的合并

2.1、合并的含义

  1. 注解只在springboot中有用
  2. 注解本身并不能被注解继承
  3. 注解合并需要使用:@AliasFor

2.2、合并注解的案例

@RestController = @Controller + @ResponseBody

通过 @AliasFor来完成

2.3、@AliasFor 的4个作用

2.3.1、注释中的显式别名

public @interface ContextConfiguration {
  
  @AliasFor("locations")
  String[] value()default {};
  
  @AliasFor("value")
  String[] locations()default {};
}
 

在@ContextConfiguration中, value和locations是彼此的显式别名。

2.3.2、元注释中属性的显式别名


@ContextConfiguration
public @interface XmlTestConfig {

	# xmlFiles覆盖了@ContextConfiguration中的locations属性。
   @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
   String[] xmlFiles();
}



在@XmlTestConfig中,xmlFiles是@ContextConfiguration中locations的显式别名。

2.3.3、注释中的隐式别名

@ContextConfiguration
public @interface MyTestConfig {

  @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
  String[] value() default {};
  
  @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
  String[] groovyScripts() default {};
  
  @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
  String[] xmlFiles() default {};
}

 
  1. 在@MyTestConfig中,value、groovyScripts、xmlFiles覆盖了@ContextConfiguration中locations属性。
  2. value、groovyScripts、xmlFiles 属性也是彼此的隐式别名。

2.3.4、注释中的传递隐式别名

@MyTestConfig
public @interface GroovyOrXmlTestConfig {
  @AliasFor(annotation = MyTestConfig.class, attribute = "groovyScripts")
  String[] groovy() default {};
  
  @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
  String[] xml() default {};
}

 
  1. groovy 显式覆盖 @MyTestConfig中 groovyScripts属性
  2. xml 显式覆盖 @ContextConfiguration中 locations 属性。
  3. groovy 和 xml 是彼此的可传递隐式别名,因为它们都有效地覆盖了 @ContextConfiguration 中的 locations 属性。

2.4、案例

把多个注解合并成1个,这里把@RestController和@RequestMapping合并为一个@PathRestController

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented

@RestController
@RequestMapping
public @interface PathRestController {
    @AliasFor("path")
    String[] value()default {};
 
    @AliasFor("value")
    String[] path()default {};
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
springboot vue-simple-uploader 是一个前后端分离的文件上传插件,在使用前需要进行相关配置和代码的编写。 首先,在后端部分,我们使用的是Spring Boot框架。需要导入spring-boot-starter-web依赖,并在配置文件中配置相关参数,例如设置文件上传临时目录、文件上传大小限制等。然后,我们需要编写一个处理文件上传请求的Controller类,使用@RequestBody注解接收前端传递的文件信息,并使用multipartFile.transferTo()方法保存文件到指定目录中。 在前端部分,我们使用的是Vue.js框架,并引入vue-simple-uploader插件。首先,我们需要安装该插件,可以使用npm安装或者直接引入插件的CDN地址。然后,在Vue实例中,我们可以通过配置uploaderOptions对象来进行插件的相关配置,例如设置上传的url、自定义headers、文件的最大数量和大小限制等。然后,在需要上传文件的组件中,我们可以通过引入uploader组件,并使用v-model绑定上传的文件列表。 通过上述配置和代码编写,我们就可以实现前后端分离的文件上传功能了。当用户选择上传的文件后,前端会将文件信息发送给后端,后端接收到请求后进行文件保存操作,并返回相应的结果给前端,例如文件的保存路径或者上传成功的提示信息。 总结一下,springboot vue-simple-uploader是一个支持前后端分离的文件上传插件,通过在后端配置文件上传参数和编写Controller类,在前端通过配置uploaderOptions对象和使用uploader组件,我们可以实现文件的上传和保存功能。这样,我们就可以方便地在Spring Boot和Vue.js项目中实现文件上传的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值