springboot项目启动,自动执行某个方法中的某个类

package com.trs.idap.web.rest.controller;

import com.trs.idap.service.GetCarInfoFromKEDAService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


/**
 * Created by Administrator on 2018/9/29.
 * 描述:项目启动,自动执行类
 * @author Young
 * @create 2018-09-29 14:43
 */
@Component
public class AutoStart implements CommandLineRunner {

    @Value("${spring.data.kafka.producer.topic}")
    private String topic;

    @Autowired
    private GetCarInfoFromKEDAService getCarInfoFromKEDAService;
    @Override
    public void run(String... strings) throws Exception {
        System.out.println("hahahahha");
        System.out.println(topic+"jsdlf");
        String info = getCarInfoFromKEDAService.getInfo();
        System.out.println("topic为:"+topic);
    }
}

注解@Component

实现接口: CommandLineRunner

重写run方法:内部执行的方法

该执行时,也会在所有的bean都创建后执行,且能获取到配置文件中的配置属性.执行结果为:

猜你喜欢

转载自blog.csdn.net/YoungLee16/article/details/82898419