近期Java杂项问题

@ConfigurationProperties加载static静态属性为null的问题
解决:@ConfigurationProperties默认是调用非static Setter方法,把静态的Setter方法的static去掉就可以了
@Value注入静态属性
解决:将@Value注解放在静态Setter方法上

SpringMVC

在这里插入图片描述
默认只认识index.jsp,不认识index.html
如何解决:
1.第一种,使SpringMVC管不到这里,比如将/改为/common等。

    protected String[] getServletMappings() {
        return new String[]{"/common/*"};
    }
// springmvc拦截了所有以"/common"为前缀的请求,在匹配的时候也截掉了"/common",用剩下的"/user/login"匹配controller
// 注意此时,请求接口要加一个/common的头,不然404
// http://127.0.0.1:8080/common/user/login
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    public Result login(User user){
        return userService.login(user);
    }
}

2.第二种,将index.html放到page文件夹里,之后让SpringMVC放行静态资源。

@Configuration
@ComponentScan({"controller"})
@EnableWebMvc       // 开启 JSON数据转对象的功能
public class SpringMVCConfig implements WebMvcConfigurer{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/page/**").addResourceLocations("/page/");
    }
}
// 注意: 这里不要使用继承WebMvcConfigurationSupport类的方式,不奏效
// 想使用的话, 要单独再写一个类继承他, 实现addResourceHandlers方法, 然后作为bean, 被SpringMVC扫到

LocalDateTime和时间戳互转

LocalDateTime时间戳(相互转换)
时间戳:从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数
获取时间戳:

// 当前时间戳(单位毫秒, 精确到毫秒) 13位
// 1670597596266
// 1670597596266
// 1670597596266
long p = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
long l = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
long l1 = System.currentTimeMillis();

// 当前时间戳(单位秒, 精确到秒) 10位
// 1670597596
// 1670597596
long l2 = LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8));
long l3 = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).getEpochSecond();

时间戳转换为LocalDateTime:

LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(时间戳/1000或者10位时间戳, 0, ZoneOffset.ofHours(8));
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(13位时间戳), ZoneOffset.ofHours(8));
// 转字符串
String createDate = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

System.currentTimeMillis()

以毫秒的方式返回当前时间。请注意,虽然返回值的时间单位是毫秒,但是这个值的粒度取决于底层操作系统并且可能粒度更大。例如,许多操作系统是以几十毫秒为粒度测量时间的。
return 当前时间和1970年1月1日午夜之间的差值,以毫秒来测量。

System.nanoTime()

以纳秒为单位,这个方法只能被用来测量逝去的时间
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.
返回值表示自某个固定但任意的起始时间(时间基准点)以来的纳秒数(可能在将来,因此值可能为负数)
The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative).
在同一个Java虚拟机实例中,该方法的所有调用都使用相同的某个时间基准点
The same origin is used by all invocations of this method in an instance of a Java virtual machine。other virtual machine instances are likely to use a different origin.
只有当计算从同一个Java虚拟机实例中获得的两个此类值的差值时,才有意义
The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine,is computed.
计算某段代码执行的时间

long startTime = System.nanoTime();
// .... code to be executed
long endTime = System.nanoTime();
long elapsedNanos = endTime - startTime;

使用@Async时的问题

需要注意
启动类上需要开启@EnableAsync
Due to the proxy-based nature of Spring’s AOP framework, calls within the target object are by definition not intercepted.
@Async标注的方法,想要起效果,必须是在另一个类中被调用
Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies.
For JDK proxies, only public interface method calls on the proxy can be intercepted.
被标注的方法必须是public
被标注的方法返回值只能为void或者Future
@Transactional和@Async的实现都是基于AOP,AOP的实现是基于动态代理模式,所以被标注的方法需要被动态代理对象调用

截取字符串

// 截取指定字符串之后的数据
String str = "https://***cs.com/test/IMG_20200505_184916.jpg";
str = str.substring(str.indexOf("com/") + 4);
// 截取string1内分隔符string2后的字符串
String uri="/forebuy";
String method = StringUtils.substringAfterLast(uri,"/fore" );
method="buy"

// substringAfter方法取str内, 首次出现分隔符separator后的字符串
// substringAfterLast方法取str内, 最后一个分隔符separator后的字符串

遗留

@Validated
数据库ID生成策略,批量踢人,redis批量删除,视频流
mybatis返回值是null还是空对象,或者空列表
SpringMVC_ServletMappings路径匹配原理
SpringMVC不能访问html
MySQL中bigint类型无法通过year函数直接转换
SpringCache相关注解
SpringBoot整合Cache使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值