使用Spring初始化程序创建一个简单的Spring引导。我只在可用的选项下选择DevTools。
在创建项目之后,不对其进行任何更改,就能够很好地运行程序。
现在,当我试图在这个项目中做一些自动装配时,它根本不起作用。我还是不明白。在此之前的所有问题都是有解决方案的,但都没有效果,而且在我的情况下,我所做的也没有什么复杂的事情,如下所示。请告诉我错过了什么。
@SpringBootApplication
public class DemoApplication {
// @Autowired
// private static Maker maker; // Stopped using this cos I wanted to check if the autowiring is not working in this class only or anywhere. Turns out it is anywhere.
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
Maker maker = new Maker();
maker.printName(); // Fails cos of ServiceHelper Autowiring
}
}
@Service
public class Maker {
@Autowired
private ServiceHelper serviceHelper;
public void printName(){
System.out.println("This is from the method body itself.");
System.out.println("Auto wiring works cos I got this -> " + serviceHelper.help());
}
}
@Component
public class ServiceHelper {
public String help(){
return "...FROM HELPER...";
}
}StackTrace
sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法中线程"restartedMain“java.lang.reflect.InvocationTargetException中的异常)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)中由: java.lang.NullPointerException at com引起的异常.example.demo.services.Maker.printName(Maker.java:15) at com.example.demo.DemoApplication.main(DemoApplication.java:17) .5
发布于 2018-08-18 14:47:42
如果使用new关键字创建任何bean,则该bean不会添加到Spring Application Context中,这是@Autowire静态bean的一种方法
@SpringBootApplication
public class DemoApplication {
@Autowired
private static Maker maker;
@Autowired
private Maker tMaker;
@PostConstruct
public void init() {
DemoApplication.maker = tMaker;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
maker.printName();
}
}或者您可以使用Autowire执行Constructor,在创建DemoApplication时,Maker实例作为构造函数的参数注入。
@Autowired
public DemoApplication(Maker maker) {
DemoApplication.maker = maker;
}或者可以在setter方法上使用@Autowired,在创建setter方法时,将使用Maker实例调用setter方法。
@Autowired
public void setMaker(Maker maker) {
DemoApplication.maker = maker
}发布于 2018-08-18 14:44:01
如果您创建了一个实例,它的ServiceHelper不会在Spring之前自动完成:
Maker maker = new Maker();您可以通过ApplicationContext访问bean
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext cts = SpringApplication.run(DemoApplication.class, args);
Maker maker = ctx.getBean(Maker.class);
maker.printName();
}
}发布于 2018-08-18 15:06:19
当您使用new Maker()时,您没有使用为您创建的bean Spring,因此您拥有的对象未初始化,因为没有注入依赖项。
您需要得到Spring框架创建的bean,就像我在下面所做的那样,您不能直接自动创建静态字段:
@SpringBootApplication
public class DemoApplication {
private static Maker maker;
@Autowired
public void setMaker(Maker maker) {
DemoApplication.maker = maker;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
maker.printName();
}
}https://stackoverflow.com/questions/51909671
复制相似问题