Java Spring引导无请求运行控制器

Java Spring引导无请求运行控制器,java,spring-boot,Java,Spring Boot,有没有办法在Spring Boot启动Tomcat之前运行一个控制器来初始化一些数据 我当前的代码如下所示: @SpringBootApplication public class Application { public static void main(String[] args) { AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class)

有没有办法在Spring Boot启动Tomcat之前运行一个控制器来初始化一些数据

我当前的代码如下所示:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        Controller controller = (Controller) context.getBean("controller");
        controller.start();
        context.close();

        SpringApplication.run(Application.class, args);
    }
}

@Controller
@Component("controller")
public class Controller {
    @Autowired
    private Runner runner;

    public void start() {
        runner.test();
    }
}

@Configuration
@PropertySource("classpath:config.properties")
@Component("runner")
public class Runner {
    @Value("${name}")
    private String name;

    public void test() {
        System.out.println("hello " + name)
    }

    public String getName() {
        return name;
    }
}

@Controller
public class HelloController {
    @Autowired
    private Runner runner;

    @RequestMapping("/hello")
    public CalenderCollection data(@PathVariable("name")String name, Model model) {
        model.addAttribute("name", runner.getName());
        return "hello";
    }
}

@Configuration
@ComponentScan(basePackages = "com.test")
public class AppConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
这会将正确的名称打印到控制台中。但是当我访问url时,
runner
为空。然后我考虑将应用程序和控制器类更改为:

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

@Controller
@Component("controller")
public class Controller {
    @Autowired
    private Runner runner;

    public Controller() {
        runner.test();
    }
}

但是现在我遇到了一个问题,
runner
一开始就是空的。一开始收集一些数据,然后继续这个过程的正确方法是什么

如果您只是在应用程序启动时尝试运行一些代码,则无需使用Controller。Spring为此类用例提供了各种应用程序生命周期挂钩

代码很可能如下所示

@Component
public class MyListener 
        implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private Runner runner;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        runner.test();
    }
}
@组件
公共类MyListener
实现ApplicationListener{
@自动连线
私人跑步者;
ApplicationEvent(ContextRefreshedEvent事件)上的公共无效{
runner.test();
}
}

查看博客文章和部分文档以了解更多信息

如果您只是尝试在应用程序启动时运行一些代码,则无需使用Controller。Spring为此类用例提供了各种应用程序生命周期挂钩

代码很可能如下所示

@Component
public class MyListener 
        implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private Runner runner;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        runner.test();
    }
}
@组件
公共类MyListener
实现ApplicationListener{
@自动连线
私人跑步者;
ApplicationEvent(ContextRefreshedEvent事件)上的公共无效{
runner.test();
}
}

查看博客文章和部分文档以了解更多信息

如果要初始化某些值,可以执行以下操作:

@SpringBootApplication
public class Application implements CommandLineRunner {

  @Override
  public void run( String... args ) throws Exception {
    //initialise your value here
  }
}

如果要初始化某些值,可以执行以下操作:

@SpringBootApplication
public class Application implements CommandLineRunner {

  @Override
  public void run( String... args ) throws Exception {
    //initialise your value here
  }
}

在应用程序启动之前,通常使用
ApplicationRunner
CommandLineRunner
运行一些代码

如果您需要在Spring应用程序启动后运行某些特定代码 开始时,可以实现ApplicationRunner或CommandLineRunner 接口。两个接口以相同的方式工作,并提供单一的 run方法,该方法将在SpringApplication.run(…​) 完成

CommandLineRunner接口提供对应用程序的访问 参数作为简单字符串数组,而ApplicationRunner使用 上面讨论的ApplicationArguments接口


在应用程序启动之前,通常使用
ApplicationRunner
CommandLineRunner
运行一些代码

如果您需要在Spring应用程序启动后运行某些特定代码 开始时,可以实现ApplicationRunner或CommandLineRunner 接口。两个接口以相同的方式工作,并提供一个 run方法,该方法将在SpringApplication.run(…​) 完成

CommandLineRunner接口提供对应用程序的访问 参数作为简单字符串数组,而ApplicationRunner使用 上面讨论的ApplicationArguments接口


如果这是只影响您可以使用的类的内容

@PostConstruct 
在控制器类的方法上

<>如果这是与整个应用程序相关联的东西,你应该考虑。
ApplicationReadyEvent

上创建应用程序侦听器,如果此操作只影响您可以使用的类

@PostConstruct 
在控制器类的方法上

<>如果这是与整个应用程序相关联的东西,你应该考虑。
ApplicationReadyEvent

上创建应用程序侦听器,您真的需要在tomcat启动之前执行此操作吗?或者可以在上下文启动期间执行此操作吗?或者可以在上下文启动期间执行此操作吗?谢谢。我刚刚注意到tomcat在运行之前启动。是吗不可能翻转它们吗?谢谢。我刚刚注意到tomcat在跑步者之前启动。可以翻转它们吗?