I created a standalone SpringBoot application, and created a new object in my main component,
but got a NullPointerException at the new class.
How to use Autowired correctly?
package com.aro.try.main;
import com.aro.try.model.AccountInfo;
import com.aro.try.service.AccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MainProc implements CommandLineRunner {
@Autowired
private AccountInfoService accountInfoService;
@Override
public void run(String... args) throws Exception {
System.out.println("Hello MainProc~~~~~~");
// System.out.printf("account:" + accountInfoService.getAllAccountInfo());
new ABC().action();
}
}
class ABC {
@Autowired
private AccountInfoService accountInfoService;
public ABC() {
System.out.println("Init something~");
}
public void action(){
System.out.println("do action~");
System.out.println(accountInfoService.getAllAccountInfo());
}
}