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 {@AutowiredYouHService youHService;private Map<String, Function<String,String>> m=new HashMap<>();@PostConstructpublic 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 {@AutowiredYouHTypeService 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: 8088path: /helloerror: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>

java:@PostConstruct注解使用相关推荐

  1. java postconstruct_java的@PostConstruct注解

    从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...

  2. Java封装redis工具类RedisUtils,以及在@Postconstruct注解中调用redis可能出现redisTemplate空指针异常

    1.封装redis工具类RedisUtils import org.springframework.data.redis.core.RedisTemplate; import org.springfr ...

  3. java spring框架 注解_史上最全的java spring注解

    史上最全的java spring注解,没有之一 注解是个好东西,但好东西我们也是看见过,整理过,理解过,用过才知道好.不求我们每个都记住,但求保有印象,在需要的时候能提取出来再查找相关资料,平时工作就 ...

  4. 玩转java(Android)注解

    2019独角兽企业重金招聘Python工程师标准>>> 玩转java(Android)注解 1. java标准(原生)注解概览 Java API 中,在java.lang.java. ...

  5. @async注解_史上最全的java spring注解

    史上最全的java spring注解,没有之一 注解是个好东西,但好东西我们也是看见过,整理过,理解过,用过才知道好.不求我们每个都记住,但求保有印象,在需要的时候能提取出来再查找相关资料,平时工作就 ...

  6. postconstruct_@PostConstruct注解,你该好好看看

    在最近的工作中,get到一个很实用的注解,分享给诸位. 痛点 做过微信或支付宝支付的童鞋,可能遇到过这种问题,就是填写支付结果回调,就是在支付成功之后,支付宝要根据我们给的地址给我们进行通知,通知我们 ...

  7. @PostConstruct注解学习

    @PostConstruct注解好多人以为是Spring提供的.其实是Java自己的注解. Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法.被@Pos ...

  8. @PostConstruct注解学习,最详细的分享教程

    该注解可以实现在运行工程时,自动运行该注解下的方法: @PostConstruct是java自带的注解,指的是在项目启动的时候执行这个方法,也可以理解为在spring容器启动的时候执行,可作为一些数据 ...

  9. @PostConstruct注解详解

    简介 javaEE5引入了@PostConstruct和@PreDestroy两个作用于Servlet生命周期的注解,实现Bean初始化之前和销毁之前的自定义操作 使用场景 在项目中主要是在Servl ...

最新文章

  1. 毕业后的第二个月的一点思绪
  2. 相同的字符串哈希值一样吗_关于哈希,来看这里!
  3. linux 下ssh端口反弹,利用ssh隧道反弹shell
  4. 分别是什么意思_美国FBA头程:空派/海派分别是什么意思?
  5. SparkSQL之External Data
  6. .NET 时间格式 ----------摘自MSDN
  7. linux开机自动ZFS,linux – 为什么重新启动导致我的ZFS镜像的一面成为UNAVAIL?
  8. 2. 通用基础技术框架搭建
  9. python在线翻译小程序_Python 做一个翻译小程序
  10. excel中的联系方式导入手机电话簿
  11. 阿里月饼门 vs 阿里价值观
  12. dpdk LRO功能总结
  13. 后端理解ajax和axios
  14. grammarly怎么安装到word
  15. 墨尔本大学计算机科学要求,墨尔本大学计算机科学
  16. ClickHouse-物化视图
  17. 抖音开展大规模打击刷粉、刷量,账号广告导流行动
  18. This'is wath!
  19. mysql慢sql分析平台_慢SQL分析工具 - __KK的个人空间 - OSCHINA - 中文开源技术交流社区...
  20. 19年6月仔细阅读A篇:游戏界声优

热门文章

  1. 关于花呗的一点点想法
  2. 个性化推荐系统之用户画像研究笔记
  3. 华为ensp模拟器实验:端口安全绑定MAC地址ip地址
  4. [C#复习向整合]object与装箱拆箱
  5. r7 3750h和r5 3550h的差距 r7 3750h和r5 3550h哪个好
  6. r5 3500u和r5 3550h性能差多少
  7. Oracle的各个版本升级路线图
  8. Django计算机毕业设计-Steam游戏平台系统论文python(源码程序+lw+远程部署)
  9. 【字符串算法】刷题总结
  10. JQuery外部js定义全局变量