java:@PostConstruct注解使用
1 使用条件
1.1 方法是非静态方法
1.2 注解修饰的方法,不可以具有参数
否则抛错:
Lifecycle method annotation requires a no-arg method
1.3 注解修饰的方法,返回值是void
实际上,可以有返回值,springboot启动没有抛错,但一般仅用于初始化bean,添加些缓存数据用,故而一般不需要返回值:
@PostConstruct
public String Yhinit(){
log.error("我来啦,今天需要加载优惠券的三种方式:{}","谢谢");
m.put("红包来啦",r->youHService.RedPaper(r));
m.put("购物券来啦",r->youHService.ShoppingQuan(r));
m.put("腾讯Vip来啦",r->youHService.TengXunVip(r));
return "aa";
}
2 实际使用
在service中,增加多种不同类型服务的逻辑:
package com.xiaoxu.service;
import org.springframework.stereotype.Service;
/**
* @author xiaoxu
* @date 2022-01-24
* Ymybatis:com.xiaoxu.service.YouHService
*/
@Service
public class YouHService {
public String RedPaper(String resourceType){
return "红包";
}
public String ShoppingQuan(String resourceType){
return "购物券";
}
public String TengXunVip(String resourceType){
return "腾讯Vip";
}
}
然后service下,新增各个服务的获取组件,通过@PostConstruct注解来初始化bean的数据:
package com.xiaoxu.service;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/**
* @author xiaoxu
* @date 2022-01-24
* Ymybatis:com.xiaoxu.service.YouHTypeService
*/
@Slf4j
@Service
public class YouHTypeService {
@Autowired
YouHService youHService;
private Map<String, Function<String,String>> m=new HashMap<>();
@PostConstruct
public Map<String,Integer> Yhinit(){
log.error("我来啦,今天需要加载优惠券的三种方式:{}","谢谢");
m.put("红包来啦",r->youHService.RedPaper(r));
m.put("购物券来啦",r->youHService.ShoppingQuan(r));
m.put("腾讯Vip来啦",r->youHService.TengXunVip(r));
return ImmutableMap.<String,Integer>builder().put("1",12).build();
}
public String getResult(String resourceType){
Function<String,String> f = m.get(resourceType);
if(f!=null){
return f.apply(resourceType);
}
return "找不到优惠券的类型";
}
}
这里需要使用到函数式接口Function的apply方法,该方法,根据定义的Function<T,R>类型,获取一个T类型参数,返回一个R类型结果:
然后新建个controller进行测试,注入上面使用@PostConstruct来缓存数据的service服务:
package com.xiaoxu.controller;
import com.google.common.collect.ImmutableMap;
import com.xiaoxu.service.YouHTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author xiaoxu
* @date 2022-01-24
* Ymybatis:com.xiaoxu.controller.YouHuiQuanController
*/
@RestController
public class YouHuiQuanController {
@Autowired
YouHTypeService youHTypeService;
@PostMapping(value = "/Youh2")
public Map<String,Object> getYouhuiTypesNew2(@RequestParam("type") String resourceType){
return ImmutableMap.<String,Object>builder()
.put("1",youHTypeService.getResult(resourceType))
.build();
}
}
执行springboot主程序类,打印如下:
postman请求结果如下:
2.1:
2.2:
2.3:
注意:
1.如果希望springboot封装的500异常,不返回trace,那么可以为application.yml增加上如下配置:
server:
port: 8088
path: /hello
error:
include-stacktrace: never
其中,server.error.include-stacktrace设置为never时,500异常就不会返回trace,比如对于上面@PostMapping,对于注解@RequestParam(“type”) String resourceType,如果前端传值的key名称和@RequestParam不符合,那么会抛错:
Required request parameter 'type' for method parameter type String is not present
以下的500异常响应,不包含trace:
去掉yml的trace配置,trace展示如下:
tips:使用了@RequestParam(“type”) String resourceType,那么前端传值的key只能为type,即使是传形参resourceType也是不行的。
2.yml希望有配置时的提示,pom.xml安装configuration-processor依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
效果如下:
3 ImmutableMap工具类使用,需要配置guava的依赖:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.0-jre</version>
</dependency>