当前位置: 首页 > 知识库问答 >
问题:

Spring数据的Spring不满足依赖关系

谭俊
2023-03-14
@Component
public interface RoleRepo extends JpaRepository<Role, Long> {
    @Query("from Role ro order by ro.name")
    List<Role> getRoles();  
}
@Service
@Component
public class SecurityServiceImpl  {

    @Autowired
    RoleRepo roleRepo;

    @PostConstruct
    public void init() {
        System.out.println(roleRepo);
    }

    public SecurityServiceImpl (){
        System.out.println("SecurityServiceImpl created");
    }
}

在SecurityServiceImpl中注入此RoleRepo时,我面临此错误。

我的spring上下文文件

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

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->

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

<!-- 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>

<!-- Create DataSource Bean -->

<beans:bean id="dbDataSource"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <beans:property name="jndiName"
        value="java:comp/env/jdbc/EmdDS" />
</beans:bean>


<jee:jndi-lookup id="dbDataSource" jndi-name="EmdDS"
    expected-type="javax.sql.DataSource" />

<!-- using JEE namespace for lookup -->
<!-- <jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/MyLocalDB" expected-type="javax.sql.DataSource" 
    /> -->

<context:component-scan
    base-package="com.example.jpa.hibernate" />


<!-- Hibernate 3 Annotation SessionFactory Bean definition -->
<beans:bean id="hibernate3AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <beans:property name="dataSource" ref="dbDataSource" />
    <beans:property name="annotatedClasses">
        <beans:list>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect
            </beans:prop>
            <beans:prop key="hibernate.current_session_context_class">thread</beans:prop>
            <beans:prop key="hibernate.show_sql">false</beans:prop>
            <beans:prop key="hibernate.hbm2ddl.auto">create</beans:prop>
        </beans:props>
    </beans:property>

</beans:bean>

我的角色类

     @Entity
            @Table(name = "A_ROLE")
            @NamedQuery(name = "Role.findAll", query = "SELECT f FROM Role f")
public class Role extends AbstractModel {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "ROLE_ID_GENERATOR", sequenceName = "ROLE_SEQ", allocationSize = 1, initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ROLE_ID_GENERATOR")
    private Long id;

    private String description;

    private String name;

    // bi-directional many-to-one association to FrtUserRole
    @OneToMany(mappedBy = "role", orphanRemoval = true)
    private Set<UserRole> userRoles = new LinkedHashSet<UserRole>();

    public Role() {
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<UserRole> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(Set<UserRole> userRoles) {
        this.userRoles = userRoles;
    }

    @SuppressWarnings("unchecked")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return name;
    }
}

共有1个答案

巫马翰翮
2023-03-14

首先要知道错误说什么,让翻译一下错误说什么:

例如:

 NoSuchBeanDefinitionException: No qualifying bean of type 'com.tk.emd.ui.service.RoleRepo' 

也就是说,尝试注入一个未定义的bean,其原因是在Spring上下文中未定义所需的bean,您会得到这个错误消息

expected at least 1 bean which qualifies as autowire candidate.

您想要的bean并不存在于正确注释为bean(@component、@respository、@service等)的所需bean的上下文中,该bean可能定义在未被Spring扫描的包中。

因此,正如我们所看到的,上下文文件配置中没有roleRepo的bean id的定义。

 类似资料:
  • 我看了所有类似的问题,没有一个能帮助我,所以事情是这样的: 为了在不同的程序中重现错误,我创建了一个简单的Spring Boot程序。在尝试运行它时,我遇到了一个我无法解决的非常奇怪的错误: 相关类别: 通用域名格式。实例演示。演示应用程序。爪哇: com.example.demo.configs.RootConfiguration.java 通用域名格式。实例演示。控制器。测试控制器。爪哇: 我

  • 我从Spring MVC开始,阅读了《Spring in Action》一书,我正在使用Spitter应用程序进行第5章的练习,但我遇到了以下错误: org.springframework.beans.factory.[C:\xampp\htdocs.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\spitter\WEB-I

  • java.lang.noClassDefoundError:scala/collection/gentraversableonce$class at kafka.utils.pool.(pool.scala:28)~[kafka2.10-0.8.1.1.jar:na] at kafka.consumer.FetchRequestandResponseStatsRegistry$.~[kafka2.

  • 我有一个属性XML文件,如下所示: 我该怎么解决这个?

  • 我正在尝试使用cucumber框架与selenium和appium,但在执行cucumber特性时,我得到以下异常: @CucumberOptions(features={“src//test//java//feature”},glue={“pages”},plugin={“pretty”,“html:target/cucumber”},tags={“@web”,“@test”,“@appium”