类未在spring-boot中实例化(从j2EE到spring-boot的迁移)

时间:2019-07-14 15:04:40

标签: java spring-boot

我正在将j2EE应用程序迁移到spring-boot。.我在这里面临很多挑战..需要您的小帮助,以下是我遵循的步骤

  1. 是否已设置POM,JAR和WAR包装
  2. 尝试将Servlet转换为RestController
  3. 我已注册为
  4. 的过滤器
@Configuration

public class FilterConfigService {

    @Bean
    public FilterRegistrationBean mdcFilter() {
        FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
        filterRegBean.setFilter(new MDCFilter());
        filterRegBean.addUrlPatterns("/v2/*");
        filterRegBean.setOrder(1);
        return filterRegBean;
    }

    @Bean
    public FilterRegistrationBean apiOriginFilter() {
        FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
        filterRegBean.setFilter(new ApiOriginFilter());
        filterRegBean.addUrlPatterns("/v2/*");
        filterRegBean.setOrder(2);
        return filterRegBean;
    }
}
  1. 有一个用于通过servlet的Init()实例化某些必要内容的singleton类,我将其删除了,因为默认情况下所有bean都是作用域中的singleton,并进行了以下实例化(不知道这是否是一种好习惯)
    @Configuration
    public class PdfExtractServerConfig implements ApplicationListener<ContextRefreshedEvent> {

        @Autowired
        PdfExtractServer pdfExtractServer;

        private static Logger log = Logger.getLogger(PdfExtractServerConfig.class);

        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            try {
                pdfExtractServer.instance(); // this was called in init of servlet
                System.out.println("instance created ..");
            }
            catch (Throwable e) {
                log.error("Unable to start PdfExtractServer", e);
                throw e;
            }
            //Start monitoring for system health
            ResourceCheck.startMonitoring();
            SplunkMgr.instance().addSplunkMessage("BackPressure", LogLevel.INFO);
        }
    }

单吨级如下。

像之前一样

    public static PdfExtractServer instance() {
            if (instance == null) {
                synchronized (startupLock) {
                    if (instance == null) {
                        instance = new PdfExtractServer();
                        instance.start("PES");
                    }
                }
            }
            return instance;
        }

现在我把它做成

    public static PdfExtractServer instance() {
    //      instance = new PdfExtractServer();
            instance.start("PES");
            return instance;
        }

这是我的测试控制器

    @RestController
    //@RequestMapping("/")
    public class Test {
        @RequestMapping("/test" )
        public String test(){
            return "Tested OK";
        }
    }

我不知道我在哪里做错..当我尝试运行此渲染器时

  

无法在以下位置启动PdfExtractServer java.lang.NullPointerException   com.it.pes.pdfextract.service.PdfExtractServer.instance(PdfExtractServer.java:78)     在   com.it.pes.pdfextract.config.PdfExtractServerConfig.onApplicationEvent(PdfExtractServerConfig.java:24)     在   com.it.pes.pdfextract.config.PdfExtractServerConfig.onApplicationEvent(PdfExtractServerConfig.java:13)     在   com.it.pes.pdfextract.config.PdfExtractServerConfig $$ EnhancerBySpringCGLIB $$ e703305a.onApplicationEvent()     在   org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)     在   org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)     在   org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)     在   org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)     在   org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883)     在   org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)     在   org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)     在   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)     在   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:303)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)

观察:我尝试通过glassfish球衣注册我的过滤器,但是内部在一些罐子中使用了1.1.1的javax.ws.rs-api,因此发生冲突,我的应用程序没有启动,因此需要注册这些过滤器如上所述。

您的帮助对我来说将是非常棒的举动。。谢谢您。

包装结构附在此处。 enter image description here

2 个答案:

答案 0 :(得分:0)

当您要求容器将其提供给您时,您的PdfExtractServer对象为null。该类中的实例对象为null。

在调用类的构造函数(在您的情况下为PdfExtractServer)时,@ Autowired实例变量尚不包含其值。由于您依赖它来执行特定的逻辑/或实例创建,因此建议使用@PostConstruct批注。此注释允许在构造实例之后以及在注入所有@Autowired实例之后执行特定方法。有关here

的更多信息

下面的链接应该能够帮助您了解并可能解决空指针问题。

The usual Autowiring not working issues and it's intended fixes

答案 1 :(得分:0)

我同意以上观点。我认为您太过复杂了。

我相信这就是您所需要的。 @Configuration带注释的类将在服务器启动时运行。 @Bean带注释的方法将被执行一次,并默认创建一个@Autowire可用的单例bean

所有配置类将首先运行,然后将扫描所有程序包并提供服务,组件,存储库,控制器的信息。

@Configuration
public class PdfExtractServerConfig {

    @Bean
    public PdfExtractServer pdfExtractServer() {
       final PdfExtractServer pdfExtractServer = new PdfExtractServer();
       pdfExtractServer.start("PES");
       return pdfExtractServer
    }
}

因此,当您想在其他spring managed bean中使用singelton时

// Spring will by default scan after this annotation recursively through
// packages on classes and create by default a singleton. It will also 
// search after all bean dependencies that need to be injected. in this case 
// PdfExtracServer and then inject it.
@Component 
public class Foo {

    @Autowire
    private PdfExtractServer pdfExtractServer;

    //different methods here

}