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