执行springbean时出现问题

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

我有一个名为textfilewriter的bean将字符串实体写入hdfs。我已经在bean配置文件中配置了springbean。执行时,我得到nullpointerexception。请帮我解决这个问题。
我的bean配置:-

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/hadoop"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
  4. xmlns:hdp="http://www.springframework.org/schema/hadoop" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  7. http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
  8. <hdp:configuration id="hadoopConfigBean">
  9. fs.defaultFS=${hdp.fs}
  10. </hdp:configuration>
  11. <beans:bean id="textFileWriter"
  12. class="org.springframework.data.hadoop.store.output.TextFileWriter">
  13. <beans:constructor-arg index="0" ref="hadoopConfigBean"></beans:constructor-arg>
  14. <beans:constructor-arg index="1"
  15. type="org.apache.hadoop.fs.Path" value="/user/mhduser"></beans:constructor-arg>
  16. <beans:constructor-arg index="2" type="org.springframework.data.hadoop.store.codec.CodecInfo" >
  17. <beans:null></beans:null>
  18. </beans:constructor-arg>
  19. </beans:bean>
  20. <context:property-placeholder location="hadoop-configs.properties" />
  21. </beans:beans>

主要类别:-

  1. public class MainApp {
  2. @Autowired
  3. TextFileWriter textFileWriter;
  4. public static void main(String[] args) {
  5. AbstractApplicationContext context = new ClassPathXmlApplicationContext(
  6. "/META-INF/spring/application-context.xml", MainApp.class);
  7. System.out.println("Context loaded...");
  8. MainApp obj=new MainApp();
  9. obj.someMethod();
  10. context.registerShutdownHook();
  11. }
  12. private void someMethod() {
  13. try {
  14. textFileWriter.write("hi there");
  15. } catch (IOException e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. }
  20. }

此行中出现空指针异常:-

  1. textFileWriter.write("hi there");
bvhaajcl

bvhaajcl1#

@Autowired 不会在这里工作,因为您正在使用类的新示例 MainApp 这不是由spring管理的,这就是为什么你会得到一个 NullPointerException ```
MainApp obj=new MainApp();
obj.someMethod();

  1. 解决办法是:

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

  1. TextFileWriter textFileWriter = (TextFileWriter ) context.getBean("textFileWriter");
  2. MainApp obj=new MainApp(textFileWriter );
  3. obj.someMethod();
  4. context.registerShutdownHook();

}

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

  1. }
展开查看全部
fhg3lkii

fhg3lkii2#

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

  1. <context:annotation-config />

使Spring钢丝与

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

相关问题