【问题标题】:Java Spring application @autowired returns null pointer exceptionJava Spring 应用程序@autowired 返回空指针异常
【发布时间】:2016-03-18 13:02:33
【问题描述】:

我对 Java Spring IoC 还很陌生,这是我的问题

我有一个 FactoryConfig 类,其中包含所有 bean 和注释 @Configuration 和 @ComponentScan,如下所示。

import org.springframwork.*

@Configuration
@ComponentScan(basePackages="package.name")
public class FactoryConfig {

    public FactoryConfig() {

    }

    @Bean
    public Test test(){
         return new Test();
    }

    //And few more @Bean's
}

我的 Test 类有一个简单的 Print 方法

public class Test {

    public void Print() {
        System.out.println("Hello Test");

    }
}

现在在我的主类中,我创建了 FactoryConfig 的 ApplicationContentext。 (我希望工厂配置中的所有@Beans 都将被初始化。但是,当我使用@Autowired 访问Test 类时它返回null

我的主要课程

public class Main {

     @Autowired
     protected static Test _autoTest;

     public static void main(String[] args) throws InterruptedException {
          // TODO Auto-generated method stub
     ApplicationContext context = 
               new AnnotationConfigApplicationContext(FactoryConfig.class);

     FactoryConfig config = context.getBean(FactoryConfig.class);

     config.test().Print();  

    // _autoTest.Print();   <--- Im getting NULL Pointer Ex here 
   }

}

@Autowire 和使用对象/bean 的正确方法是什么?任何更清晰的解释将不胜感激。

【问题讨论】:

  • @Autowired 和其他注解只能作用于 Spring 处理的对象。 Spring 不处理您的 Main 类。 Spring 可以做很多事情,但它并不神奇,它会在完全独立的类/对象实例中注入一些东西。

标签: java spring


【解决方案1】:

只有 Spring 管理的 bean 才能有 @Autowire 注释。您的主类不是由 Spring 管理的:它是由您创建的,而不是在 Spring 上下文中声明的:Spring 对您的类一无所知,也不注入此属性。

你可以在你的 main 方法中访问 Test bean:

context.getBean(Test.class).Print();

通常,您会从上下文中获得一个“引导程序”,然后调用此引导程序来启动您的应用程序。

此外:

  • 在 Java 上,方法不应以大写字母开头。您的 Test 类应该有一个 print 方法,而不是 Print
  • 如果你从 Spring 开始,你应该尝试一下 Spring Boot

【讨论】:

  • 我认为你给出了一个相当简单的答案!
【解决方案2】:

Spring 不管理您的 Main 类,这就是您收到 Nullpointer 异常的原因。 使用 ApplicationContext 加载 bean,您可以像现在一样获取 bean 和访问方法 -

ApplicationContext context = 
           new AnnotationConfigApplicationContext(FactoryConfig.class);

 FactoryConfig config = context.getBean(FactoryConfig.class);

 config.test().Print();  

【讨论】:

    【解决方案3】:

    删除静态参数 受保护的测试_autoTest;

    【讨论】:

      【解决方案4】:

      你的班级

      public class Test {
          public void Print() {
              System.out.println("Hello Test");
          }
      }
      

      对 Spring 不可见。尝试为其添加适当的注释,例如 @Component

      【讨论】:

      • Test是通过@BeanFactoryConfig中定义的,所以它实际上是一个Srping托管bean
      【解决方案5】:

      原因是您的Main 不是由 Spring 管理的。将其作为 bean 添加到您的配置中:

      import org.springframwork.*
      
      @Configuration
      @ComponentScan(basePackages="package.name")
      public class FactoryConfig {
      
          public FactoryConfig() {
      
          }
      
          @Bean
          public Test test(){
               return new Test();
          }
      
          @Bean
          public Main main(){
               return new Main();
          }
      
          //And few more @Bean's
      }
      

      然后您可以按如下方式编辑您的main()

      public class Main {
      
           @Autowired
           protected Test _autoTest;
      
           public static void main(String[] args) throws InterruptedException {
               ApplicationContext context = 
                     new AnnotationConfigApplicationContext(FactoryConfig.class);
      
               Test test = context.getBean(Test.class);
               Main main = context.getBean(Main.class);
      
               test.Print();  
               main._autoTest.Print();
           }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-01
        相关资源
        最近更新 更多