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); } 