diff --git a/src/main/java/com/nurkiewicz/jdbcrepository/JdbcRepository.java b/src/main/java/com/nurkiewicz/jdbcrepository/JdbcRepository.java index 2c623d7..4128d9d 100644 --- a/src/main/java/com/nurkiewicz/jdbcrepository/JdbcRepository.java +++ b/src/main/java/com/nurkiewicz/jdbcrepository/JdbcRepository.java @@ -17,11 +17,8 @@ package com.nurkiewicz.jdbcrepository; import com.nurkiewicz.jdbcrepository.sql.SqlGenerator; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; @@ -52,30 +49,35 @@ * Implementation of {@link PagingAndSortingRepository} using {@link JdbcTemplate} */ public abstract class JdbcRepository, ID extends Serializable> - implements PagingAndSortingRepository, InitializingBean, BeanFactoryAware { + implements PagingAndSortingRepository, InitializingBean { private final TableDescription table; private final RowMapper rowMapper; private final RowUnmapper rowUnmapper; - private SqlGenerator sqlGenerator = new SqlGenerator(); - private BeanFactory beanFactory; + private SqlGenerator sqlGenerator; private JdbcOperations jdbcOperations; - public JdbcRepository(RowMapper rowMapper, RowUnmapper rowUnmapper, - SqlGenerator sqlGenerator, TableDescription table) { + public JdbcRepository(RowMapper rowMapper, RowUnmapper rowUnmapper, TableDescription table, + JdbcOperations jdbcOperations, SqlGenerator sqlGenerator) { Assert.notNull(rowMapper); Assert.notNull(rowUnmapper); Assert.notNull(table); + this.jdbcOperations = jdbcOperations; this.rowUnmapper = rowUnmapper; this.rowMapper = rowMapper; - this.sqlGenerator = sqlGenerator; + this.sqlGenerator = sqlGenerator != null ? sqlGenerator : new SqlGenerator(); this.table = table; } + public JdbcRepository(RowMapper rowMapper, RowUnmapper rowUnmapper, + SqlGenerator sqlGenerator, TableDescription table) { + this(rowMapper, rowUnmapper, table, null, sqlGenerator); + } + public JdbcRepository(RowMapper rowMapper, RowUnmapper rowUnmapper, TableDescription table) { this(rowMapper, rowUnmapper, null, table); } @@ -110,27 +112,22 @@ public static Object[] pk(Object... idValues) { } - @Override - public void afterPropertiesSet() throws Exception { - obtainJdbcTemplate(); - if (sqlGenerator == null) { - obtainSqlGenerator(); - } - } - - @Override - public void setBeanFactory(BeanFactory beanFactory) throws BeansException { - this.beanFactory = beanFactory; + public void afterPropertiesSet() { + Assert.notNull(jdbcOperations, "Property jdbcOperations is required"); + Assert.notNull(sqlGenerator, "Property sqlGenerator is required"); } + @Autowired(required = false) public void setSqlGenerator(SqlGenerator sqlGenerator) { this.sqlGenerator = sqlGenerator; } + @Autowired(required = false) public void setJdbcOperations(JdbcOperations jdbcOperations) { this.jdbcOperations = jdbcOperations; } + @Autowired(required = false) public void setDataSource(DataSource dataSource) { this.jdbcOperations = new JdbcTemplate(dataSource); } @@ -296,23 +293,6 @@ protected S postUpdate(S entity) { } - private void obtainSqlGenerator() { - try { - sqlGenerator = beanFactory.getBean(SqlGenerator.class); - } catch (NoSuchBeanDefinitionException e) { - sqlGenerator = new SqlGenerator(); - } - } - - private void obtainJdbcTemplate() { - try { - jdbcOperations = beanFactory.getBean(JdbcOperations.class); - } catch (NoSuchBeanDefinitionException e) { - DataSource dataSource = beanFactory.getBean(DataSource.class); - jdbcOperations = new JdbcTemplate(dataSource); - } - } - private S createWithManuallyAssignedKey(S entity, Map columns) { String createQuery = sqlGenerator.create(table, columns); Object[] queryParams = columns.values().toArray();