Spring Core 作为 Spring Boot

Spring Core as Spring Boot

提问人:Tilek Zholdoshbek 提问时间:11/8/2023 更新时间:11/8/2023 访问量:59

问:

我有一个包含实体、存储库、服务和 rest 控制器的 Spring Boot 项目。一切都有效,但我的导师给了我一个任务,我必须在没有@SpringBootApplication的情况下开始我的申请。

那么,如何在不创建 webapp、xml 尤其是任何 html 文件的情况下实现它呢?

换句话说,我怎样才能用f.ex替换注释。Java 自定义(或不)类?

谢谢!

java spring-boot spring-mvc tomcat

评论

1赞 Roman C 11/8/2023
你尝试了什么?您应该明确需要启动哪个应用程序。
0赞 dan1st 11/8/2023
你还允许在没有的情况下使用Spring Boot吗?@SpringBootApplication
0赞 Tilek Zholdoshbek 11/8/2023
我有一个简单的Spring Boot应用程序,它有不同的端点,但现在我需要运行我的项目,以便我可以调用我的端点,但根本不使用@SpringBootApplication。不,我不被允许使用 Spring Boot。

答:

0赞 Peng 11/8/2023 #1

注解是为了方便加载到spring上下文中,它位于包中,一般情况下,会自动注入需要的bean,比如Web应用场景,会注入tomcat、netty、oracle、mysql、pgsql等网络上下文。SpringBootApplicationspring-boot-auto-configure-starterSpringBootApplication

简而言之,如果您想获得紧凑的 spring 上下文,请阅读以下代码片段

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
    public static void main(String[] args) {
         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // previous applicationContext.xml is your bean Definition scope  or else you can use AnnotationContext 
         // ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // Your Spring context is now initialized.
        // You can access beans and use them.
    }
}


有关Spring IOC容器的更多信息,请单击此处 Spring Doc

0赞 I.Yuldoshev 11/8/2023 #2

用 @ComponentScan和 @EnableAutoConfiguration并尝试以下方法来运行应用程序

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