简体   繁体   中英

Spring Boot Application failed to start because of missing bean

How I "solved" it: The initial problem was that I forgot to change the package name in the main class that launches the application:

@EnableJpaRepositories(basePackages = {"com.sda.VictorLiviu.ecommerce.jparepository"})

After that I just instanced itemMapper as a normal class in ItemService like this:

ItemMapper itemMapper = new ItemMapper();

and than used its toEntity method as:

itemMapper.toEntity();

. . . . . . . . . . . .

My Spring Boot app says it doesn't have a Bean class which it clearly has. I will post the Pom, service and repository. I want to point out that I have exactly the same class and repository for UserService and OrderService and they don't give any error, the only difference is the @Transactional and I removed it and got the same error.Any suggestions are welcomed!

The Error:

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 class

    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!"));
    }
}

Repository

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> {
}

Item Class

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 Class

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

You need to turn on jpa repositories processing.

To do this, annotate your application with @EnableJpaRepositories

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM