After starting the application faylitsya. I can not understand what the error is. I moved the Internet, but something has not yet found the answer. Here is a description:

Description: Field userRepository in com.pro100denysko.app.resttest.controller.UserController required a bean of type 'com.pro100denysko.app.resttest.service.UserRepository' that could not be found. Action: Consider defining a bean of type 'com.pro100denysko.app.resttest.service.UserRepository' in your configuration. 

Here is the controller:

 package com.pro100denysko.app.resttest.controller; import com.pro100denysko.app.resttest.model.User; import com.pro100denysko.app.resttest.service.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/users") public class UserController { @Autowired UserRepository userRepository; @RequestMapping(value = "/listOfUsers", method = RequestMethod.GET) @ResponseBody public List<User> findAll() { return userRepository.findAll(); } } 

Here is the model:

 package com.pro100denysko.app.resttest.model; import lombok.Data; import javax.persistence.*; @Entity @Table(name = "users") public @Data class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") private int id; @Column(name = "NAME") private String name; @Column(name = "LASTNAME") private String lastName; @Column(name = "AGE") private int age; } 

Here is the repository service:

 package com.pro100denysko.app.resttest.service; import com.pro100denysko.app.resttest.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository("UserRepository") public interface UserRepository extends JpaRepository<User, Integer> { } 

Well, Maine himself:

 package com.pro100denysko.app.resttest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = {"com.pro100denysko.app.resttest"}) @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class}) public class RestTestApplication { public static void main(String[] args) { SpringApplication.run(RestTestApplication.class, args); } } 
  • Try to RestTestApplication @EnableJpaRepositories on @EnableJpaRepositories(basePackages = {"com.pro100denysko.app.resttest"}) - Nofate
  • The @Nofate error has changed to Consider defining a bean named 'entityManagerFactory' in your configuration . - Pro100Denysko
  • Well, it's natural. Need an EMF bin. And all of you have disconnected it in EnableAutoConfiguration. Where should Spring save users? - Nofate
  • @Nofate I have a user database. The application should take them from the database and display them on the screen. - Pro100Denysko
  • But you have disabled JPA, DataSource, and Hibernate auto-configuration. - Nofate

1 answer 1

Since you have disabled the Autoconfiguration of the DataSource with the Spring Boot tools, according to the documentation for working with the Spring Data repositories, you need to configure JPA yourself:

 @Configuration @EnableJpaRepositories(basePackages = {"com.pro100denysko.app.resttest"}) @EnableTransactionManagement public class RestTestApplication { @Bean public DataSource dataSource() return // тут DataSource к вашей БД } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("com.pro100denysko.app.resttest.model"); factory.setDataSource(dataSource()); return factory; } @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory()); return txManager; } }