Spring扩展-01


Spring扩展-01

1.初始化Bean时执行指定的方法

  1. 使用Java注解@PostConstruct实现。
@Slf4j
@Component
public class AnnotationInitBean {

    @PostConstruct
    public void init() {
        log.info("通过 @PostConstruct 在创建Bean时执行init方法");
    }
}
  1. 实现Spring中的InitializingBean接口。
@Slf4j
@Component
public class SpringInitBean implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("实现了 InitializingBean 接口在Bean初始化是会执行,afterPropertiesSet方法。");
    }
}

2.Web容器Tomcat启动后执行任务

  1. 实现CommandLineRunner接口。
@Slf4j
@Component
public class SpringContainerAfterInitBean implements CommandLineRunner {
    
    @Override
    public void run(String... args) throws Exception {
        log.info("Tomcat启动。。。");
    }
}
  1. 实现ApplicationRunner接口。
@Slf4j
@Component
public class SpringContainerAfterInitBean implements ApplicationRunner {
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
		log.info("Tomcat启动。。。");
    }
}
  1. ApplicationRunner和CommandLineRunner接口的区别,实现run()方法需要的参数不同。

3.Spring循环依赖的解决方法

  1. @Lazy打破等待添加。
@Slf4j
@Service
public class UserService {

    @Lazy
    @Resource
    private AccountService accountService;
}

@Slf4j
@Service
public class AccountService {

    @Resource
    private UserService userService;
}

4.Spring开启定时任务的两种方式

  1. ThreadPoolTaskScheduler+Trigger。
@Slf4j
@Component
public class AnnotationInitBean {

    @PostConstruct
    public void init() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(10);
        scheduler.initialize();

        String cron = "0/1 * * * * ?";
        scheduler.schedule(() -> log.info("定时任务 -->> "), new CronTrigger(cron));
    }
}
  1. 注解,@EnableScheduling+@Scheduled。
@EnableScheduling
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

@Slf4j
@Component
public class AnnotationInitBean {
    
    @Scheduled(cron = "0/1 * * * * ?")
    public void schedule() {
        log.info("注解开启定时任务 -->> ");
    }
}

3. scheduleAtFixedRate()和scheduleWithFixedDelay()的区别

@Slf4j
@Component
public class AnnotationInitBean {

    @PostConstruct
    public void init() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(10);
        scheduler.initialize();

        // scheduleAtFixedRate() 每隔两秒执行。
        scheduler.scheduleAtFixedRate(() -> {
            try {
                TimeUnit.SECONDS.sleep(2L);
                log.info("Rate ----------->");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 1000L);

        // scheduleWithFixedDelay() 每隔三秒执行,1+2,执行2秒后再延时1秒钟。
        scheduler.scheduleWithFixedDelay(() -> {
            try {
                TimeUnit.SECONDS.sleep(2L);
                log.info("Delay ----------->");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 1000L);
    }
}