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

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

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

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中,我有以下代码

@Controller
public class UserController {

    private UserService employeeServiceImpl;

    @RequestMapping("/")
    public String employee() {
        this.employeeServiceImpl.listAlUsers();
        return "employee";
    }

    @Autowired(required = true)
    public void setEmployeeService(UserService employeeServiceImpl) {
        this.employeeServiceImpl = employeeServiceImpl;
    }

}

我的用户服务

public interface UserService {
    public abstract List<User> listAlUsers();
    public abstract User addUser(User user);
    public abstract int removeUser(int id);
    public abstract User updateUser(User user);
}

我的用户服务实现

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    @Qualifier("userJpaRepository")
    private UserRepository userJpaRepository;

    @Override
    public List<User> listAlUsers() {
        return userJpaRepository.findAll();
    }

    @Override
    public User addUser(User user) {
        return userJpaRepository.save(user);
    }

    @Override
    public int removeUser(int id) {
        userJpaRepository.delete(id);
        return 0;
    }

    @Override
    public User updateUser(User user) {
        return userJpaRepository.save(user);
    }
}

我的JpaRepository

@Repository("userJpaRepository")
public interface UserRepository extends JpaRepository<User,Serializable> {

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我的servlet上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!--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.-->
    <!--Step 1 : HandlerMapping -->
    <context:component-scan base-package="com.loiane.controller" />

    <!--JPA Repository-->

    <jpa:repositories base-package="com.loiane.repository.EmployeeJpaRepository"/>

    <mvc:annotation-driven />

    <!--Step 3 : View Resolver-->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <!--Bundles-->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <value>messages</value>
        </property>
    </bean>

    <!--Declaramos el interceptor para permitir el cambio de idioa en tiempo de ejecucion-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean id="localeChangeInterceptor"
                  class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <property name="paramName" value="lang" />
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>

    <!--Si el idioma no existe cargamos el en-->

    <bean id="localeResolver"
          class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="cookieName" value="lang" />
        <property name="defaultLocale" value="en" />
    </bean>

</beans>

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

56lgkhnf

56lgkhnf1#

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

@Autowired
private UserRepository userJpaRepository;

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

@Repository
public interface UserRepository extends JpaRepository<User,Integer> {

}

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

xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/data/jpa
     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

   <jpa:repositories base-package="net.codejava.spring.repository"/>
cgh8pdjw

cgh8pdjw2#

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

导入数据库;

相关问题