现在出现此错误:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-06-24 15:55:37.651 ERROR 6924 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryController' defined in file [D:\Max\JavaWorks\Diplom\OnlineShop\target\classes\com\maximmalikov\onlineshop\rest\DeliveryController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deliveryService' defined in file [D:\Max\JavaWorks\Diplom\OnlineShop\target\classes\com\maximmalikov\onlineshop\service\DeliveryService.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ordersRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.maximmalikov.onlineshop.domain.Orders com.maximmalikov.onlineshop.repository.OrdersRepository.getByUserId(com.maximmalikov.onlineshop.domain.Users)! No property userId found for type Orders! Did you mean 'users'?
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:732) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at...
Process finished with exit code 0
我的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>
<groupId>com.maximmalikov</groupId>
<artifactId>onlineshop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>onlineshop</name>
<description>Onlineshop project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-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>
主类,服务类和带有存储库的控制器类:
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class})
@SpringBootApplication
public class OnlineshopApplication {
public static void main(String[] args) {
SpringApplication.run(OnlineshopApplication.class, args);
}
}
@Repository
public interface OrdersRepository extends JpaRepository<Orders,Long> {
Orders getByOrderNumber(long orderNumber);
Orders getByOrderDate(Date orderDate);
Orders getByGoodsProductId(Goods productId);
Orders getByGoodsProductName(Goods productName);
}
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrdersService {
private final OrdersRepository ordersRepository;
private final GoodsRepository goodsRepository;
private final UsersRepository usersRepository;
private final DeliveryRepository deliveryRepository;
private OrdersDTO fromOrders(Orders order) {
if (order != null) {
return OrdersDTO.builder()
.orderNumber(order.getOrderNumber())
.orderDate(order.getOrderDate())
.orderTime(order.getOrderTime())
.orderStatus(order.getOrderStatus())
.paymentMethod(order.getPaymentMethod())
.goods(order.getGoods() != null
? order.getGoods().stream()
.map(Goods::getProductId)
.collect(Collectors.toList())
: null)
.users(order.getUsers() != null
? order.getUsers().stream()
.map(Users::getUserId)
.collect(Collectors.toList())
: null)
.delivery(order.getDelivery().getOrderNumber())
.build();
}
return null;
}
private Orders fromDTO(OrdersDTO ordersDTO) {
if (ordersDTO != null) {
return Orders.builder()
.orderNumber(ordersDTO.getOrderNumber())
.orderDate(ordersDTO.getOrderDate())
.orderTime(ordersDTO.getOrderTime())
.orderStatus(ordersDTO.getOrderStatus())
.paymentMethod(ordersDTO.getPaymentMethod())
.goods(ordersDTO.getGoods() != null
? goodsRepository.findAllById(ordersDTO.getGoods())
: null)
.users(ordersDTO.getUsers() != null
? usersRepository.findAllById(ordersDTO.getUsers())
: null)
.delivery(ordersDTO.getDelivery() > 0L
? deliveryRepository.getOne(ordersDTO.getDelivery())
: null)
.build();
}
return null;
}
public List<OrdersDTO> getAllOrders() {
return ordersRepository.findAll().stream()
.map(this::fromOrders)
.collect(Collectors.toList());
}
@Transactional
public OrdersDTO addOrder(OrdersDTO ordersDTO) {
if (!ordersRepository.existsById(ordersDTO.getOrderNumber())) {
return fromOrders(ordersRepository.saveAndFlush(fromDTO(ordersDTO)));
}
return null;
}
@Transactional
public void deleteByOrderNumber(long orderNumber) {
if (ordersRepository.existsById(orderNumber)) {
ordersRepository.deleteById(orderNumber);
}
}
@Transactional
public OrdersDTO updateOrder(OrdersDTO ordersDTO) {
if (ordersRepository.existsById(ordersDTO.getOrderNumber())) {
Orders ordersTemp = ordersRepository.getOne(ordersDTO.getOrderNumber());
ordersTemp.setOrderDate(ordersDTO.getOrderDate());
ordersTemp.setOrderTime(ordersDTO.getOrderTime());
ordersTemp.setOrderStatus(ordersDTO.getOrderStatus());
ordersTemp.setPaymentMethod(ordersDTO.getPaymentMethod());
return fromOrders(ordersRepository.saveAndFlush(ordersTemp));
}
return null;
}
public OrdersDTO getByOrderNumber(long orderNumber) {
if (ordersRepository.existsById(orderNumber)) {
return fromOrders(ordersRepository.getByOrderNumber(orderNumber));
}
return null;
}
public OrdersDTO getByOrderDate(Date orderDate) {
return fromOrders(ordersRepository.getByOrderDate(orderDate));
}
public OrdersDTO getByProductId(Goods product) {
return fromOrders(ordersRepository.getByGoodsProductId(goodsRepository.getByProductId(product.getProductId())));
}
public OrdersDTO getByProductName(Goods product) {
return fromOrders(ordersRepository.getByGoodsProductName(goodsRepository.getByProductName(product.getProductName())));
}
}
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrdersController {
private final OrdersService ordersService;
private final GoodsRepository goodsRepository;
@GetMapping
public List<OrdersDTO> getAll() {
return ordersService.getAllOrders();
}
@PostMapping
public ResponseEntity<OrdersDTO> addOrder(@RequestBody OrdersDTO ordersDTO) {
OrdersDTO ordersDTO1 = ordersService.addOrder(ordersDTO);
return ResponseEntity.ok(ordersDTO1);
}
@DeleteMapping("/{order_number}")
public ResponseEntity<Void> deleteOrder(@PathVariable(value = "order_number") long orderNumber) {
try {
ordersService.deleteByOrderNumber(orderNumber);
return ResponseEntity.ok().build();
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}
@PutMapping
public ResponseEntity<OrdersDTO> updateOrder(@RequestBody OrdersDTO ordersDTO) {
OrdersDTO ordersDTO1 = ordersService.updateOrder(ordersDTO);
return ResponseEntity.ok(ordersDTO1);
}
@GetMapping("/{order_number}")
public ResponseEntity<OrdersDTO> getOrderByNumber(@PathVariable(value = "order_number") long orderNumber) {
return ResponseEntity.ok(ordersService.getByOrderNumber(orderNumber));
}
@GetMapping("/{order_date}")
public ResponseEntity<OrdersDTO> getOrderByDate(@PathVariable(value = "order_date") Date orderDate) {
return ResponseEntity.ok(ordersService.getByOrderDate(orderDate));
}
@GetMapping("/goods/{product_id}")
public ResponseEntity<OrdersDTO> getOrderByProductId(@PathVariable(value = "product_id") long productId) {
return ResponseEntity.ok(ordersService.getByProductId(goodsRepository.getByProductId(productId)));
}
@GetMapping("/goods/{product_name}")
public ResponseEntity<OrdersDTO> getOrderByProductName(@PathVariable(value = "product_name") String productName) {
return ResponseEntity.ok(ordersService.getByProductName(goodsRepository.getByProductName(productName)));
}
}
在application.properties中配置数据库连接:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/onlineshopbd?useSSL=false
spring.datasource.username=root
spring.datasource.password=malikovmaxim1997
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.open-in-view = true
该项目使用Spring-Boot配置,并且所有配置均仅通过注释进行配置,也就是说,我在Internet上也遇到了很多相同的问题,但是所有bin设置都在xml文件中配置。此方法不适用于该项目。帮助您了解此错误以及如何在不使用xml的情况下调整垃圾箱。
答案 0 :(得分:0)
看着你pom.xml我注意到你有spring-boot-starter-data-jpa
和hibernate-entitymanager version 5.2.17.Final
spring-boot-starter-data-jpa将获取您需要的所有休眠依赖项
因此从pom.xml中删除此依赖项,它应该可以工作
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.17.Final</version>
</dependency>
编辑1:
更改此
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CharacteristicsService {
private final GoodsRepository goodsRepository;
到
@Service
public class CharacteristicsService {
@AutoWired
private final GoodsRepository goodsRepository;
还有
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class})
@SpringBootApplication
public class OnlineshopApplication {
我不知道为什么您排除了@EnableAutoConfiguration,但是您可以尝试一下
答案 1 :(得分:0)
一切正常,谢谢大家的帮助。我通过删除@EnableAutoConfiguration异常解决了该问题。而且在OrdersRepository中有一个写得不正确的方法,需要分别从服务和控制器中删除该方法。
答案 2 :(得分:0)
伙计,看起来你缺少一些 spring 注释,在我的情况下,它是我的存储库类上的一个简单的 @Repository 标记...
我在这里找到了一些信息: https://www.baeldung.com/spring-unsatisfied-dependency
还有另一种情况,当你有一个以上的 bean 满足一个操作时.. 对于这种情况,您可以在您的应用程序类中添加此注释:
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.demo.yourpackage"}) //this line**
public class SpringDependenciesExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDependenciesExampleApplication.class, args);
}
}