执行 spring bean 时出现问题

Issue when executing spring bean

我有一个名为 textFileWriter 的 bean,用于将字符串实体写入 HDFS。我已经在 bean 配置文件中配置了 spring bean。执行时收到 NullPointerException。请帮我解决这个问题。

我的 bean 配置:-

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/hadoop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:hdp="http://www.springframework.org/schema/hadoop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">


    <hdp:configuration id="hadoopConfigBean">
        fs.defaultFS=${hdp.fs}
    </hdp:configuration>


    <beans:bean id="textFileWriter"
        class="org.springframework.data.hadoop.store.output.TextFileWriter">
        <beans:constructor-arg index="0" ref="hadoopConfigBean"></beans:constructor-arg>
        <beans:constructor-arg index="1"
            type="org.apache.hadoop.fs.Path" value="/user/mhduser"></beans:constructor-arg>
        <beans:constructor-arg index="2" type="org.springframework.data.hadoop.store.codec.CodecInfo" >
        <beans:null></beans:null>
        </beans:constructor-arg>
    </beans:bean>

    <context:property-placeholder location="hadoop-configs.properties" />
</beans:beans>

主要Class:-

public class MainApp {
    @Autowired
    TextFileWriter textFileWriter;

    public static void main(String[] args) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(
                "/META-INF/spring/application-context.xml", MainApp.class); 
        System.out.println("Context loaded...");
        MainApp obj=new MainApp();
        obj.someMethod();

        context.registerShutdownHook();

    }

    private void someMethod() {
        try {
            textFileWriter.write("hi there");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

我在这一行中遇到空指针异常:-

textFileWriter.write("hi there");

@Autowired 在这里不起作用,因为您使用的是 class MainApp 的新实例,它不受 spring 管理,这就是为什么您得到一个NullPointerException

    MainApp obj=new MainApp();
    obj.someMethod();

解决方法是:

public class MainApp {

public MainApp(TextFileWriter textFileWriter){
  this.textFileWriter = textFileWriter;
}

public MainApp(){
} 

@Autowired
TextFileWriter textFileWriter;

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "/META-INF/spring/application-context.xml"); 
     System.out.println("Context loaded...");

     TextFileWriter textFileWriter = (TextFileWriter )  context.getBean("textFileWriter");
     MainApp obj=new MainApp(textFileWriter );
      obj.someMethod();

     context.registerShutdownHook();

 }

 private void someMethod() {
     try {
         textFileWriter.write("hi there");
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
 }

}

您仍然可以使 Spring 注入依赖项而无需显式添加 MainApp 作为 bean。

  1. 启用注解驱动配置。添加到application-context.xml以下

    <context:annotation-config />
    
  2. 使 Spring 与

    建立依赖关系
    MainApp obj=new MainApp();
    context.getAutowireCapableBeanFactory().autowireBean(obj);