我如何“解决”它: 最初的问题是我忘记在启动应用程序的主类中更改软件包名称:
@EnableJpaRepositories(basePackages = {"com.sda.VictorLiviu.ecommerce.jparepository"})
之后,我将itemMapper实例化为ItemService中的普通类,如下所示:
ItemMapper itemMapper = new ItemMapper();
,然后将其toEntity方法用作:
itemMapper.toEntity();
。 。 。 。 。 。 。 。 。 。 。
。我的Spring Boot应用程序说它显然没有Bean类。我将发布Pom,服务和存储库。我想指出的是,我为UserService和OrderService使用了完全相同的类和存储库,它们没有给出任何错误,唯一的区别是@Transactional,我将其删除并得到了相同的错误。欢迎任何建议! / p>
错误:
Description:
Field repository in com.sda.VictorLiviu.ecommerce.service.ItemService required a bean of type 'com.sda.VictorLiviu.ecommerce.JpaRepository.ItemRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.sda.VictorLiviu.ecommerce.JpaRepository.ItemRepository' in your configuration.
POM XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sda.VictorLiviu</groupId>
<artifactId>e-commerce</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>e-commerce</name>
<description>E-commerce website Final Project sda</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ItemService类
package com.sda.VictorLiviu.ecommerce.service;
import com.sda.VictorLiviu.ecommerce.DtoMapper.ItemMapper;
import com.sda.VictorLiviu.ecommerce.JpaRepository.ItemRepository;
import com.sda.VictorLiviu.ecommerce.exceptions.ItemNotFoundException;
import com.sda.VictorLiviu.ecommerce.model.Item;
import com.sda.VictorLiviu.ecommerce.dto.ItemDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Transactional
@Service
public class ItemService {
@Autowired
ItemRepository repository;
@Autowired
ItemMapper itemMapper;
public void add(ItemDto dto) {
repository.save(itemMapper.toEntity(dto));
}
public Item delete(Long id) {
repository.deleteById(id);
return null;
}
public List<Item> getItems() {
return repository.findAll();
}
public Item getItemById(Long id) {
Optional<Item> optionalItem = repository.findById(id);
return optionalItem.orElseThrow( () -> new ItemNotFoundException("Couldn't find the specific item!"));
}
}
存储库
package com.sda.VictorLiviu.ecommerce.JpaRepository;
import com.sda.VictorLiviu.ecommerce.model.Item;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {
}
物品类别
package com.sda.VictorLiviu.ecommerce.model;
import javax.persistence.*;
@Entity
@Table(name="item")
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "item_seq_gen")
@SequenceGenerator(name = "item_seq_gen", sequenceName = "item_seq", allocationSize = 1)
@Column(name = "id")
private Long id;
@Column(name = "price")
private double price;
@Column(name = "name")
private String name;
@Column(name = "category")
private String category;
@Column(name = "quantity")
private int quantity;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
ItemDto类
package com.sda.VictorLiviu.ecommerce.dto;
public class ItemDto {
private Long id;
private double price;
private String name;
private String category;
private int quantity;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
Application.properties
logging.level.org.springframework=info
jwt.signing.key.secret=mySecret
jwt.get.token.uri=/authenticate
jwt.refresh.token.uri=/refresh
jwt.http.request.header=Authorization
jwt.token.expiration.in.seconds=604800
spring.jpa..show-sql=true
spring.h2.console.enabled=true
!!!!!! Image with my packages
答案 0 :(得分:0)
您需要打开jpa存储库处理。
为此,请用@EnableJpaRepositories
注释您的应用程序