I've been racking my brains for a week now, everything seems to be all right, but I don’t see something, please tell me what I haven’t

The exception itself:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'File.service.AddressService' available 

main:

 public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext("File.settings.ApplicationConfig.class"); AddressService repositoryAddress = applicationContext.getBean(AddressService.class); Address address = new Address(); address.setId(13L); address.setCity("Lviv"); address.setCountry("UKraine"); address.setStreet("Shevchenka 42A"); address.setPostCode("435433"); repositoryAddress.save(address); 

}

JavaConfig:

 @Configuration @EnableJpaRepositories("File.repository") @EnableTransactionManagement @ComponentScan("File") @PropertySource("classpath:db.properties") public class ApplicationConfig { @Resource private Environment env; @Bean public DataSource getDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(env.getRequiredProperty("db.driver")); dataSource.setUrl(env.getRequiredProperty("db.url")); dataSource.setUsername(env.getRequiredProperty("db.user_name")); dataSource.setPassword(env.getRequiredProperty("db.password")); return dataSource; } @Bean public LocalContainerEntityManagerFactoryBean getEntityManager() { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(getDataSource()); entityManagerFactoryBean.setPackagesToScan (env.getRequiredProperty("db.entity.pages")); entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); entityManagerFactoryBean.setJpaProperties(getProperties()); return entityManagerFactoryBean; } @Bean public JpaTransactionManager getJpaTransactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(getEntityManager().getObject()); return transactionManager; } public Properties getProperties() { Properties properties = new Properties(); InputStream inp = getClass().getClassLoader().getResourceAsStream("hibernate.properties"); try { properties.load(inp); } catch (IOException e) { System.out.println("Can't find hibernate.propertis in classpath!" + e); } return properties; } 

}

DataSource settings:

 db.driver = com.mysql.cj.jdbc.Driver db.url = jdbc:mysql://localhost:3306/public?useLegacyDatetimeCode=false&serverTimezone=UTC db.user_name = root db.password = 1111 db.entity.pages = File.Entity 

Hibernate settings:

 hibernate.dialect = org.hibernate.dialect.MySQL5Dialect hibernate.show_sql = true hibernate.hbm2ddl.auto = update 

JpaRepository:

 public interface RepositoryAddress extends JpaRepository<Address,Long> { } 

Service:

 @Service public class AddressService implements AddressDAO { @Autowired private RepositoryAddress repositoryAddress; @Override public void save(Address address) { repositoryAddress.saveAndFlush(address); } @Override public Address getById(Long id) { return repositoryAddress.findById(id).orElse(null); } @Override public List<Address> getAll() { return repositoryAddress.findAll(); } @Override public void update(Address address) { repositoryAddress.saveAndFlush(address); } @Override public void deleteById(Long id) { repositoryAddress.deleteById(id); } } 

AddressDAO:

 public interface AddressDAO { void save(Address address); Address getById(Long id); List<Address> getAll(); void update(Address address); void deleteById(Long id); } 

enter image description here

  • AddressService repositoryAddress = applicationContext.getBean (AddressService.class); Address address = applicationContext.getBean (Address.class); this is strange, because the Address class should not be a bin at all, and the AddressService clause implements AddressDAO, and it is not in your example at all. In addition, I would like to see the naming of packages in the classes. - Dmitry
  • @ Dmitry Alexandrovich, added AddressDAO at the end, and the screen for package names in classes, but with applicationContext.getBean (Address.class) really got it, fixed it to normal new Address (), but the exception crashes higher and there is no difference how I created a copy of Address (( - Konstantin Volosyan
  • hibernate.show_dialect = true - and this does not confuse you? moreover, your context is not clearly loaded from where: ApplicationContext applicationContext = new AnnotationConfigApplicationContext ("File.settings.ApplicationConfig"). this, apparently, should be a configuration file, but you obviously have a configuration using annotations. correct it, if it does not help, write - I will raise the project myself, such things are difficult to assess by sight. - Dmitriy
  • @ Dmitry Alexandrovich, I fixed it, but the result is still the same, moreover I tried to do the same thing but with the settings in the Spring xml container; I also tried with xml settings but without using an heir or CrudRepository itself, well I implemented dao methods using EntityManager methods. In general, I tried this way and that, but the result, namely, the exception is the same. The feeling that the error is very simple and common among these attempts, but I do not see ( - Konstantin Volosyan
  • fex.net/598160563374?fileId=1295643823 - link to file sharing. unpack the archive and run - Dmitriy

0