@PostContruct注解服务启动后运行方法

@PostContruct是spring框架的注解,在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法,或者理解为在系统在接口正常访问之前去执行该注解标注的方法

注解的加载顺序
Constructor >> @Autowired >> @PostConstruct

@PostConstruct注解的方法将会在依赖注入完成后被自动调用

两种使用方式:

import com.alibaba.fastjson.JSONArray;
import com.sys.yang.dao.entity.first.FirstUser;
import com.sys.yang.service.impl.FirstUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Created by yangLongFei on 2021/9/6 14:39
 * Version: $
 * Description: 服务启动后自动执行的操作
 */
@Component
public class PostConstructTest {


    @Autowired
    private FirstUserServiceImpl firstUserService;


    @PostConstruct
    public void init0() {
        System.out.println("一一一一一一一一一一一一");
        System.out.println("....@@@@.....@@@@...");
        System.out.println(".........$$.........");
        System.out.println("........&&&&&........");
        System.out.println("....................");

        List<FirstUser> list = firstUserService.list();
        System.out.println(JSONArray.toJSONString(list));

    }

    public @PostConstruct void init1() {
        System.out.println("二二二二二二二二二二二二");
        System.out.println("....@@@@.....@@@@...");
        System.out.println(".........$$.........");
        System.out.println("........&&&&&........");
        System.out.println("....................");

        List<FirstUser> list = firstUserService.list();
        System.out.println(JSONArray.toJSONString(list));

    }


}

目前来看项目中还没有用到过,每次服务启动的时候,都要执行的代码

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值