有几天我正在尝试创建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) 使用jdk 1.8
我遇到了这样的错误
org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping'的bean时出错:init方法的调用失败; 嵌套异常是java.lang.NoSuchMethodError:org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation
我的问题:
为什么错误的鼓励和如何解决它?
web.xml中
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>product</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>product</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
产品servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="com.kopylov.spring.controller" />
<mvc:annotation-driven/>
Run Code Online (Sandbox Code Playgroud)
调节器
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ProductController {
@RequestMapping
public String showHome(){
return "home";
}
Run Code Online (Sandbox Code Playgroud)
}
AppInitializer
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
Run Code Online (Sandbox Code Playgroud)
公共类AppInitializer扩展AbstractAnnotationConfigDispatcherServletInitializer { …