i’m new to spring boot so i got this error.
Description:
Field productService in com.itac.main.DataApplication required a bean of type ‘com.itac.service.ProductService’ 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.itac.service.ProductService’ in your configuration.
it’s ProductService
package com.itac.service;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itac.model.Product;
import com.itac.repository.ProductRepository;
@Service
public class ProductService{
@Autowired
private ProductRepository productRepository;
public Iterable<Product> getProducts() {
return productRepository.findAll();
}
public Optional<Product> getProductById(Integer id) {
return productRepository.findById(id);
}
}
it’s dataapplication
package com.itac.main;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.itac.model.Product;
import com.itac.service.ProductService;
@SpringBootApplication
public class DataApplication implements CommandLineRunner {
@Autowired
private ProductService productService;
public static void main(String[] args) {
SpringApplication.run(DataApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Iterable<Product> products = productService.getProducts();
products.forEach(product -> System.out.println(product.getName()));
}
}
it’s ProducductRepository interface
package com.itac.repository;
import org.springframework.data.repository.CrudRepository;
import com.itac.model.Product;
public interface ProductRepository extends CrudRepository<Product, Integer> {
}
and application.properties
spring.application.name=data
spring.datasource.url=jdbc:mysql://localhost:3306/library
spring.datasource.username=root
spring.datasource.password=
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
#Tomcat configuration
server.port=9001
#Log level configuration
logging.level.root=ERROR
logging.level.com.openclassrooms=INFO
logging.level.org.springframework.boot.web.embedded.tomcat=INFO
i added @service and @Autowired but nothing
Jeff Theirson’s Voltaire is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.