0

文字列エンティティを HDFS に書き込むための textFileWriter という名前の Bean があります。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>

メインクラス:-

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");
4

2 に答える 2

1

@AutowiredMainApp春によって管理されていないクラスの新しいインスタンスを使用しているため、ここでは機能しません。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();
     }
 }

}

于 2016-04-21T09:15:49.247 に答える
1

MainAppBean として明示的に追加しなくても、Spring に依存関係を注入させることができます。

  1. アノテーション駆動型構成を有効にします。application-context.xml以下に追加

    <context:annotation-config />
    
  2. Spring ワイヤーの依存関係を作成します

    MainApp obj=new MainApp();
    context.getAutowireCapableBeanFactory().autowireBean(obj);
    
于 2016-04-21T10:00:57.630 に答える