0

I have a public final class that extends Configured and implements Tool that refuses to see an autowired variable. This class is passed to the static ToolRunner.run method and I'm not sure what impact I should expect from this added layer of complexity.

I've offloaded the logic of the context.getBean to a separate class and the autowiring works but introduces more bugs (nulls the configuration of the ToolRunner). The code for this separate class is a carbon copy of the code in the Driver class and is annotated as @Component. Why the Autowired works for a separate class but not this one is unknown to me.

Here's the code from the Driver class:

@Component
public final class Driver extends Configured implements Tool {
private static Driver driver = new Driver();
@Autowired
private Connection connection;

public static void main(String[] args) throws Exception{
int exitCode = ToolRunner.run(new Configuration(),driver, args);
}

@Override
public int run(String[] args) throws Exception{
AbstractApplicationContext context = new AnnotationConfigApplicationContext(HdfsConfig.class);
driver = context.getBean(Driver.class);
\\and it continues till I hit my getConnection NPE

Here's HdfsConfig.class:

@Configuration
@ComponentScan(basePackages = {"shared.folder"}
public class HdfsConfig{

@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public org.apache.hadoop.conf.Configuration configuration = new Configuration();
\\So on and so on until I return my configuration

What I really want to do is retain my ToolRunner info and connection object in the class with everything populated and happy. I suspect something regarding the nature of my Driver variable is breaking things but the reason is escape my feeble brain.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
dvw85
  • 1
  • 2
  • Here is a generic answer: https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null – Stephen C May 12 '19 at 00:52
  • It is also (probably) a bad idea to autowire the `connection` variable. Shouldn't you be managing connections via a connection pool? Or by using Spring JDBCtemplate? https://spring.io/guides/gs/relational-data-access/ – Stephen C May 12 '19 at 01:33

0 Answers0