Spring 如何从控制器调用服务
我正在尝试从控制器调用以下服务:Spring 如何从控制器调用服务,spring,spring-boot,sendgrid,Spring,Spring Boot,Sendgrid,我正在尝试从控制器调用以下服务: @Service class MyMailService { private final SendGrid sendGrid; @Inject public SendGridMailService(SendGrid sendGrid) { this.sendGrid = sendGrid; } void sendMail() { Request request = new Reques
@Service
class MyMailService {
private final SendGrid sendGrid;
@Inject
public SendGridMailService(SendGrid sendGrid) {
this.sendGrid = sendGrid;
}
void sendMail() {
Request request = new Request();
Response response = this.sendGrid.api(request);
}
}
# controller
public String index(Model model) {
MyMailService.sendMail() // how to do this properly?
return "register";
}
和我的控制器:
@Service
class MyMailService {
private final SendGrid sendGrid;
@Inject
public SendGridMailService(SendGrid sendGrid) {
this.sendGrid = sendGrid;
}
void sendMail() {
Request request = new Request();
Response response = this.sendGrid.api(request);
}
}
# controller
public String index(Model model) {
MyMailService.sendMail() // how to do this properly?
return "register";
}
从控制器调用此命令的正确方式是什么?基本上,我正在尝试“自动配置”sendgrid,因此每当调用MyMailService类时,我不必使用API键初始化sendgrid对象。正如其他人所建议的,我也认为您需要花更多时间学习Java和Spring基础知识。但要回答您的问题,您需要在控制器中自动连接/注入
服务单例对象以使用它:
@Controller
public class ControllerA
{
.
.
.
@Autowired
private MyMailService mymailService; //singleton object instance injected to be used/shared by all controllers
public String index(Model model) {
mymailService.sendMail()
return "register";
}
.
.
.
}
正如其他人所建议的,我也认为您需要花更多的时间学习Java和Spring基础知识。但要回答您的问题,您需要在控制器中自动连接/注入服务单例对象以使用它:
@Controller
public class ControllerA
{
.
.
.
@Autowired
private MyMailService mymailService; //singleton object instance injected to be used/shared by all controllers
public String index(Model model) {
mymailService.sendMail()
return "register";
}
.
.
.
}
您可以通过以下方式呼叫您的服务
1) 只需将@Autowired添加到类类型
@Autowired
MyMailService myMailService;
2) 使用构造函数注入,如下所示
MyMailService myMailService;
@Autowired
public WebController(MyMailService myMailService) {
this.myMailService = myMailService;
}
- here i assumed my Controller name is WebController
MyMailService myMailService;
@Autowired
public void setMyMailService(MyMailService myMailService) {
this.myMailService = myMailService;
}
myMailService.sendMail();
3) 使用Setter注入,如下所示
MyMailService myMailService;
@Autowired
public WebController(MyMailService myMailService) {
this.myMailService = myMailService;
}
- here i assumed my Controller name is WebController
MyMailService myMailService;
@Autowired
public void setMyMailService(MyMailService myMailService) {
this.myMailService = myMailService;
}
myMailService.sendMail();
最后,在控制器中,您可以调用服务类方法,如下所示
MyMailService myMailService;
@Autowired
public WebController(MyMailService myMailService) {
this.myMailService = myMailService;
}
- here i assumed my Controller name is WebController
MyMailService myMailService;
@Autowired
public void setMyMailService(MyMailService myMailService) {
this.myMailService = myMailService;
}
myMailService.sendMail();
注:
遵循命名约定。您可以通过以下方式调用您的服务
1) 只需将@Autowired添加到类类型
@Autowired
MyMailService myMailService;
2) 使用构造函数注入,如下所示
MyMailService myMailService;
@Autowired
public WebController(MyMailService myMailService) {
this.myMailService = myMailService;
}
- here i assumed my Controller name is WebController
MyMailService myMailService;
@Autowired
public void setMyMailService(MyMailService myMailService) {
this.myMailService = myMailService;
}
myMailService.sendMail();
3) 使用Setter注入,如下所示
MyMailService myMailService;
@Autowired
public WebController(MyMailService myMailService) {
this.myMailService = myMailService;
}
- here i assumed my Controller name is WebController
MyMailService myMailService;
@Autowired
public void setMyMailService(MyMailService myMailService) {
this.myMailService = myMailService;
}
myMailService.sendMail();
最后,在控制器中,您可以调用服务类方法,如下所示
MyMailService myMailService;
@Autowired
public WebController(MyMailService myMailService) {
this.myMailService = myMailService;
}
- here i assumed my Controller name is WebController
MyMailService myMailService;
@Autowired
public void setMyMailService(MyMailService myMailService) {
this.myMailService = myMailService;
}
myMailService.sendMail();
注:
遵循命名约定。sendMail()是一个实例方法,而不是静态方法。所以你需要一个MyMailService的实例来调用它,你可以把它注入到你的控制器中。这是基本的,基本的面向对象的东西。我建议您在深入研究更复杂的内容之前,花更多的时间学习基础知识(什么是方法,什么是静态方法,什么是对象)。sendMail()是一种实例方法,而不是静态方法。所以你需要一个MyMailService的实例来调用它,你可以把它注入到你的控制器中。这是基本的,基本的面向对象的东西。我建议你花更多的时间学习基础知识(什么是方法,什么是静态方法,什么是对象),然后再深入研究更复杂的东西。