Java 无法自动关联字段(Spring mvc-批注)

Java 无法自动关联字段(Spring mvc-批注),java,spring,Java,Spring,我无法解决我的问题与春天的豆子。我用jar模块(有服务、dao、ropositories…)和war模块(有控制器和应用程序配置)创建了多模块项目。当我在Jboss上启动应用程序时,会出现以下异常: Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'login': Injection of autowired

我无法解决我的问题与春天的豆子。我用jar模块(有服务、dao、ropositories…)和war模块(有控制器和应用程序配置)创建了多模块项目。当我在Jboss上启动应用程序时,会出现以下异常:

Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'login': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.engineering.pawel.service.UserService com.engineering.pawel.controller.Login.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.engineering.pawel.repository.UserRepository com.engineering.pawel.service.UserService.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.engineering.pawel.repository.UserRepository] 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)}
这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
    version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml,
            /WEB-INF/spring/spring-security.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>

    <!-- ........................................................................... -->
    <!-- Spring Security -->
    <!-- ........................................................................... -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <persistence-unit-ref>
        <persistence-unit-ref-name>persistence/my-emf</persistence-unit-ref-name>
        <persistence-unit-name>my-jpa</persistence-unit-name>
    </persistence-unit-ref>

</web-app>

我将非常感谢您的帮助。

仔细查看您的异常情况将告诉您:

NoSuchBeanDefinitionException: No qualifying bean of type 
   [com.engineering.pawel.repository.UserRepository] found for dependency:
   expected at least 1 bean which qualifies as autowire candidate for this dependency. 
这仅仅意味着:

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

}
Spring上下文加载程序找不到UserRepository的子类。

在我的例子中


我从外部a.properties获取属性,但忘记在a.properties文件中添加属性。因为我们有一个包含类似文件(a.properties)的批处理应用程序,我在该批处理的属性文件中添加了值。

UserRepository的子类在哪里。。。?不能注入接口,必须在运行时注入UserRepository的实现。查看错误/例外仅猜测您的
根上下文.xml
没有扫描存储库/服务,但只有
servlet上下文.xml
是。我认为这是错误的答案。它应该可以工作,但他的配置是错误的。我使用Java配置文件,我也遇到了同样的问题,我通过添加@EnableJpaRepositories(basePackages={“com.smartintranet.repository”}修复了它。我不确定xml配置,但他可能应该使用
@Service
@Transactional
public class UserService {
    
    @Autowired
    private UserRepository userRepository;
    
    public void addUser(final String userNick, final String userPassword){
        final User user = new User();
        user.setNick(userNick);
        user.setPassword(userPassword);
        userRepository.saveAndFlush(user);
    }
    
    public List<User> getUsers(){
        return this.userRepository.findAll();
    }

}
@Controller
public class Login {
    
    @Autowired
    private UserService userService;
}
NoSuchBeanDefinitionException: No qualifying bean of type 
   [com.engineering.pawel.repository.UserRepository] found for dependency:
   expected at least 1 bean which qualifies as autowire candidate for this dependency. 
@Repository
public interface UserRepository extends JpaRepository<User,Integer>{

}
@Service
@Transactional
public class UserService {

    @Autowired
    private UserRepository userRepository;  // Here you need to have a Bean 
                    // Implementing this UserRepository as an autowire candidate
    ...
}