В настоящее время я сталкиваюсь с этой проблемой в Spring Boot, когда речь идет о подключении интерфейса репозитория к классу обслуживания. вот мой пакет класса Item com.ensa.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Item {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private float prix;
public Item(Long id,String name,float prix) {
this.id=id;
this.name=name;
this.prix=prix;
}
public Item() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrix() {
return prix;
}
public void setPrix(float prix) {
this.prix = prix;
}}
вот мой интерфейс хранилища
package com.ensa.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.ensa.entity.Item;
@Repository
@Component
public interface ItemRepository extends JpaRepository<Item, Long> {
}
мой класс обслуживания:
package com.ensa.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ensa.entity.Item;
import com.ensa.repository.ItemRepository;
@Service
@Transactional
public class ItemService {
@Autowired
private ItemRepository itemRepo ;
private List<Item> list = new ArrayList<Item>();
public ItemService() {
Item A= new Item(1L,"AAAA",2);
list.add(A);
//itemRepo.count();
}
public List<Item> getAllItems() { //Returning all Items
return list;
}
public Item getItemById(Long id) { //getting Item by Id
return null;
}
public void addItem(Item item) { //adding new item
list.add(item);
}
public void updateItem(Long id, Item item) { //updating an existing Item
itemRepo.save(item);
}
public void deleteItem(Long id) { //deleting Item by id
for (Item temp : list) {
if (temp.getId() == id) {
int i = list.indexOf(temp);
list.remove(i);
return;
}
}
}
}
, с которым я работал список для тестирования API REST, но как только я добавил репозиторий, я начал получать эту ошибку
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2020-03-05 10:19:41.426 INFO 468 --- [ main] com.ensa.app.SpringApp : Starting SpringApp on DESKTOP-FPSG9AG with PID 468 (C:\Users\perso\eclipse-workspace\springTest\target\classes started by perso in C:\Users\perso\eclipse-workspace\springTest)
2020-03-05 10:19:41.430 INFO 468 --- [ main] com.ensa.app.SpringApp : No active profile set, falling back to default profiles: default
2020-03-05 10:19:42.226 INFO 468 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-03-05 10:19:42.252 INFO 468 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17ms. Found 0 repository interfaces.
2020-03-05 10:19:42.680 INFO 468 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d295b9fe] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-05 10:19:43.105 INFO 468 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-03-05 10:19:43.152 INFO 468 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-05 10:19:43.153 INFO 468 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2020-03-05 10:19:43.161 INFO 468 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk-13.0.1\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk-13.0.1/bin/server;C:/Program Files/Java/jdk-13.0.1/bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;c:\Program Files\Microsoft SQL Server\110\Tools\Binn\;c:\Program Files\Microsoft SQL Server\110\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;c:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\Java\jdk-13.0.1\bin;C:\Program Files\apache-maven-3.6.3\bin;;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.3\bin;;C:\Users\perso\OneDrive\Bureau;;.]
2020-03-05 10:19:43.352 INFO 468 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-05 10:19:43.352 INFO 468 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1873 ms
2020-03-05 10:19:43.563 INFO 468 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-03-05 10:19:43.705 INFO 468 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-03-05 10:19:43.765 INFO 468 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2020-03-05 10:19:43.849 INFO 468 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.7.Final}
2020-03-05 10:19:43.851 INFO 468 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2020-03-05 10:19:44.022 INFO 468 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2020-03-05 10:19:44.190 INFO 468 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-03-05 10:19:44.538 INFO 468 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-03-05 10:19:44.584 WARN 468 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemController': Unsatisfied dependency expressed through field 'itemServ'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemService': Unsatisfied dependency expressed through field 'itemRepo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ensa.repository.ItemRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-03-05 10:19:44.584 INFO 468 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-03-05 10:19:44.589 INFO 468 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-03-05 10:19:44.602 INFO 468 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2020-03-05 10:19:44.603 INFO 468 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-03-05 10:19:44.619 INFO 468 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-05 10:19:44.781 ERROR 468 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field itemRepo in com.ensa.service.ItemService required a bean of type 'com.ensa.repository.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.ensa.repository.ItemRepository' in your configuration.
**
Есть идеи о том, как решить эту проблему?