Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring mvc@Autowired error未满足要求';必需的';类型依赖关系_Java_Spring_Spring Data_Autowired_Spring Data Mongodb - Fatal编程技术网

Java spring mvc@Autowired error未满足要求';必需的';类型依赖关系

Java spring mvc@Autowired error未满足要求';必需的';类型依赖关系,java,spring,spring-data,autowired,spring-data-mongodb,Java,Spring,Spring Data,Autowired,Spring Data Mongodb,我有处理用户实体的用户服务,在使用用户服务之前,@Autowired在用户控制器类中。所以,我得到了一个错误: 类型为[class com.yes.service.UserService]的未满足的“必需”依赖项。至少需要1个匹配bean 代码如下: 用户服务 package com.yes.service; import java.util.List; import java.util.UUID; import org.springframework.beans.factory.annot

我有处理用户实体的用户服务,在使用用户服务之前,@Autowired在用户控制器类中。所以,我得到了一个错误:

类型为[class com.yes.service.UserService]的未满足的“必需”依赖项。至少需要1个匹配bean

代码如下:

用户服务

package com.yes.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yes.domain.User;
import com.yes.repository.RoleRepository;
import com.yes.repository.UserRepository;

@Service
public class UserService {


    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleRepository roleRepository;

    public User create(User user) {
        user.setId(UUID.randomUUID().toString());
        user.getRole().setId(UUID.randomUUID().toString());
        // We must save both separately since there is no cascading feature
        // in Spring Data MongoDB (for now)
        roleRepository.save(user.getRole());
        return userRepository.save(user);
    }
    public User read(User user) {
        return user;
    }
    public List<User> readAll() {
        return userRepository.findAll();
    }
    public User update(User user) {
        User existingUser = userRepository.findByUsername(user.getUserName());
        if (existingUser == null) {
            return null;
        }
        existingUser.setFirstName(user.getFirstName());
        existingUser.setLastName(user.getLastName());
        existingUser.getRole().setRole(user.getRole().getRole());
        // We must save both separately since there is no cascading feature
        // in Spring Data MongoDB (for now)
        roleRepository.save(existingUser.getRole());
        return userRepository.save(existingUser);
    }
    public Boolean delete(User user) {
        User existingUser = userRepository.findByUsername(user.getUserName());
        if (existingUser == null) {
            return false;
        }
        // We must delete both separately since there is no cascading feature
        // in Spring Data MongoDB (for now)
        roleRepository.delete(existingUser.getRole());
        userRepository.delete(existingUser);
        return true;
    }
}
spring-data.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <context:property-placeholder
        properties-ref="deployProperties" />


    <!-- MongoDB host -->
    <mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}" />

    <!-- Template for performing MongoDB operations -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"
        c:mongo-ref="mongo" c:databaseName="${mongo.db.name}" />

    <!-- Activate Spring Data MongoDB repository support -->
    <mongo:repositories base-package="com.yes.repository" mongo-template-ref="mongoTemplate"/>



    <!-- Service for initializing MongoDB with sample data using MongoTemplate -->
    <bean id="initMongoService" class="com.yes.service.InitMongoService" init-method="init"/>
</beans>
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.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/applicationContext.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>

上下文配置位置
/WEB-INF/spring/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/
编辑:问题是在修复了Sotirios Delimanolis回答和评论中的错误后提出的

导致错误的问题是什么

回答:问题是索蒂里奥斯·德里马诺利斯回答中描述的。在对其答案的评论中描述的确切解决方案


谢谢

您的应用程序上下文和servlet上下文正在对相同的包进行组件扫描

您的应用程序上下文

<context:component-scan base-package="com.yes" />

与servlet上下文中的所有内容相比

<context:component-scan base-package="com.yes.service"/>
<context:component-scan base-package="com.yes.controller"/>
<context:component-scan base-package="com.yes.domain"/>
<context:component-scan base-package="com.yes.repository"/>
<context:component-scan base-package="com.yes.dto"/>


因此,一些bean将被覆盖。你不会想要这个的。servlet上下文应该扫描
@Controller
bean。应用程序上下文应该扫描所有其他内容,但不要让应用程序上下文扫描子(导入的)数据上下文已经扫描的内容。修复您的包声明,使所有这些声明不相交。

为什么您有这么多
mongo:repositories
条目?只保留第一个。另外两个正在声明不存在的包。@SotiriosDelimanolis谢谢。我删除该声明。但问题还在这里…再次感谢你。除了留在那里的
和userService中解决的问题之外,我将所有的
上下文:组件扫描
从servlet上下文转移到ApplicationContext,而不是旧的。。。,我能做什么?@yoni用新的上下文编辑您的上述问题。@yoni尝试从应用程序上下文中删除
。spring数据上下文已经扫描了该包。如果您有与spring数据不相关的
@Component
类,则必须将它们放在更具体的子文件夹中,并引用这些子文件夹。@yoni基于此,我认为您不需要
@Repository
注释。请发布异常的整个堆栈跟踪。我没有Mongo配置的经验。@yoni很抱歉要这么多文档,但这对您来说是春天。您的servlet上下文似乎没有从应用程序上下文继承bean。你能发布你的
web.xml
?或者你如何实例化你的上下文。
ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.yes.service.UserService com.yes.controller.UserController.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.yes.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.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/applicationContext.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>
<context:component-scan base-package="com.yes" />
<context:component-scan base-package="com.yes.service"/>
<context:component-scan base-package="com.yes.controller"/>
<context:component-scan base-package="com.yes.domain"/>
<context:component-scan base-package="com.yes.repository"/>
<context:component-scan base-package="com.yes.dto"/>