没有可用的“repository.Dao”类型的合格 bean
Posted
技术标签:
【中文标题】没有可用的“repository.Dao”类型的合格 bean【英文标题】:No qualifying bean of type 'repository.Dao' available 【发布时间】:2020-06-10 09:19:05 【问题描述】:当我运行这段代码时,它显示了这种错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“orderController”的bean时出错:不满意 通过方法'setOrderService'参数0表示的依赖关系; 嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建文件中定义的名称为“orderServiceImpl”的 bean 时出错 [C:\projects\crudmvc\out\artifacts\crudmvc_war_exploded\WEB-INF\classes service\OrderServiceImpl.class]: 通过构造函数参数 0 表示的不满足的依赖关系; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 “repository.Dao”类型的合格 bean 可用:预期 至少 1 个符合自动装配候选资格的 bean。依赖 注释: 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:719) 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 在 org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:401) 在 org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:292) 在 org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103) 在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4699) 在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5165) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743) 在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719) 在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705) 在 org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1720) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 方法)在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:566) 在 org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287) 在 java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:809) 在 java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 在 org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:479) 在 org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:428) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 方法)在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:566) 在 org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287) 在 java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:809) 在 java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 在 java.management/com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468) 在 java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466) 在 java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307) 在 java.base/java.security.AccessController.doPrivileged(本机 方法)在 java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1406) 在 java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:827) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 方法)在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:566) 在 java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359) 在 java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200) 在 java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197) 在 java.base/java.security.AccessController.doPrivileged(本机 方法)在 java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196) 在 java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562) 在 java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796) 在 java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677) 在 java.base/java.security.AccessController.doPrivileged(本机 方法)在 java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676) 在 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 在 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) 在 java.base/java.lang.Thread.run(Thread.java:834) 引起: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建文件中定义的名称为“orderServiceImpl”的 bean 时出错 [C:\projects\crudmvc\out\artifacts\crudmvc_war_exploded\WEB-INF\classes\service\OrderServiceImpl.class]: 通过构造函数参数 0 表示的不满足的依赖关系; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 “repository.Dao”类型的合格 bean 可用:预期 至少 1 个符合自动装配候选资格的 bean。依赖 注释: 在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:787) 在 org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:226) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 在 org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:711) ... 61 更多原因: org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 “repository.Dao”类型的合格 bean 可用:预期 至少 1 个符合自动装配候选资格的 bean。依赖
控制器:
@Controller
@RequestMapping("/general")
public class OrderController
private OrderService orderService;
@Autowired
@Qualifier("orderServiceImpl")
public void setOrderService(OrderService orderService)
this.orderService = orderService;
@GetMapping(value = "/list")
public String get(Model theModel)
theModel.addAttribute("orders", orderService.getOrders());
return "index";
@GetMapping(value = "/showFormForAdd")
public String add(Model theModel)
Orders orders = new Orders();
theModel.addAttribute("order", orders);
return "create-orders";
@PostMapping(value = "/saveOrders")
public String addOrders(@ModelAttribute Orders orders, @RequestParam MultipartFile file) throws IOException
orders = orderService.uploadOrders(orders, file);
orderService.save(orders);
return "redirect:/general/list";
型号:
@Entity
@Table(name = "orders")
public class Orders
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "description")
private String description;
@Column(name = "price")
private int price;
@Column(name = "sold")
private boolean sold;
@Column(name = "body")
private String body;
@Column(name = "brand")
private String brand;
@Column(name = "engine")
private String engine;
@Column(name = "model")
private String model;
@Lob
@Column(name = "image")
private byte[] image;
@Column(name = "imageStr")
private String imageStr;
public Orders()
public Orders(int id)
this.id = id;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getDescription()
return description;
public void setDescription(String description)
this.description = description;
public boolean isSold()
return sold;
public void setSold(boolean sold)
this.sold = sold;
public int getPrice()
return price;
public void setPrice(int price)
this.price = price;
public String getBody()
return body;
public void setBody(String body)
this.body = body;
public String getBrand()
return brand;
public void setBrand(String brand)
this.brand = brand;
public String getEngine()
return engine;
public void setEngine(String engine)
this.engine = engine;
public String getModel()
return model;
public void setModel(String model)
this.model = model;
public byte[] getImage()
return image;
public void setImage(byte[] image)
this.image = image;
public String getImageStr()
return imageStr;
public void setImageStr(String imageStr)
this.imageStr = imageStr;
存储库:
@Repository
public interface Dao extends CrudRepository<Orders, Long>
订单服务:
public interface OrderService
List<Orders> getOrders();
Orders uploadOrders(Orders orders, MultipartFile file) throws IOException;
void save(Orders orders);
OrderServiceImpl:
@Service
public class OrderServiceImpl implements OrderService
private Dao dao;
@Autowired
public OrderServiceImpl(Dao dao)
this.dao = dao;
@Override
public List<Orders> getOrders()
return Lists.newArrayList(dao.findAll());
@Override
public void save(Orders orders)
dao.save(orders);
@Override
public Orders uploadOrders(Orders orders, MultipartFile file) throws IOException
if (!file.isEmpty())
String uploadDir = file.getOriginalFilename();
orders.setImage(file.getBytes());
orders.setImageStr(uploadDir);
return orders;
spring-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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<import resource="hibernate.cfg.xml"/>
<bean id = "dao" class ="repository.Dao" abstract="true"/>
<context:component-scan
base-package="
controller
repository
service">
</context:component-scan>
</beans>
hibernate.cfg.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url"/>
<property name="username" value="postgres"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan" value="models"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="myTransactionManager"/>
</beans>
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-context.xml
</param-value>
</context-param>
<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-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
【问题讨论】:
你用弹簧靴吗? 不是弹簧靴 但是你尝试使用spring data jpa:interface Dao extends CrudRepository<Orders, Long>
?
如何打包您的应用程序?是耳朵,战争吗?
是的,我想使用 spring data jpa。我把它打包在战争中。
【参考方案1】:
我在 hibernate.cfg.xml 中添加了这样的代码:
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url"/>
<property name="username" value="postgres"/>
<property name="password" value="root"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="models" />
<property name="dataSource" ref="myDataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.mysqlDialect</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
</property>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<jpa:repositories base-package="repository" entity-manager-factory-ref="entityManagerFactory" />
我还在 OrderServiceImpl 类中添加了这个:
@Resource
private Dao dao;
现在没有错误,一切正常
【讨论】:
以上是关于没有可用的“repository.Dao”类型的合格 bean的主要内容,如果未能解决你的问题,请参考以下文章