执行顺序:
Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
项目应用:
在项目中初始化Servlet时加载一些配置等。
注意事项:
使用此注解时会影响到服务的启动时间。
被注解方法不得有任何参数;
被注解方法返回值为void;
被注解方法需是非静态方法;
此方法只会被执行一次;
示例:
@Component
public class Abc {
private SystemParam systemParam;
@Autowired
private UserService userService;
@PostConstruct
private void initSystemParam() {
User user = userService.selectUser();
this.systemParam = new SystemParam(user.getKey(), user..getValue());
}
}