执行springbean时出现问题

c6ubokkw  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(384)

我有一个名为textfilewriter的bean将字符串实体写入hdfs。我已经在bean配置文件中配置了springbean。执行时,我得到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");
bvhaajcl

bvhaajcl1#

@Autowired 不会在这里工作,因为您正在使用类的新示例 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();
}
}

}
fhg3lkii

fhg3lkii2#

您仍然可以在不添加 MainApp 作为一个豆子。
启用注解驱动配置。添加到 application-context.xml 下列的

<context:annotation-config />

使Spring钢丝与

MainApp obj=new MainApp();
context.getAutowireCapableBeanFactory().autowireBean(obj);

相关问题