简体   繁体   English

在另一个项目中运行 spring 引导而不使用 @SpringBootApplication 注释作为 jar 库

[英]Run spring boot without @SpringBootApplication annotation as jar library in another project

I'm trying to use @autowired annotation in a spring boot project that's not started by main method with @SpringBootApplication annotation.我正在尝试在 spring 引导项目中使用 @autowired 注释,该项目不是由带有 @SpringBootApplication 注释的 main 方法启动的。 Instead, i did created a jar of that spring project and i'm using that jar as an external jar in a legacy project (non-spring project). Instead, i did created a jar of that spring project and i'm using that jar as an external jar in a legacy project (non-spring project). As result i can't get ApplicationContext and all beans managed by spring when you run application from main method are null.结果,当您从 main 方法运行应用程序时,我无法获得 ApplicationContext 并且由 spring 管理的所有 bean 都是 null。

Is that possible to use Spring boot project as a.jar without run main method??是否可以使用 Spring 引导项目作为 a.jar 而不运行主要方法?

public class RetrieveSubscriberType {
    public RetrieveSubscriberType() {
        ApplicationContext appCtx = ApplicationContextUtils
                .getApplicationContext();
        
        this.subscriber = (SubscriberDAOImpl)appCtx.getBean("subscriber");
    }

appCtx always null appCtx 总是 null

@Configuration
public class ApplicationContextUtils implements ApplicationContextAware {   
    private static ApplicationContext ctx;

      @Override
      public void setApplicationContext(ApplicationContext appContext)
          throws BeansException {
        ctx = appContext;

      }

      public static ApplicationContext getApplicationContext() {
        return ctx;
      }
}

ApplicationContextUtils where method setApplicationContext is not called未调用方法 setApplicationContext 的 ApplicationContextUtils

From the provided docs从提供的文档

You might need to bootstrap your @Configuration classes via AnnotationConfigApplicationContext您可能需要通过AnnotationConfigApplicationContext引导您的@Configuration

@Configuration classes are typically bootstrapped using either AnnotationConfigApplicationContext or its web-capable variant, AnnotationConfigWebApplicationContext . @Configuration类通常使用AnnotationConfigApplicationContext或其支持网络的变体AnnotationConfigWebApplicationContext进行引导。

A simple example with the former follows:前者的简单示例如下:

 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
 ctx.register(AppConfig.class);
 ctx.refresh();
 MyBean myBean = ctx.getBean(MyBean.class);
 // use myBean ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导项目 jar 作为另一个项目中的依赖项 - Spring Boot project jar as dependency in another project 将Spring Web项目打包到没有Spring Boot的jar(uber-jar)中(非spring-boot项目) - Package Spring web project into jar (uber-jar) without spring boot (non spring-boot project) 如果没有@SpringBootApplication,Spring 启动应用程序如何工作? - How does Spring boot application works without @SpringBootApplication? 如何使用 JUnit 在没有 @SpringBootApplication 的情况下测试 Java Spring Boot 应用程序? - How to test Java Spring Boot application without @SpringBootApplication using JUnit? 将一个 spring-boot 项目作为 jar 添加到另一个项目中 - Add one spring-boot project as a jar to another project 如何从spring boot项目创建jar,我们要在另一个spring boot应用中使用的jar? - how to create jar from spring boot project , this jar we want to use in another spring boot app? 带有JSP项目的Spring-Boot无法与jar或war文件一起运行 - Spring-Boot with JSP project not able to run with jar or war file 将生成的胖 jar 复制到 Spring 引导项目中的另一个目标文件夹 - Copy resulting fat jar to another target folder in Spring boot project @Autowired 没有 spring boot 的注解 - @Autowired annotation without spring boot 在没有@ComponentScan 和 Xml 配置的情况下将 spring boot jar 导入另一个应用程序 - Importing spring boot jar into another app without @ComponentScan and Xml Configurations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM