@Component
public class SegmentAddDataListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private AttractionsRepository attractionsRepository;
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
if(contextRefreshedEvent.getApplicationContext().getParent() == null)//root application context 没有parent,他就是老大.
{
//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。
List<Attractions> attractionsList = attractionsRepository.findAll();
System.out.println(attractionsList.toString());
System.out.println("\n\n\n\n\n______________\n\n\n加载了\n\n_________\n\n");
}
}
}
第二种方式:CommandLineRunner