spring 创建名为“userService”的Bean时出错:通过字段“userJpaRepository”表示的未满足的依赖关系

7cwmlq89  于 2022-11-21  发布在  Spring
关注(0)|答案(2)|浏览(262)

当我尝试访问主页面时,出现以下错误

  1. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userJpaRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'net.codejava.spring.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=userJpaRepository)}

在我的UserController中,我有以下代码

  1. @Controller
  2. public class UserController {
  3. private UserService employeeServiceImpl;
  4. @RequestMapping("/")
  5. public String employee() {
  6. this.employeeServiceImpl.listAlUsers();
  7. return "employee";
  8. }
  9. @Autowired(required = true)
  10. public void setEmployeeService(UserService employeeServiceImpl) {
  11. this.employeeServiceImpl = employeeServiceImpl;
  12. }
  13. }

我的用户服务

  1. public interface UserService {
  2. public abstract List<User> listAlUsers();
  3. public abstract User addUser(User user);
  4. public abstract int removeUser(int id);
  5. public abstract User updateUser(User user);
  6. }

我的用户服务实现

  1. @Service("userService")
  2. public class UserServiceImpl implements UserService {
  3. @Autowired
  4. @Qualifier("userJpaRepository")
  5. private UserRepository userJpaRepository;
  6. @Override
  7. public List<User> listAlUsers() {
  8. return userJpaRepository.findAll();
  9. }
  10. @Override
  11. public User addUser(User user) {
  12. return userJpaRepository.save(user);
  13. }
  14. @Override
  15. public int removeUser(int id) {
  16. userJpaRepository.delete(id);
  17. return 0;
  18. }
  19. @Override
  20. public User updateUser(User user) {
  21. return userJpaRepository.save(user);
  22. }
  23. }

我的JpaRepository

  1. @Repository("userJpaRepository")
  2. public interface UserRepository extends JpaRepository<User,Serializable> {
  3. }

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  6. <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>/WEB-INF/spring/root-context.xml</param-value>
  10. </context-param>
  11. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  12. <listener>
  13. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  14. </listener>
  15. <!-- Processes application requests -->
  16. <servlet>
  17. <servlet-name>appServlet</servlet-name>
  18. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  19. <init-param>
  20. <param-name>contextConfigLocation</param-name>
  21. <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  22. </init-param>
  23. <load-on-startup>1</load-on-startup>
  24. </servlet>
  25. <servlet-mapping>
  26. <servlet-name>appServlet</servlet-name>
  27. <url-pattern>/</url-pattern>
  28. </servlet-mapping>
  29. </web-app>

我的servlet上下文

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  13. <!--The <context:component-scan...> tag will be use to activate Spring MVC annotation scanning capability which allows to make use of annotations like @Controller and @RequestMapping etc.-->
  14. <!--Step 1 : HandlerMapping -->
  15. <context:component-scan base-package="com.loiane.controller" />
  16. <!--JPA Repository-->
  17. <jpa:repositories base-package="com.loiane.repository.EmployeeJpaRepository"/>
  18. <mvc:annotation-driven />
  19. <!--Step 3 : View Resolver-->
  20. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  21. <property name="prefix">
  22. <value>/WEB-INF/views/</value>
  23. </property>
  24. <property name="suffix">
  25. <value>.jsp</value>
  26. </property>
  27. </bean>
  28. <!--Bundles-->
  29. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  30. <property name="basename">
  31. <value>messages</value>
  32. </property>
  33. </bean>
  34. <!--Declaramos el interceptor para permitir el cambio de idioa en tiempo de ejecucion-->
  35. <mvc:interceptors>
  36. <mvc:interceptor>
  37. <mvc:mapping path="/**" />
  38. <bean id="localeChangeInterceptor"
  39. class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
  40. <property name="paramName" value="lang" />
  41. </bean>
  42. </mvc:interceptor>
  43. </mvc:interceptors>
  44. <!--Si el idioma no existe cargamos el en-->
  45. <bean id="localeResolver"
  46. class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
  47. <property name="cookieName" value="lang" />
  48. <property name="defaultLocale" value="en" />
  49. </bean>
  50. </beans>

根上下文为空。
我不知道哪里是错误的可能原因,我试图找到不同的选项,但如果我使用DAO模式,我收到同样的错误,所以我会知道这是问题,试图解决它。
项目的配置是用xml的,但我认为解决这种情况并不重要。
问候!

56lgkhnf

56lgkhnf1#

您正在储存库界面@Repository("userJpaRepository")上使用Bean限定词名称。如果有一个以上的类别实作通用界面,则限定词名称非常有用。它有助于解析特定的Bean,它将被自动连接到其他Bean的接口变量。在接口上声明它将使其失去作用。如果没有其他类(除了Spring创建的名称为userRepositoryImpl的代理Bean)正在实现UserRepository接口。

  1. @Autowired
  2. private UserRepository userJpaRepository;

并将Serializable替换为ID列的Datatype,如Integer或Idclass。

  1. @Repository
  2. public interface UserRepository extends JpaRepository<User,Integer> {
  3. }

现在,要使spring能够扫描扩展其预定义存储库接口的接口,请在spring上下文中添加要扫描的包

  1. xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  2. xsi:schemaLocation="http://www.springframework.org/schema/beans
  3. http://www.springframework.org/schema/beans/spring-beans.xsd
  4. http://www.springframework.org/schema/data/jpa
  5. http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
  6. <jpa:repositories base-package="net.codejava.spring.repository"/>
展开查看全部
cgh8pdjw

cgh8pdjw2#

在我的例子中,这是一个错误的用户导入的问题(而不是导入一个用户实体,我已经手动创建-我导入了另一个用户)。
我已经改变了导入组织。

导入数据库;

相关问题