Java Spring应用程序@autowired返回空指针异常

Java Spring application @autowired returns null pointer exception

我刚接触Java Spring IoC,这是我的问题

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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方法

1
2
3
4
5
6
7
public class Test {

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

    }
}

现在在我的主类中,Ive创建了FactoryConfig的ApplicationContentext。 (我期望工厂配置中的所有@Beans都将被初始化。但是,当我使用@Autowired

访问Test类时,它返回null。

我的主班

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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的正确方法是什么?任何更清晰的解释将不胜感激。


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

您可以使用:

来在您的主要方法中访问Test bean。

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

通常,您从上下文中获得一个"引导程序",并调用该引导程序来启动您的应用程序。

此外:

  • 在Java上,方法不应以大写开头。您的Test类应该具有print方法,而不是print
  • 如果您从Spring开始,则应该尝试使用Spring Boot


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


Spring不会管理您的Main类,这就是为什么您会收到Nullpointer异常。
使用ApplicationContext加载bean,就可以像执行操作一样获取bean和访问方法-

1
2
3
4
5
6
ApplicationContext context =
           new AnnotationConfigApplicationContext(FactoryConfig.class);

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

 config.test().Print();

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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();
     }

}

您的课程

1
2
3
4
5
public class Test {
    public void Print() {
        System.out.println("Hello Test");
    }
}

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