kop*_*lov 42 java spring spring-mvc
有几天我正在尝试创建Spring CRUD应用程序.我糊涂了.我无法解决这个错误.
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'clientController'的bean时出错:通过方法'setClientService'参数0表示不满意的依赖关系; 嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'clientService'的bean时出错:通过字段'clientRepository'表示的不满意的依赖关系; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型'com.kopylov.repository.ClientRepository'的限定bean可用:预期至少有1个bean有资格作为autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
还有这个
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'clientService'的bean时出错:通过字段'clientRepository'表示的不满意的依赖关系; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型'com.kopylov.repository.ClientRepository'的限定bean可用:预期至少有1个bean有资格作为autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
ClientController
@Controller
public class ClientController {
private ClientService clientService;
@Autowired
@Qualifier("clientService")
public void setClientService(ClientService clientService){
this.clientService=clientService;
}
@RequestMapping(value = "registration/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute Client client){
this.clientService.addClient(client);
return "home";
}
}
Run Code Online (Sandbox Code Playgroud)
ClientServiceImpl
@Service("clientService")
public class ClientServiceImpl implements ClientService{
private ClientRepository clientRepository;
@Autowired
@Qualifier("clientRepository")
public void setClientRepository(ClientRepository clientRepository){
this.clientRepository=clientRepository;
}
@Transactional
public void addClient(Client client){
clientRepository.saveAndFlush(client);
}
}
Run Code Online (Sandbox Code Playgroud)
ClientRepository
public interface ClientRepository extends JpaRepository<Client, Integer> {
}
Run Code Online (Sandbox Code Playgroud)
我查看了很多类似的问题,但没有人回答他们无法帮助我.
Ale*_*oig 19
ClientRepository应使用@Repository
标记进行注释.使用您当前的配置,Spring将不会扫描该类并了解它.在启动和连线的那一刻将找不到ClientRepository类.
编辑
如果添加@Repository
标记没有帮助,那么我认为问题可能现在与ClientService
和ClientServiceImpl
.
尝试使用注释ClientService
(接口)@Service
.由于您的服务只应该有一个实现,因此您无需使用可选参数指定名称@Service("clientService")
.Spring将根据接口名称自动生成它.
此外,正如Bruno所提到的那样,因为您只有一个服务实现,所以@Qualifier
不需要ClientController
.
ClientService.java
@Service
public interface ClientService {
void addClient(Client client);
}
Run Code Online (Sandbox Code Playgroud)
ClientServiceImpl.java(选项1)
@Service
public class ClientServiceImpl implements ClientService{
private ClientRepository clientRepository;
@Autowired
public void setClientRepository(ClientRepository clientRepository){
this.clientRepository=clientRepository;
}
@Transactional
public void addClient(Client client){
clientRepository.saveAndFlush(client);
}
}
Run Code Online (Sandbox Code Playgroud)
ClientServiceImpl.java(选项2 /首选)
@Service
public class ClientServiceImpl implements ClientService{
@Autowired
private ClientRepository clientRepository;
@Transactional
public void addClient(Client client){
clientRepository.saveAndFlush(client);
}
}
Run Code Online (Sandbox Code Playgroud)
ClientController.java
@Controller
public class ClientController {
private ClientService clientService;
@Autowired
//@Qualifier("clientService")
public void setClientService(ClientService clientService){
this.clientService=clientService;
}
@RequestMapping(value = "registration", method = RequestMethod.GET)
public String reg(Model model){
model.addAttribute("client", new Client());
return "registration";
}
@RequestMapping(value = "registration/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute Client client){
this.clientService.addClient(client);
return "home";
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我知道这似乎为时已晚,但将来可能会对其他人有所帮助。
我遇到了同样的错误,问题是 spring boot 没有读取我的服务包,所以添加:
@ComponentScan(basePackages = {"com.example.demo.Services"})
(您必须指定自己的服务包路径)和类 demoApplication
(具有主要功能的类)和服务接口必须注释 @Service
,实现服务接口的类必须用 注释@Component
,然后自动装配服务接口。
If you are using Spring Boot, your main app should be like this (just to make and understand things in simple way) -
package aaa.bbb.ccc;
@SpringBootApplication
@ComponentScan({ "aaa.bbb.ccc.*" })
public class Application {
.....
Run Code Online (Sandbox Code Playgroud)
Make sure you have @Repository and @Service appropriately annotated.
Make sure all your packages fall under - aaa.bbb.ccc.*
In most cases this setup resolves these kind of trivial issues. Here is a full blown example. Hope it helps.
归档时间: |
|
查看次数: |
259101 次 |
最近记录: |