我有一个Roles pojo类,它映射到数据库中的Roles表。 然后我有RolesDao及其实现RolesDaoImpl。 我在main()方法中使用以下代码调用RolesDao:-
RolesDao rolesDao = context.getBean( RolesDao.class);
rolesDao.getRolesList();
但是我得到以下错误:-
Exception in thread "main" java.lang.NullPointerException
at com.rahul.spring.dao.RolesDaoImpl.getRolesList(RolesDaoImpl.java:35)
我该如何解决?
我的RolesDaoImpl在下面:-
@Component
public class RolesDaoImpl extends BaseDaoImpl implements RolesDao {
@Autowired
private OracleConfiguration dataSource;
public List<Roles> getRolesList() {
String sql = "SELECT * FROM ROLES";
List<Roles> customers = getJdbcTemplate().query(sql,
new BeanPropertyRowMapper<Roles>(Roles.class));
return customers;
}
我的BaseDaoImpl在下面
@Component
public class BaseDaoImpl {
private JdbcTemplate jdbcTemplate; //getters and setters omitted for brevity
private String packageName;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setDataSource ( DataSource dataSource )
{
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
我的OracleConfiguration类是
@Configuration
//@ConfigurationProperties("oracle")
@PropertySource("classpath:dev.properties")
public class OracleConfiguration {
@Value( "${oracle.username}" )
private String username;// we can remove value injection and just use
@Value( "${oracle.password}" )
private String password;
@Value( "${oracle.url}" )
private String url;
@Value( "${oracle.driverType}" )
private String driverType;
@Bean
DataSource dataSource() throws SQLException {
OracleDataSource dataSource = new OracleDataSource();
dataSource.setDriverType(driverType);
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setURL(url);
dataSource.setImplicitCachingEnabled(true);
dataSource.setFastConnectionFailoverEnabled(true);
return dataSource;
}
我能够从dev.properties文件中打印/获取值。