spring jdbcdaosupport returns null

762 views Asked by At

I have searched in every corner of the web with no result. I'm using Spring and trying to inject Jdbc operations into my DaoImpl class.

Seems like I am missing something as the DaoImpl class is returning null when querying for something, what am I doing wrong?

Bean configurations:

 <bean id="transactionDao" class="jdbc.dao.OracleTransactionDao">
            <property name="jdbcTemplate" ref="jdbcTemplate" />
        </bean>

        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource" />
        </bean>


        <!-- ORACLE CONNECTION -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="${url}" />
            <property name="username" value="${dbname}" />
            <property name="password" value="${dbpassword}" />
        </bean>

Interface dao:

interface TransactionDao extends Serializable {
    public abstract void insert (Transaction transaction);

As you can see in the bean config, and in this class below, I'm currently both trying to inject the JdbcTemplate and trying to use the extended methods of JdbcDaoSupport Implementation dao:

OracleTransactionDao extends JdbcDaoSupport implements
TransactionDao {
    @Autowired
    JdbcTemplate              template;

    @Override
    public void insert (Transaction transaction) {
        List<Map<String, Object>> list;
        try{
            list = template
                    .queryForList("SELECT * FROM CUSTOMER");
        }catch(NullPointerException e) {
            list = this.queryForList("SELECT * FROM CUSTOMER");
        }
        System.out.println("called insert" + list.size());

    }

My main: ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "beanConfigs.xml"); context.registerShutdownHook(); OracleTransactionDao dao = (OracleTransactionDao) context .getBean("transactionDao");

I must be missing some detail here?

Thanks in advance!

2

There are 2 answers

3
rgrebski On

Unit test i mentioned in a comment. spring-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">


  <bean id="transactionDao" class="jdbc.dao.TransactionDao">
    <property name="dataSource" ref="dataSource"/>
  </bean>

  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
  </bean>

  <jdbc:embedded-database id="dataSource" type="H2" />

</beans>

TransactionDao:

public class TransactionDao extends JdbcDaoSupport {
    @Autowired
    private JdbcTemplate template;

    public List<Map<String, Object>> insert() {
        String sql = "CREATE TABLE REGISTRATION " +
                "(id INTEGER not NULL, " +
                " first VARCHAR(255), " +
                " last VARCHAR(255), " +
                " age INTEGER, " +
                " PRIMARY KEY ( id ))";

        template.execute(sql);
        return template.queryForList("select * from registration");
    }
}

TransactionDaoTest:

@ContextConfiguration(locations = {"classpath*:spring-context.xml"})
public class TransactionDaoTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private TransactionDao transactionDao;

    @Test
    public void testJdbcTemplateNotNull(){
        Assertions.assertThat(transactionDao.insert()).isNotNull();
    }
}

pom.xml (some deps are not needed):

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-framework-bom</artifactId>
        <version>4.1.6.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.3</version>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.4</version>
    </dependency>

    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>18.0</version>
    </dependency>

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.9.4</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>3.0.0</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>


    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.4.187</version>
      <scope>test</scope>
    </dependency>



  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
0
MRK187 On

Basically came around this issue by injecting the context to the OracleTransactionDao

OracleTransactionDao implements TransactionDao,
          ApplicationContextAware {
    private static JdbcOperations jdbc;

    @Override
    public void setApplicationContext (ApplicationContext context) {
        this.jdbc = context.getBean(JdbcOperations.class);
    }

all I had to add in the beanConfig

<bean id="transactionDao" class="jdbc.dao.OracleTransactionDao">
</bean>

If anyone has a better looking solution please advise