如何在新的 [=10th=] 构造函数中使用 Spring Boot @Autowired
How to use SpringBoot @Autowired in new class constructor
我创建了一个独立的 Spring Boot 应用程序,并在我的主要组件中创建了一个新对象,
但在新的 class 获得了 NullPointerException
。
如何正确使用Autowired
?
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());
}
}
@Autowired
仅适用于 Spring 托管 类,而在您的情况下 ABC
不是一个。要使 ABC
由 Spring 管理,请用 @Component
对其进行注释。然后不是在 MainProc
中显式实例化 ABC
,而是使用 @Autowired
注入它。 AccountInfoService
如果不直接在那里使用,可以从 MainProc
中删除。
@Component
public class MainProc implements CommandLineRunner {
@Autowired
private ABC abc;
@Override
public void run(String... args) throws Exception {
System.out.println("Hello MainProc~~~~~~");
abc.action();
}
}
@Component
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());
}
}
奖励:构造函数注入
一般建议使用构造函数注入而不是字段注入。 here.
讨论了它的优点
We usually advise people to use constructor injection for all
mandatory collaborators and setter injection for all other properties.
Again, constructor injection ensures all mandatory properties have
been satisfied, and it is simply not possible to instantiate an object
in an invalid state (not having passed its collaborators).
要使用构造函数注入,请使用 @Autowired
注释构造函数并将依赖项作为参数包含在内。然后,从字段中删除 @Autowired
。
@Component
public class MainProc implements CommandLineRunner {
private ABC abc;
@Autowired
public MainProc(ABC abc) {
this.abc = abc;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Hello MainProc~~~~~~");
abc.action();
}
}
@Component
public class ABC {
private AccountInfoService accountInfoService;
@Autowired
public ABC(AccountInfoService accountInfoService) {
this.accountInfoService = accountInfoService
System.out.println("Init something~");
}
public void action() {
System.out.println("do action~");
System.out.println(accountInfoService.getAllAccountInfo());
}
}
我创建了一个独立的 Spring Boot 应用程序,并在我的主要组件中创建了一个新对象,
但在新的 class 获得了 NullPointerException
。
如何正确使用Autowired
?
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());
}
}
@Autowired
仅适用于 Spring 托管 类,而在您的情况下 ABC
不是一个。要使 ABC
由 Spring 管理,请用 @Component
对其进行注释。然后不是在 MainProc
中显式实例化 ABC
,而是使用 @Autowired
注入它。 AccountInfoService
如果不直接在那里使用,可以从 MainProc
中删除。
@Component
public class MainProc implements CommandLineRunner {
@Autowired
private ABC abc;
@Override
public void run(String... args) throws Exception {
System.out.println("Hello MainProc~~~~~~");
abc.action();
}
}
@Component
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());
}
}
奖励:构造函数注入
一般建议使用构造函数注入而不是字段注入。 here.
讨论了它的优点We usually advise people to use constructor injection for all mandatory collaborators and setter injection for all other properties. Again, constructor injection ensures all mandatory properties have been satisfied, and it is simply not possible to instantiate an object in an invalid state (not having passed its collaborators).
要使用构造函数注入,请使用 @Autowired
注释构造函数并将依赖项作为参数包含在内。然后,从字段中删除 @Autowired
。
@Component
public class MainProc implements CommandLineRunner {
private ABC abc;
@Autowired
public MainProc(ABC abc) {
this.abc = abc;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Hello MainProc~~~~~~");
abc.action();
}
}
@Component
public class ABC {
private AccountInfoService accountInfoService;
@Autowired
public ABC(AccountInfoService accountInfoService) {
this.accountInfoService = accountInfoService
System.out.println("Init something~");
}
public void action() {
System.out.println("do action~");
System.out.println(accountInfoService.getAllAccountInfo());
}
}