0

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

mCs
  • 167
  • 1
  • 4
  • 16

2 Answers2

1

It should be like this...

 ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/configBeans/BeanLocations.xml");
 RepositorySource src = appContext.getBean("repositorySourceBean",RepositorySource.class);  
user2550754
  • 884
  • 8
  • 15
  • `getBean()` is NOT DI. You should avoid it. I am after DI with Spring. Read the link from my above post plz. – mCs Aug 06 '13 at 13:45
  • Then, instead of `getBean()` you can use XML configuration for injecting bean. But in that case also, you have to declare your class variable(`RepositorySource`). – user2550754 Aug 06 '13 at 13:47
  • That is the code I have psoted. It is not working due to NPE. – mCs Aug 06 '13 at 13:49
  • I referred to the link, you mentioned above. Read carefully, he is saying that `getBean()` is not `IOC`, but then he is telling to use `XML` based `dependency injection`. – user2550754 Aug 06 '13 at 13:52
  • I understand but that is exactly what is in my example that is not working.The `ResourceSource1 is not app main class. The DI is not working here, why? – mCs Aug 06 '13 at 14:13
  • Because, you are using `RepositorySource src = new RepositorySource();` which creates a new object. Remove this statement. – user2550754 Aug 06 '13 at 14:15
  • But the `ResourceSource` is not my main app class. And more `getBean()` would have to be stated here. So shall I create a `App`bean which would contain all the beans and then start only ONE `getBean("App")`? – mCs Aug 06 '13 at 14:19
  • No you can `autowiring` or `xml` based `DI`. – user2550754 Aug 06 '13 at 14:24
  • It is not answering to the problem. It just suggest an answer that force to use `getBean()` which is not `DI` I am doing it with XML (not autowire) due to `ref=beanName` argument of properties. So what are you trying to say? – mCs Aug 06 '13 at 14:41
0

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

user2550754
  • 884
  • 8
  • 15
  • This seems bit ugly solution. The `RepositorySource` is not supposed to be a global variable for the main app. This is workaround but it is not solving the problem. I dont believe this can not be solved by `constructor-arg` for `ResourceSource` like they suggest [HERE](http://stackoverflow.com/a/14656434) – mCs Aug 06 '13 at 15:25
  • Actually the `RepositorySource` is going to be used in a method of a main app. – mCs Aug 06 '13 at 15:33