后端初始化变量,InitializingBean,static代码块

版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/86515799

1InitializingBean初始化变量

@Service
public class AirService implements InitializingBean {

    public static final Logger logger = LoggerFactory.getLogger(AirService.class);

    public static List<Integer> toBeUsedList2 = new ArrayList<>();


    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("afterPropertiesSet...");
        toBeUsedList2.add(1);
        toBeUsedList2.add(2);
        toBeUsedList2.add(3);
    }

    @Scheduled(cron = "0/2 * * * * ?")
    public void f1() {
        logger.info(toBeUsedList2.toString());
    }

}

INFO: 2019-01-16 10:07:27 [com.weather.weatherexpert.service.AirService:55] afterPropertiesSet...
INFO: 2019-01-16 10:07:30 [org.springframework.boot.web.embedded.tomcat.TomcatWebServer:206] Tomcat started on port(s): 8080 (http) with context path ''
INFO: 2019-01-16 10:07:30 [com.weather.weatherexpert.WeatherExpertApplication:59] Started WeatherExpertApplication in 10.767 seconds (JVM running for 13.384)
INFO: 2019-01-16 10:07:30 [com.weather.weatherexpert.initialize.StarterClass1:22] StarterClass1...
INFO: 2019-01-16 10:07:30 [com.weather.weatherexpert.initialize.StarterClass2:22] StarterClass2...
INFO: 2019-01-16 10:07:32 [com.weather.weatherexpert.service.AirService:63] [1, 2, 3]
INFO: 2019-01-16 10:07:34 [com.weather.weatherexpert.service.AirService:63] [1, 2, 3]
INFO: 2019-01-16 10:07:36 [com.weather.weatherexpert.service.AirService:63] [1, 2, 3]

2 static代码块初始化变量


/**
 * <p>Title: </p>
 * <p>Description: </p>
 *
 * @Author
 * @CreateTime
 */
@Service
public class AirService {

    public static final Logger logger = LoggerFactory.getLogger(AirService.class);


    public static List<Integer> toBeUsedList = new ArrayList<>();

    static {
        logger.info("static...");
        toBeUsedList.add(1);
        toBeUsedList.add(2);
        toBeUsedList.add(3);
    }

    @Scheduled(cron = "0/2 * * * * ?")
    public void f1() {
        logger.info(toBeUsedList.toString());
    }
}

INFO: 2019-01-16 10:10:53 [com.weather.weatherexpert.service.AirService:28] static...
INFO: 2019-01-16 10:10:56 [org.springframework.boot.web.embedded.tomcat.TomcatWebServer:206] Tomcat started on port(s): 8080 (http) with context path ''
INFO: 2019-01-16 10:10:56 [com.weather.weatherexpert.WeatherExpertApplication:59] Started WeatherExpertApplication in 8.479 seconds (JVM running for 12.369)
INFO: 2019-01-16 10:10:56 [com.weather.weatherexpert.initialize.StarterClass1:22] StarterClass1...
INFO: 2019-01-16 10:10:56 [com.weather.weatherexpert.initialize.StarterClass2:22] StarterClass2...
INFO: 2019-01-16 10:10:58 [com.weather.weatherexpert.service.AirService:36] [1, 2, 3]
INFO: 2019-01-16 10:11:00 [com.weather.weatherexpert.service.AirService:36] [1, 2, 3]
INFO: 2019-01-16 10:11:02 [com.weather.weatherexpert.service.AirService:36] [1, 2, 3]

end

猜你喜欢

转载自blog.csdn.net/u010002184/article/details/86515799