Reputation: 177
I keep geting NPE with Spring injecting Datasource in following code.
I got two classes. Superclasss
public class RepositorySource extends PropertiesConfiguration{
private RepositoryView repositoryView;
public RepositoryView getRepositoryView() {
return repositoryView;
}
public void setRepositoryView(RepositoryView repositoryView) {
this.repositoryView = repositoryView;
}
}
And subclass
public class RepositoryView {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void testConn_RV(
Connection con;
try {
con = dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here is the bean definition to inject the DataSource from it:
<bean id="RepositorySourceBean" class="com.acme.persistence.metamodel.views.impl.RepositorySource" >
<property name="repositoryView">
<bean class="com.acme.persistence.metamodel.views.impl.RepositoryView">
<property name="dataSource" ref="mysqlDataSource"> </property>
</bean>
</property>
</bean>
And the Datasource bean:
<!-- DATABASE PROPERTIES LOCALIZATION -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>properties/mysql-persistence.properties</value>
</property>
</bean>
<!-- DATASOURCE DEFINITION -->
<bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
I keep getting the NPE in last line of main method:
public static void main( String[] args )
{
App app = new App();
ApplicationContext appContext =
new ClassPathXmlApplicationContext("spring/configBeans/BeanLocations.xml");
RepositorySource src = new RepositorySource();
src.getRepositoryView().testConn_RV();} //<----- NPE HERE
It seems DataSource is not beeing initialized but why?
How to solve this?
EDIT This part:
RepositorySource src = new RepositorySource();
src.getRepositoryView().testConn_RV();}
needs to go in method of main class called say initResourceSource()
so i would have to pass appContext to the method so therefore getBean()
is no the solution
Upvotes: 0
Views: 805
Reputation: 905
Answer in continuation to comment... Use this:
private RepositorySource src;
public void setSrc(RepositorySource src){
this.src = src;
}
public static void main( String[] args )
{
App app = new App();
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/configBeans/BeanLocations.xml");
//RepositorySource src = new RepositorySource();
src.getRepositoryView().testConn_RV();
}
and inject this using XML
Upvotes: 0
Reputation: 905
It should be like this...
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/configBeans/BeanLocations.xml");
RepositorySource src = appContext.getBean("repositorySourceBean",RepositorySource.class);
Upvotes: 1