首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法自动显示字段(Spring注释)

无法自动显示字段(Spring注释)
EN

Stack Overflow用户
提问于 2015-01-11 12:09:32
回答 2查看 3.5K关注 0票数 2

我不能用注射春豆来解决我的问题。我用jar模块创建了多个模块项目(有服务、dao、ropositories .)和war模块(有控制器和aplications配置)。当我在Jboss上启动我的应用程序时,我会得到这样的例外:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>

下面是servlet-context.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee-4.0.xsd">


    <!-- Enables the Spring MVC @Controller programming model -->

    <annotation-driven />
    <context:annotation-config />

    <!-- Handles HTTP GET requests for /resources by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!-- Configure to plugin JSON as request and response in method handler -->
    <beans:bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter" />
            </beans:list>
        </beans:property>
    </beans:bean>

    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <beans:bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean>

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


    <!-- Database configuration -->
    <jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/postgreSQL"
        resource-ref="true" />

    <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:comp/env/persistence/my-emf"
        expected-type="javax.persistence.EntityManagerFactory" />

    <beans:bean id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <beans:property name="transactionManagerName" value="java:/TransactionManager" />
    </beans:bean>

    <beans:bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
</beans:beans>

下面是UserRepository.java

代码语言:javascript
复制
package com.engineering.pawel.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.engineering.pawel.pojo.User;

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

下面是UserService.java

代码语言:javascript
复制
@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();
    }

}

这是登录课:

代码语言:javascript
复制
@Controller
public class Login {
    
    @Autowired
    private UserService userService;
}

我会非常感谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-11 12:52:52

仔细观察一下您的异常将告诉您所有人:

代码语言:javascript
复制
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. 

这仅仅意味着:

代码语言:javascript
复制
@Repository
public interface UserRepository extends JpaRepository<User,Integer>{

}

代码语言:javascript
复制
@Service
@Transactional
public class UserService {

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

Spring没有找到UserRepository的子类。

票数 1
EN

Stack Overflow用户

发布于 2021-04-27 14:58:25

就我而言,

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27886864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档