@PostConstruct是java中的注解,用于在对象实例化后执行方法。Spring也实现了它,所以可以直接在Spring项目中使用。@PostConstruct注解用于非静态方法,方法修饰符可以是public、protected、private,但是方法不能有参数,返回值必须为void。@PostConstruct注解的方法在构造函数和依赖注入后执行,执行顺序为Constructor > @Autowired > @PostConstruct。
-
实例如下:
@Service public class DynamicTaskApiImpl{ private List<Long> ids = Lists.newArrayList(); @Autowired private TaskDefinitionService taskDefinitionService; @PostConstruct public void initTask() { //系统启动后,加载任务 for (TaskDefinition td : taskDefinitionService.findAll()) { if (Boolean.TRUE.equals(td.getStart())) { ids.add(td.getId()); } } } }