NoSuchBeanDefinitionException at least 1 bean which qualifies as autowire candidate for this dependency

蹲街弑〆低调 提交于 2019-12-04 10:41:24

The setter injection is not applied properly. In spring you cannot call the new operator. look into setter and constructor injection. Here is an example of setter injection...Try this:

@Repository
public class UserDaoImpl implements UserDao {

@Autowired
private JdbcTemplate template;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
      this.jdbcTemplate = jdbcTemplate;
}

@Override
public List<User> findAll(){
    return this.template.query(Sql.SQL_QUERY_FIND_ALL ,new UserRowMapper&lt;User&gt;());
}

@Override
public User fintById(long id){
    return this.template.queryForObject(Sql.SQL_QUERY_FIND_BY_ID,  new Object[]{id}, new UserRowMapper&lt;User&gt;());

}

@Override
public void create(final User user){
  ...
}

@Override
public void delete(User user){
    this.template.update(Sql.SQL_QUERY_DELETE, new Object[]{ user.getId() });
}

DBContext:

<jdbc:embedded-database id="dataSource" type="DERBY">
    <jdbc:script location="classpath:create-schema.sql"/>
</jdbc:embedded-database>

 <!--JDBC Template Bean...-->
<bean id="reportJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>

For repository/DAO classes use @Respository annotation, rather than @Component. Ensure that the base scan is enabled for the DAO classes so Spring can find and inject them. You don't have to explicit define the bean to be injected in the xml file with @Service, @Repository and @Controller annotations.

<context:component-scan base-package="com.something.dao"/>

Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!