java - 没有可用的“repository.Dao”类型的合格 bean
问题描述
当我运行此代码时,它显示了这种错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“orderController”的bean时出错:通过方法“setOrderService”参数0表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: Error created bean with name 'orderServiceImpl' defined in file [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。依赖注释:{} at org.springframework.beans.factory.annotation。\projects\crudmvc\out\artifacts\crudmvc_war_exploded\WEB-INF\classes\service\OrderServiceImpl.class]:通过构造函数参数0表示的不满足依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“repository.Dao”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注释:{} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:787) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:226) at org. springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358) 在 org.springframework。预计至少有 1 个 bean 有资格作为 autowire 候选者。依赖
控制器:
@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;
}
}
弹簧上下文.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>
休眠.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>
解决方案
我在 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;
现在没有错误,一切正常
推荐阅读
- css - 如何在剪辑的 div 中正确定位图像列表
- actionlistener - 使用外部资源时未解决的编译错误
- angular - 动态插入角度 5
- c# - C# - 按选定列对矩形数组进行排序
- pandas - 为什么 sum 和 lambda sum 的变换不同?
- jquery - 从 div hidden 开始,然后使用 Animate.css fadeIn 和 fadeOut 切换
- javascript - Firebase 查询:“where”子句不起作用,继续返回所有文档
- ios - 更改帧/方向时加载 YTPlayerView
- xamarin - Xamarin Forms 应用程序未在 iOS 模拟器中显示
- gradle - 使用 Gradle 时如何从多模块项目构建 jar?