简体   繁体   English

找不到相依路径类型为path.repositoryClass的相符bean,至少应有1个符合此相依条件的自动装配候选

[英]No matching bean of type path.repositoryClass found for dependency expected at least 1 bean which qualifies as autowire candidate for this dependency

I am new to Spring, and have searched extensively to resolve this problem, but no success yet. 我是Spring的新手,已经进行了广泛的搜索来解决此问题,但尚未成功。
Under src/WEB-INF folder I have two files: 在src / WEB-INF文件夹下,我有两个文件:

1. dispatcherServlet-servlet.xml
2. web.xml

Under src/META-INF, I have below files: 在src / META-INF下,我有以下文件:

1. applicationContext.xml 
2. hibernate-context.xml 
3. orm.xml
4. persistence.xml 
5. spring-data.xml 
6. spring.properties

Source code for all is listed after the question in the sequence given above. 所有问题的源代码均按上述顺序在问题后列出。
I am not getting the error message (which is mentioned in subject) when I am removing the below line from dispatcherServlet-servlet.xml: 当我从dispatcherServlet-servlet.xml中删除以下行时,没有得到错误消息(在主题中提到):

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

But then it's not creating the tables in DB. 但这不是在数据库中创建表。

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext*.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<session-config>
  <session-timeout>60</session-timeout>
</session-config>

dispatcherServlet-servlet.xml dispatcherServlet-servlet.xml

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

<mvc:annotation-driven/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<context:component-scan base-package="com.myapp.controller"/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

applicationContext.xml applicationContext.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="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
    http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="com.myapp">
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>   

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

<!-- Load Hibernate related configuration -->
<import resource="hibernate-context.xml" />

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
     <array>
       <bean id="jsonConverter"
          class="com.myapp.util.MappingJackson2HttpMessageConverter">
         <property name="objectMapper">
           <bean class="com.myapp.util.HibernateAwareObjectMapper"/>
         </property>
       </bean>
     </array>
    </property>

<!-- Imports datasource configuration -->
<import resource="spring-data.xml"/>

<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"
p:location="/META-INF/spring.properties" />

hibernate-context.xml hibernate-context.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:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
                    http://www.springframework.org/schema/data/jpa
                    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        ">

<context:property-placeholder location="/META-INF/spring.properties" />
<jpa:repositories base-package="com.myapp.repository" />
<tx:annotation-driven transaction-manager="transactionManager" />   

<!-- Declare a datasource that has pooling capabilities-->   
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close"
            p:driverClass="${app.jdbc.driverClassName}"
            p:jdbcUrl="${app.jdbc.url}"
            p:user="${app.jdbc.username}"
            p:password="${app.jdbc.password}"
            p:acquireIncrement="5"
            p:idleConnectionTestPeriod="60"
            p:maxPoolSize="100"
            p:maxStatements="50"
            p:minPoolSize="10" />

</beans>

orm.xml orm.xml

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.0" xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
<persistence-unit-metadata>
    <persistence-unit-defaults>
    </persistence-unit-defaults>
</persistence-unit-metadata>

persistence.xml persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <!--mapping-file>./META-INF/orm.xml</mapping-file-->
            <class>com.myapp.model.ACLEntityManager</class>
            <class>com.myapp.model.MerchantACL</class>
            <class>com.myapp.model.Resource</class>
            <class>com.myapp.model.AdminACL</class>
            <class>com.myapp.model.Aggregator</class>
            <class>com.myapp.model.Customer</class>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
    </properties>
    <mapping-file>./META-INF/orm.xml</mapping-file>
</persistence-unit>

spring-data.xml 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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.1.xsd">

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

<tx:annotation-driven transaction-manager="transactionManager" />   

<!-- Activate Spring Data JPA repository support -->
<jpa:repositories base-package="com.myapp.repository" />

<!-- Declare a datasource that has pooling capabilities-->   
<bean id="jpaDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"
    p:driverClass="${app.jdbc.driverClassName}"
    p:jdbcUrl="${app.jdbc.url}"
    p:user="${app.jdbc.username}"
    p:password="${app.jdbc.password}"
    p:acquireIncrement="5"
    p:idleConnectionTestPeriod="60"
    p:maxPoolSize="100"
    p:maxStatements="50"
    p:minPoolSize="10" />

<!-- Declare a JPA entityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
            p:persistenceXmlLocation="./META-INF/persistence.xml"
    p:persistenceUnitName="hibernatePersistenceUnit"
    p:dataSource-ref="jpaDataSource"
    p:jpaVendorAdapter-ref="hibernateVendor"/>

<!-- Specify our ORM vendor -->
<bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
            p:showSql="true"/>

<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" 
    p:entityManagerFactory-ref="entityManagerFactory"/>

spring.properties spring.properties

# database properties
app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost/mydb
app.jdbc.username=root
app.jdbc.password=

Stack Trace: 堆栈跟踪:

SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminACLController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.myapp.service.AdminACLService com.myapp.controller.AdminACLController.adminACLService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminACLService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.myapp.repository.AdminACLRepository com.myapp.service.AdminACLService.adminACLRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.myapp.repository.AdminACLRepository] 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)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1267)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1186)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1081)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3920)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Thread.java:722)

I am guessing that com.myapp.service.AdminACLService does not have an @Service annotation on it. 我猜com.myapp.service.AdminACLService上没有@Service批注。 If you do have that @Service annotation on that class you also need to make sure that you are scanning this package. 如果确实在该类上具有@Service批注,则还需要确保正在扫描此程序包。

Not sure why you are doing this 不知道为什么要这么做

<context:component-scan base-package="com.myapp">
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>   

Though I am not sure if that is a problem 虽然我不确定这是否是一个问题

AdminACLController AdminACLController

package com.myapp.controller;

import com.myapp.dto.AdminACLDTO;
import com.myapp.exception.NotFoundException;
import com.myapp.model.AdminACL;
import com.myapp.service.AdminACLService;
import java.security.NoSuchAlgorithmException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class AdminACLController {
@Autowired
private AdminACLService adminACLService;

@RequestMapping(value = "/adminACL", method = RequestMethod.POST, headers = "Accept=application/json")
public @ResponseBody
AdminACLDTO add(
        // @RequestParam(value = "sessionUserId") String sessionUserId,

        //private Integer ;
//private String adminName;
        @RequestParam(value = "adminId") String adminId,
        @RequestParam(value = "adminName") String adminName
        ) 
        throws NotFoundException, NoSuchAlgorithmException {


    AdminACL checkAdminACL = this.adminACLService.getAdminACL(adminId);

    if (checkAdminACL == null) {
        AdminACL adminACL = new AdminACL();
        adminACL.setAdminId(Integer.parseInt(adminId));
        adminACL.setName(adminName);

        this.adminACLService.addAdminACL(adminACL);
        AdminACLDTO adminACLDTO = new AdminACLDTO(adminACL);
        return adminACLDTO;

    }
    throw new NotFoundException("Resource already added");
}
}

AdminACLService AdminACLService

package com.myapp.service;

import com.myapp.model.AdminACL;
import com.myapp.repository.AdminACLRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AdminACLService {
@Autowired
AdminACLRepository adminACLRepository;

private static final Logger logger = LoggerFactory.getLogger(MerchantACLService.class);

@Transactional
public AdminACL addAdminACL(AdminACL admin) {
     adminACLRepository.save(admin);      
     return admin;   
   }

@Transactional
public AdminACL getAdminACL(String adminId) {
  AdminACL policy = adminACLRepository.findByAdminId(Long.parseLong(adminId));
  return policy;
}
}

AdminACLRepository AdminACL储存库

package com.myapp.repository;


import com.myapp.model.AdminACL;
import org.springframework.data.jpa.repository.JpaRepository;


public interface AdminACLRepository extends JpaRepository<AdminACL, Long> {
AdminACL findByAdminId(Long adminId);
}

In order to make use of Spring's JpaRepository , you need to add some Spring JPA magic: 为了使用Spring的JpaRepository ,您需要添加一些Spring JPA魔术:

<beans
    ...
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        ...
        ">

  <jpa:repositories base-package="com.myapp.repository" />

In your case, I'd suggest to add it to the hibernate-context.xml file. 对于您的情况,建议将其添加到hibernate-context.xml文件中。

First try 第一次尝试

Seems like you did not annotate com.myapp.repository.AdminACLRepository (or any implementation thereof) as a @Component / @Service . 似乎您没有将com.myapp.repository.AdminACLRepository (或其任何实现)注释为@Component / @Service

暂无
暂无

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

相关问题 没有匹配的 bean 类型……为依赖找到……预计至少有 1 个符合自动装配候选者的 bean - No matching bean of type … found for dependency....expected at least 1 bean which qualifies as autowire candidate 没有为依赖项找到UserRepository类型的限定bean:预期至少有1个bean符合此依赖项的autowire候选者 - No qualifying bean of type UserRepository found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency 未找到依赖项的类型为[PATHTOCLASS]的合格Bean:至少应有1个符合此依赖项自动候选条件的Bean - No qualifying bean of type [PATHTOCLASS] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency Crudrepository - 没有为依赖找到符合条件的 bean:预计至少有 1 个 bean 有资格作为这个依赖的自动装配候选者 - Crudrepository - No qualifying bean of type found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency 未找到依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。 依赖注解: - No found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: jira rest 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者 - jira rest expected at least 1 bean which qualifies as autowire candidate for this dependency 未找到依赖项[**。Clients]的合格bean:至少应有1个合格为autowire候选对象的bean。 依赖注释:{} - No qualifying bean found for dependency [**.Clients]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} Spring 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者 - Spring expected at least 1 bean which qualifies as autowire candidate for this dependency NoSuchBeanDefinitionException期望至少有一个bean可以作为此依赖项的autowire候选者 - NoSuchBeanDefinitionException expected at least 1 bean which qualifies as autowire candidate for this dependency 预计至少有 1 个符合自动装配候选条件的 bean。 依赖注解 - expected at least 1 bean which qualifies as autowire candidate. Dependency annotations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM