为 Public 接口添加 Bean
Add Bean For Public Interface
我有一个 Spring 引导应用程序,我正在添加一些我在常规 Spring MVC 应用程序(不是引导)中编写的代码。
当我 运行 它时,我得到一个错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userService in app.WelcomeController required a bean of type 'com.myorg.account.service.UserService' that could not be found.
Action:
Consider defining a bean of type 'com.myorg.account.service.UserService' in your configuration.
所以我向 UserService 添加了 Qualifier 和 Autowired。完整代码如下。
package com.myorg.account.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import com.myorg.account.model.User;
@Controller
@Configuration
public interface UserService {
@Autowired(required = true)
@Qualifier(value="UserService")
@Bean
void save(User user);
User findByUsername(String username);
}
在 WelcomeController 上方,我指定了我认为可以解决问题的限定符。
@ComponentScan
@Controller
@Service("UserInterface")
public class WelcomeController {
这里是错误中提到的userService字段。这是来自 WelcomeController.java
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "registration";
}
userService.save(userForm);
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/welcome";
}
提前致谢。
您应该将 @Controller
注释添加到接口 UserService
而不是实现 UserService
接口的 class。
删除UserService
中的所有注释,只在'WelcomeController'
中留下@Controller
@Controller
public class WelcomeController implements UserService {
我有一个 Spring 引导应用程序,我正在添加一些我在常规 Spring MVC 应用程序(不是引导)中编写的代码。
当我 运行 它时,我得到一个错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userService in app.WelcomeController required a bean of type 'com.myorg.account.service.UserService' that could not be found.
Action:
Consider defining a bean of type 'com.myorg.account.service.UserService' in your configuration.
所以我向 UserService 添加了 Qualifier 和 Autowired。完整代码如下。
package com.myorg.account.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import com.myorg.account.model.User;
@Controller
@Configuration
public interface UserService {
@Autowired(required = true)
@Qualifier(value="UserService")
@Bean
void save(User user);
User findByUsername(String username);
}
在 WelcomeController 上方,我指定了我认为可以解决问题的限定符。
@ComponentScan
@Controller
@Service("UserInterface")
public class WelcomeController {
这里是错误中提到的userService字段。这是来自 WelcomeController.java
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
return "registration";
}
userService.save(userForm);
securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/welcome";
}
提前致谢。
您应该将 @Controller
注释添加到接口 UserService
而不是实现 UserService
接口的 class。
删除UserService
中的所有注释,只在'WelcomeController'
@Controller
@Controller
public class WelcomeController implements UserService {