启动一个简单的非基于 Web 的 java 应用程序的官方 spring 启动方式是什么?

What is the official spring boot way to start a simple non web based java application?

我正在将一个简单的 java 项目转换为 spring 启动变体。 Spring 引导参考指南 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ has been very helpful in general, but most examples of setting up a simple configuration involve some web based application. The getting started tutorial from https://spring.io/guides/gs/spring-boot/ 教程也没有提供我正在寻找的答案。

我有一个 class HelloSpring,我需要 运行 printHello() 上的一个方法。我已经配置了以下 classes,为简单起见放在同一个包中:

Application.class

@SpringBootApplication
public class Application {

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

}

HelloConfiguration.class

@Configuration
public class HelloConfiguration {

    @Bean
    public HelloSpring helloSpring(){
        HelloSpring hs = new HelloSpring();
        hs.printHello();
        hs.printHelloAgain();
        return hs;
    }

    @Autowired
    public HelloSpring hs;

}

HelloSpring.class

public class HelloSpring {

    public void printHello() {
        System.out.println("Hello Spring!");
    }

    @PostConstruct
    public void printHelloAgain() {
        System.out.println("Hello Spring?");
    }

}

它打印(spring 日志省略):

Hello Spring!
Hello Spring?
Hello Spring?

但是,我不确定执行 HelloSpring class.

的正确方法

上面的例子,当使用spring boot 时,"run" a class 的官方接线方式是什么?

只需使用 ApplicationContextSpringApplication.run returns 然后使用它。这几乎是所有需要的

public static void main(String[] args) {
    ApplicationContext context = SpringApplication.run(Application.class, args);
    HelloSpring bean = context.getBean(HelloSpring.class);
    bean.printHello();
}

因此您可以打开一个 gui 等,并使用 ApplicationContext 获取您的 bean 等。

来自文档:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-command-line-runner

Application.class

@SpringBootApplication
public class Application {

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

}

HelloSpring.class

@Component
public class HelloSpring implements CommandLineRunner {
    @Override
    public void run(String... args) {
        this.printHello();
    }

    public void printHello() {
        System.out.println("Hello Spring!");
    }
}

您甚至可以让 运行() 方法实际打印出您的消息,但这样可以使它更接近您实现方法并希望在应用程序启动时执行的意图。

第 1 步: 默认情况下 使用 Web 应用程序创建一个 Springboot 项目。 Pom.xml:

<!-- Springboot for web application -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Step2: 将以下配置添加到 application.properties 文件。该应用程序将是非网络应用程序:

spring.main.web-environment=false
spring.main.banner-mode=off

第 3 步: Springboot 加载程序,如 Web 应用程序:

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

step4: 非网络应用代码:

//uncomment @component to run non-web app mode with the above config
// comment @component to run web-app  mode without the above config
@Component
public class NonWebApp implements CommandLineRunner{
    @Override    
    public void run(String... args) throws Exception {
        System.out.println("************* better than best ************************");
   }
}

好的,声音你想使用 Spring 框架作为依赖注入。好的,您正在理解一些 spring 核心注释,例如之前: @Configuration 是告诉 spring 组件需要扫描为 spring 组件并放入 spring IoC 的方法。 Bean 是存储在spring IoC 中的对象。能够创建@Bean class,我们需要创建带有@Configuration 注解的class。

创建你的 Bean

@Configuration
public class HelloConfiguration{
@Bean
public HelloSpring helloSpring(){
    HelloSpring hs = new HelloSpring();
    return hs;
 }}

您的服务Class

public class HelloSpring {

public void printHello() {
    System.out.println("Hello Spring!");
}

public void printHelloAgain() {
    System.out.println("Hello Spring?");
}

@PostConstruct 知道 JavaEE 使用的注释,并告诉 spring 需要在 spring IoC 中初始化 bean 时执行。

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

或者你可以这样做:

@Configuration
@EnableAutoConfiguration
@@Import(HelloConfiguration.class) 
public class Application {

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

@SpringBootApplication == @Configuration & @EnableAutoConfiguration & @Import.

如果你想在你的 spring 引导应用程序已经启动时 运行 你的方法,在某些情况下你需要缓存数据并放入你的应用程序内存缓存,初始方法作为回调。有两种方法可以做到这一点:

将您的 class 创建为 spring 组件并实施 ApplicationRunner 或 CommandLineRunner。 ApplicationRunner:[https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/ApplicationRunner.html] 提供我们可以提供应用程序参数的方法运行。 CommandLineRunner:[https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/CommandLineRunner.html] 还提供方法 运行 作为回调,我们可以提供 String[] 参数。

例子

@Component
@Order(1)
public class MyRunner implement ApplicationRunner {

private final HelloSpring helloSpring 

public MyRunner(HelloSpring helloSpring){ // constructer injection
       this.helloSpring=helloSpring;
}

@Override
public void run(ApplicationArguments args) throws Exception {
   helloSpring.printHello();
   helloSpring.printHelloAgain();
   } 
}