没有使用spring注释获取jdbcTemplate的实例

时间:2013-09-30 19:43:40

标签: spring spring-annotations

我正在使用Spring jdbcTemplate进行数据库调用,但我没有获得jdbcTemplate实例,尽管我已经自动装配了它。

package com.mypackage.dao
@Repository
public class CustomerDaoImpl implements CustomerDao {

private JdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public Customer findCustomer(){
      ........... 
    jdbcTemplate.execute ......
      ...........
  }
}

在Spring applicationContext.xml

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
    <property name="initialSize" value="1" />
    <property name="maxActive" value="3" />
</bean>

 <context:component-scan base-package="com.mypackage">
    <context:exclude-filter expression="org.springframework.stereotype.Controller"
        type="annotation" />
</context:component-scan>

database.properties文件

database.url=jdbc:oracle:thin:@myhost:1521:OQA1
database.username=my_app
database.password=my_app_password
database.driverClassName=oracle.jdbc.OracleDriver

在哪个方面我犯了错误?

----------------编辑:添加上层----------------

 package com.mypackage.rest

  @Service("customerResource")
  @Path("/customer")
  public class CustomerResource extends AbstractResource{

  @Autowired
  private CustomerDao customerDao;

   }

1 个答案:

答案 0 :(得分:0)

驱动程序类错误应该是

database.driverClassName=oracle.jdbc.driver.OracleDriver