栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring--高级工具类--使用/实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring--高级工具类--使用/实例

原文网址:Spring--高级工具类--使用/实例_IT利刃出鞘的博客-CSDN博客

简介

说明

        本文介绍Spring(SpringBoot)中的一些高级工具类。

相关网址

Spring的常用工具类:Spring--常用工具类--使用/实例_IT利刃出鞘的博客-CSDN博客

AnnotationUtils

注解类

package com.example.annotation;
 
import org.springframework.core.annotation.AliasFor;
 
import java.lang.annotation.*;
 
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Inherited
public @interface MyAnnotation {
    @AliasFor(attribute = "location")
    String value() default "";
    
    @AliasFor(attribute = "value")
    String location() default "";
}

控制器

package com.example.controller;

import com.example.annotation.MyAnnotation;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @MyAnnotation(location = "location(my)")
    @RequestMapping("/test")
    public String test() {
        MyAnnotation myAnnotation = null;
        try {
            myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), MyAnnotation.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return "loation:" + myAnnotation.location();
    }
}

测试

postman访问:http://localhost:8080/hello/test

postman结果

loation:location(my)
AnnotatedElementUtils

其他网址

SpringBoot深入理解 -- @AliasFor注解的作用

说明

 AnnotatedElementUtils与AnnotationUtils的不同之处在于:AnnotatedElementUtils可以通过父注解获得子注解上的值,而后者不能。

代码

注解

package com.example.annotation;

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Inherited
public @interface MyAnnotation {
    @AliasFor(attribute = "location")
    String value() default "";
    
    @AliasFor(attribute = "value")
    String location() default "";
}
package com.example.annotation;

import org.springframework.core.annotation.AliasFor;
 
import java.lang.annotation.*;
 
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Inherited
@MyAnnotation
public @interface SubMyAnnotation {
    @AliasFor(annotation = MyAnnotation.class)
    String location() default "";

//    这个不能写,只能有一个与父属性名同名的属性,否则报错
//    @AliasFor(annotation = MyAnnotation.class)
//    String value() default "";
}

控制器

package com.example.controller;

import com.example.annotation.MyAnnotation;
import com.example.annotation.SubMyAnnotation;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @SubMyAnnotation(location = "location(my)")
    @RequestMapping("/test")
    public String test() {
        SubMyAnnotation subMyAnnotation = null;
        MyAnnotation myAnnotation = null;
        MyAnnotation myAnnotation1 = null;

        try {
            subMyAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), SubMyAnnotation.class);
            myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), MyAnnotation.class);
            myAnnotation1 = AnnotatedElementUtils.getMergedAnnotation(this.getClass().getMethod("test"), MyAnnotation.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return "loation(sub):" + subMyAnnotation.location() + "n" +
                "location:" + myAnnotation.location() + "n" +
                "location:" + myAnnotation1.location();
    }
}

 测试

前端访问:http://localhost:8080/hello/test

结果

loation(sub):location(my)
location:
location:location(my)
MultiValueMap

一个key对应多个value。

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest
class DemoApplicationTests {

    @Test
    void test1() {
        MultiValueMap multiValueMap = new LinkedMultiValueMap<>();
        multiValueMap.add("key1", "value1");
        multiValueMap.add("key1", "value2");
        multiValueMap.add("key2", "value2");
        Set stringSet = multiValueMap.keySet();
        for (String key : stringSet) {
            List values = multiValueMap.get(key);
            System.out.println(key + ": "+ values);
        }
    }
}

执行结果

key1: [value1, value2]
key2: [value2]
ClassPathResource

简介

 ClassPathResource用于加载资源文件。

用法

Resource res = new ClassPathResource("xxx");

构造函数参数:路径名。可以是相对路径、绝对路径。比如:"../../file1"。

示例:

Resource res = new ClassPathResource("xxx");      //在当前目录和classpath中寻找"xxx"文件。classpath包含/src/main/resources目录

Resource常用方法

方法作用
public File getFile()返回路径名对应的文件

注意事项

其他网址:SpringBoot打包为JAR包后访问不到Resources下的文件问题 | Homxu

在打包为JAR后,JAR是一个压缩包,不能直接获取文件对象,要改为用流进行读取和创建:

classPathResource = new ClassPathResource("static/satellite.txt");
//打包成jar无法读取文件,要用流读取
//File file = classPathResource.getFile();
InputStream inputStream = classPathResource.getInputStream();

辅助方法

ClassLoader loader = Thread.currentThread().getContextClassLoader();
System.out.println(loader.getResource("").getPath());                  //获取的是classpath的根路径

System.out.println(this.getClass().getResource("").getPath());     //获取的是相对于当前类的相对路径
System.out.println(this.getClass().getResource("/").getPath());    //获取的是classpath的根路径

其他网址

使用Spring的ClassPathResource加载资源文件_太阳虽好,总要诸君亲自去晒,旁人却替你晒不来-CSDN博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946110.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号