SpringApplication.run()无需启动

问题描述 投票:0回答:1

我是 Spring 的初学者。我目前正在尝试运行一个非常简单的项目,只是为了开始学习 Spring 并理解 IoC 和依赖注入。起初,我创建了一个简单的 Spring Boot 项目并且它有效,但现在我尝试在不启动的情况下完成它,因为我想更好地了解发生了什么。我不明白该怎么做(下面是完整课程):

SpringApplication.run(TestSpringApplication.class, args);

我尝试这样做:

TestSpringApplication app = new TestSpringApplication();
app.run(args);

但是在 run() 方法中,@Autowired messageRepository 为 null。我猜这是因为我使用 new 而不是 applicationContext.getBean() 创建了 TestSpringApplication ?但是我如何在静态 main 方法中访问/获取 applicationContext ?

请原谅我,如果这听起来微不足道,我在这里几乎从零开始,我很难找到简单的教程。

这是我正在尝试做但不起作用的事情:

package com.cypherf;
import com.cypherf.model.Message;
import com.cypherf.repository.MessageRepository;
import com.cypherf.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class TestSpringApplication {
    @Autowired
    private ApplicationContext context;

    @Autowired
    private MessageRepository messageRepository;

    @Autowired
    private MessageService messageService;

    public static void main(String[] args) {
        TestSpringApplication app = new TestSpringApplication();
        app.run(args);
    }

    public void run(String... args) {
        messageRepository.save(new Message("Message one"));
        messageRepository.save(new Message("Message two"));
        messageRepository.save(new Message("Message two"));

        messageService.printAllMessages();
    }
}

这是我之前使用 Spring Boot 进行的课程,它正在运行:

package com.cypherf;
import com.cypherf.model.Message;
import com.cypherf.repository.MessageRepository;
import com.cypherf.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestSpringApplication implements CommandLineRunner {

    @Autowired
    private MessageRepository messageRepository;

    @Autowired
    private MessageService messageService;

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

    public void run(String... args) throws Exception {
        messageRepository.save(new Message("Message one"));
        messageRepository.save(new Message("Message two"));
        messageRepository.save(new Message("Message two"));

        messageService.printAllMessages();
    }
}
java spring spring-boot inversion-of-control
1个回答
0
投票

如果您没有使用 Spring Boot,那么您必须通过创建

ApplicationContext
bean 来自行设置 Spring 容器。

一种方法是使用

AnnotationConfigApplicationContext
类。例如:

public static void main(String[] args) {
    ApplicationContext context = new ApplicationConfigApplicationContext(TestSpringApplication.class);
    MessageService messageService = context.getBean(MessageService.class);
    messageService.printAllMessages();
}

但这只是为您创建了一个基本的应用程序上下文。如果没有 Spring Boot,您将无法再访问自动配置。这意味着:

  • 如果您依赖于数据库,您现在必须手动创建一个
    DataSource
    bean。
  • 如果您依赖 Spring Data JPA 的存储库,您现在必须手动创建一个
    EntityManagerFactory
    bean 和
    TransactionManager
    (并且还必须配置 Spring Data JPA)。
  • 如果您正在运行 Web 应用程序,您将无法再访问 Spring Boot 的嵌入式 servlet 容器。现在,您要么必须自己配置嵌入式 Tomcat,要么为传统的 WAR 文件部署配置应用程序。
  • 如果
    printAllMessages()
    依赖于日志记录框架,那么您现在也必须自己配置该框架。

此外,像

CommandLineRunner
这样的课程也不再可用。现在你必须听
ContextRefreshedEvent

© www.soinside.com 2019 - 2024. All rights reserved.