简体   繁体   English

创建名称为“ userService”的bean时出错:通过字段“ userJpaRepository”表示的不满意的依赖性

[英]Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userJpaRepository'

I have the following error when I try to acces into main page 当我尝试进入主页时出现以下错误

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)}

In my UserController I have the following code 在我的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;
    }

}

My UserService 我的用户服务

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);
}

My UserServiceImpl 我的UserServiceImpl

@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);
    }
}

My JpaRepository 我的Jpa资料库

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

}

web.xml 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>

My servlet-context 我的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>

The root-context is empty. 根上下文为空。

I don't know where is the possible cause of error I try to find differents options , but If I use the DAO pattern I receive the same error , so I would to know which is to problem to try to solve it. 我不知道哪里可能是造成错误的原因,所以我尝试找到不同的选项,但是如果我使用DAO模式,则会收到相同的错误,因此我想知道尝试解决该问题的原因。

The configuration of the project is with xml but I think there is not important to solve this situation. 该项目的配置使用xml,但我认为解决这种情况并不重要。

Regards ! 问候 !

You are using bean qualifier name on repository interface @Repository("userJpaRepository") . 您正在存储库接口@Repository("userJpaRepository")上使用Bean限定符名称。 Qualifier names are useful if more than 1 class is implementing the common interface. 如果有多个类在实现公共接口,则限定符名称很有用。 It helps to resolve the specific bean, which is to be autowired to other bean's interface variable. 它有助于解析特定的bean,该bean将自动连接到其他bean的接口变量。 Declaring it on an interface will defeat its purpose. 在接口上声明它会破坏其目的。 Removing the qualifier name should work if no other class (other than spring created proxy bean whose name would be userRepositoryImpl ) is implementing UserRepository interface. 如果没有其他类(名称为userRepositoryImpl spring创建的代理bean userRepositoryImpl )没有实现UserRepository接口,则删除限定符名称应该可行。

@Autowired
private UserRepository userJpaRepository;

And replace the Serializable with the Datatype of the ID column like Integer or Idclass. 并用ID列的数据类型(如Integer或Idclass)替换Serializable。

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

}

Now to enable spring to scan for the interface that extent its predefined repository interfaces, add package to scan for in your spring context 现在要使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"/> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建名称为“ userController”的bean时出错:通过字段“ userService”表示的不满意的依赖性 - Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService Spring-data:创建名为“mainController”的 bean 时出错:通过字段“userService”表达的不满足的依赖关系 - Spring-data : Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userService' JAVA:创建带有名称的 bean 时出错; 通过字段表达的不满意依赖 - JAVA: Error creating bean with name; Unsatisfied dependency expressed through field 使用名称创建 bean 时出错:通过字段表示的不满足依赖关系 - Error creating bean with name : Unsatisfied dependency expressed through field 使用名称创建 bean 时出错:通过字段表示的不满足的依赖关系 - Error creating bean with name: Unsatisfied dependency expressed through field 创建通过字段 sessionFactory 表示的 bean 不满足的依赖项时出错 - Error creating bean unsatisfied dependency expressed through field sessionFactory Bean名称不满意的依赖项异常通过字段表示的不满意的依赖性 - UnsatisfiedDependencyException Error with bean name Unsatisfied dependency expressed through field 创建 bean 时出错; 通过字段表达的不满意依赖 - Error creating bean; Unsatisfied dependency expressed through field 为什么会出现错误“创建名称为&#39;***&#39;的bean时出错:通过字段&#39;***&#39;表示的不满意依赖性” - Why Getting error “Error creating bean with name '***': Unsatisfied dependency expressed through field '***'” 创建名称为&#39;FilterService&#39;的bean时出错:通过字段&#39;filterDAO&#39;表示的不满意依赖性 - Error creating bean with name 'FilterService': Unsatisfied dependency expressed through field 'filterDAO'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM