如何将 spring-boot-web-application 转换为 spring 应用程序

How to convert spring-boot-web-application to spring application

我正在关注此 spring-data-aerospike tutorial 并设置示例应用程序,它工作正常。

现在我想将此 spring 引导应用程序转换为 spring 应用程序,因此我编写了以下代码。

    package com.aerospike.demo.simplespringbootaerospikedemo;

    import com.aerospike.demo.simplespringbootaerospikedemo.configuration.AerospikeConfiguration;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.data.aerospike.core.AerospikeTemplate;

    public class TestSpringConfig {
       public static void main(String[] args) throws IOException {
           ApplicationContext ctx = new AnnotationConfigApplicationContext(AerospikeConfiguration.class);
           AerospikeTemplate template = ctx.getBean(AerospikeTemplate.class);
       }
    }
18:33:25.430 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'aerospikeTemplate' defined in com.aerospike.demo.simplespringbootaerospikedemo.configuration.AerospikeConfiguration: Unsatisfied dependency expressed through method 'aerospikeTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aerospikeClient' defined in com.aerospike.demo.simplespringbootaerospikedemo.configuration.AerospikeConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.aerospike.client.AerospikeClient]: Factory method 'aerospikeClient' threw exception; nested exception is com.aerospike.client.AerospikeException$Connection: Error -8: Failed to connect to host(s): 
   null 0 Error -8: java.net.ConnectException: Can't assign requested address (connect failed)
   
   org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
   org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:541)
   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334)
   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
   org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=13=](AbstractBeanFactory.java:335)
   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917)
   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582)
   org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:93)
   com.aerospike.demo.simplespringbootaerospikedemo.TestSpringConfig.main(TestSpringConfig.java:10)

所以我确实像下面那样调整了一些代码,将属性设置到系统中 -

        import com.aerospike.demo.simplespringbootaerospikedemo.configuration.AerospikeConfiguration;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.annotation.AnnotationConfigApplicationContext;
        import org.springframework.data.aerospike.core.AerospikeTemplate;
        import java.io.IOException;
        import java.io.InputStream;
        import java.util.Properties;
        import java.util.stream.Stream;
        
        public class TestSpringConfig {
          public static void main(String[] args) throws IOException {
            System.out.println(ClassLoader.getSystemResourceAsStream("application.properties").available());
        
            Properties props = new Properties();
            InputStream resourceAsStream =  ClassLoader.getSystemResourceAsStream("application.properties");
            if (resourceAsStream != null) {
              props.load(resourceAsStream);
            }
            if(props != null) {
              System.setProperties(props);
            }
            try {
              ApplicationContext ctx = new AnnotationConfigApplicationContext(AerospikeConfiguration.class);
              AerospikeTemplate template = ctx.getBean(AerospikeTemplate.class);
            } catch (Exception e) {
              StackTraceElement[] st = e.getStackTrace();
              if(st != null) {
                System.out.println("st.length = " + st.length);
              }
              Stream.of(st).forEach(System.out::println);
            } catch (ExceptionInInitializerError e) {
              System.out.println(e.getCause());
              e.getCause().printStackTrace();
            }
        
          }
        }

当我 运行 具有上述更改的程序时,我得到以下 e.getCause().printStackTrace() -

的堆栈跟踪
java.lang.NullPointerException
    at ch.qos.logback.core.CoreConstants.<clinit>(CoreConstants.java:47)
    at ch.qos.logback.classic.layout.TTLLLayout.doLayout(TTLLLayout.java:52)
    at ch.qos.logback.classic.layout.TTLLLayout.doLayout(TTLLLayout.java:23)
    at ch.qos.logback.core.encoder.LayoutWrappingEncoder.encode(LayoutWrappingEncoder.java:115)
    at ch.qos.logback.core.OutputStreamAppender.subAppend(OutputStreamAppender.java:230)
    at ch.qos.logback.core.OutputStreamAppender.append(OutputStreamAppender.java:102)
    at ch.qos.logback.core.UnsynchronizedAppenderBase.doAppend(UnsynchronizedAppenderBase.java:84)
    at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:51)
    at ch.qos.logback.classic.Logger.appendLoopOnAppenders(Logger.java:270)
    at ch.qos.logback.classic.Logger.callAppenders(Logger.java:257)
    at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:421)
    at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:383)
    at ch.qos.logback.classic.Logger.log(Logger.java:765)
    at org.apache.commons.logging.LogAdapter$Slf4jLocationAwareLog.debug(LogAdapter.java:468)
    at org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:628)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:93)
    at com.aerospike.demo.simplespringbootaerospikedemo.TestSpringConfig.main(TestSpringConfig.java:26)

我运行没有可用的选项。如何修复此错误。

密码在githubhere

我将 line.separator=\n 添加到解决问题的 application.properties