0
点赞
收藏
分享

微信扫一扫

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


简介

说明

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

相关网址

Spring的常用工具类

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

其他网址


说明

 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<String, String> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("key1", "value1");
multiValueMap.add("key1", "value2");
multiValueMap.add("key2", "value2");
Set<String> stringSet = multiValueMap.keySet();
for (String key : stringSet) {
List<String> 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的根路径


举报

相关推荐

0 条评论