Skip to content

Commit

Permalink
Inject dependencies using Autowire instead of manually from BeanFactory
Browse files Browse the repository at this point in the history
Fixes nurkiewicz#25.

And also fixes
nurkiewicz#20;
if you want to inject a non-primary bean, then you can do the following:

    public class FooRepository extends JdbcRepository {

        @Autowired @qualifier("secondaryDataSource")
        public void setDataSource(DataSource dataSource) {
            super.setDataSource(dataSource);
        }
    }
  • Loading branch information
jirutka committed Feb 13, 2016
1 parent 1a1c6b4 commit e578205
Showing 1 changed file with 18 additions and 38 deletions.
56 changes: 18 additions & 38 deletions src/main/java/com/nurkiewicz/jdbcrepository/JdbcRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,30 +49,35 @@
* Implementation of {@link PagingAndSortingRepository} using {@link JdbcTemplate}
*/
public abstract class JdbcRepository<T extends Persistable<ID>, ID extends Serializable>
implements PagingAndSortingRepository<T, ID>, InitializingBean, BeanFactoryAware {
implements PagingAndSortingRepository<T, ID>, InitializingBean {

private final TableDescription table;

private final RowMapper<T> rowMapper;
private final RowUnmapper<T> rowUnmapper;

private SqlGenerator sqlGenerator = new SqlGenerator();
private BeanFactory beanFactory;
private SqlGenerator sqlGenerator;
private JdbcOperations jdbcOperations;


public JdbcRepository(RowMapper<T> rowMapper, RowUnmapper<T> rowUnmapper,
SqlGenerator sqlGenerator, TableDescription table) {
public JdbcRepository(RowMapper<T> rowMapper, RowUnmapper<T> 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<T> rowMapper, RowUnmapper<T> rowUnmapper,
SqlGenerator sqlGenerator, TableDescription table) {
this(rowMapper, rowUnmapper, table, null, sqlGenerator);
}

public JdbcRepository(RowMapper<T> rowMapper, RowUnmapper<T> rowUnmapper, TableDescription table) {
this(rowMapper, rowUnmapper, null, table);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -296,23 +293,6 @@ protected <S extends T> 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 extends T> S createWithManuallyAssignedKey(S entity, Map<String, Object> columns) {
String createQuery = sqlGenerator.create(table, columns);
Object[] queryParams = columns.values().toArray();
Expand Down

0 comments on commit e578205

Please sign in to comment.