0

В проекте со структурой
Структура проекта.

TeamsController

@RestController
public class TeamsController {

    @Autowired
    private TeamsService teamsService;
...
}

Application

@SpringBootApplication
@ComponentScan(basePackages = {"com.test.api","com.test.service"})
    public class Application {
        public static void main(String[] args) throws Exception{
            SpringApplication.run(new Class<?>[] {Application.class, JpaConfig.class}, args);
        }
    }

JpaConfig

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.test.service.repository")
public class JpaConfig implements TransactionManagementConfigurer {

    ...

    @Bean
    public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(configureDataSource());
        entityManagerFactoryBean.setPackagesToScan("com.test.model");
...}
...
}

TeamsRepository

@Repository
public interface TeamsRepository extends JpaRepository<TeamsEntity, Long> {
}

TeamsService

public interface TeamsService {
    Collection<TeamsEntity> findAll();

    TeamsEntity findOne(Long id);

    TeamsEntity create(TeamsEntity teamsEntity);

    TeamsEntity update(TeamsEntity teamsEntity);

    void delete(Long id);
}

TeamsServiceImpl

@Service
@Transactional(
        propagation = Propagation.SUPPORTS,
        readOnly = true)
public class TeamsServiceImpl implements TeamsService {

    TeamsServiceImpl(){

    }

   @Autowired
    private TeamsRepository teamsRepository;
...

Выдает такую ошибку:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.service.TeamsService com.test.api.TeamsController.teamsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamsServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.service.repository.TeamsRepository com.test.service.TeamsServiceImpl.teamsRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamsRepository': Cannot create inner bean '(inner bean)#1b7a52dd' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1b7a52dd': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

0

1 ответ 1

0

Как оказалось, проблема была ни в способах инжекта Repository в пользовательский DAO и ни в том что при конфигурации этого добра были указаны не те пакеты, а в наименовании самой фабрики - поскольку Spring Data JPA ищет EntityManagerFactory с именем экземпляра entityManagerFactory; это видно в документации.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

Соответственно необходимо либо заменить название метода либо указать спрингу своё название, как было предложено тут.
P.S.: В следущий раз лог буду читать с конца ¯\_(ツ)_/¯

2
  • Или можно в сети поискать. Этот линк - первый в выдаче гугла.
    – Maksim
    23 ноя 2016 в 10:11
  • хорошо когда знаешь, что искать. 23 ноя 2016 в 17:08

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.