简体   繁体   中英

JdbcTemplate is null in Springboot application using Oracle 10g

Using spring-boot 2.7.2 Error - java.lang.NullPointerException: Cannot invoke "org.springframework.jdbc.core.JdbcTemplate.query(String, org.springframework.jdbc.core.RowMapper)" because "this.jdbcTemplate" is null


@Service
public class UserServiceImpl implements UserService {
.....
    @Autowired
    private UserDao userDao;

}

@Component
public class UserDaoImpl implements UserDao {
    
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public UserDaoImpl(JdbcTemplate jdbcTemplatn) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    @Override
    public  List<AttributeData> getAttributes() {
        String query = "select attributeid, ....";
        List<AttributeData> attributes = jdbcTemplate.query(query, new AttributeMapper());
        return attributes;
    }
}

application.properties :
# Oracle db related
spring.datasource.url=jdbc:oracle:thin:@localhost....
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

# JPA related
oracle.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

Just means that you don't have a @Bean defining the JdbcTemplate, eg

@Bean
public JdbcTemplate jdbcTemplate() throws SQLException {
    final JdbcTemplate template = new JdbcTemplate();
    template.setDataSource(dataSource());
    template.afterPropertiesSet();
    return template;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM