0

我一直在用以下代码注入数据源的Spring中创建NPE。Spring 3:数据源注入和NullPointerException

我有两个班。 Superclasss

public class RepositorySource extends PropertiesConfiguration{ 

    private RepositoryView repositoryView; 

    public RepositoryView getRepositoryView() { 
     return repositoryView; 
    } 
    public void setRepositoryView(RepositoryView repositoryView) { 
     this.repositoryView = repositoryView; 
    }    
} 

和子

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(); 
     } 
    } 
} 

这里是bean定义从中注入数据源:

<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> 

与数据源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> 

我一直获得NPE主要方法的最后一行:

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 

看来DataSource不是初始化的,但为什么?

如何解决这个问题?

编辑 这一部分:

RepositorySource src = new RepositorySource(); 
     src.getRepositoryView().testConn_RV();} 

需要去被称为主类的方法说initResourceSource(),所以我将不得不appContext传递给方法,以便因此getBean()没有解决

+1

您还没有任何注射注入这一点。 –

+0

如果你的意思是getBean,那么getBean()不是DI。 [这是为什么](http://stackoverflow.com/questions/812415/why-is-springs-applicationcontext-getbean-considered-bad) – mCs

回答

1

它应该是这样的...

ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/configBeans/BeanLocations.xml"); 
RepositorySource src = appContext.getBean("repositorySourceBean",RepositorySource.class); 
+0

'getBean()'不是DI。你应该避免它。我是在DI和Spring之后。阅读我上面的帖子plz的链接。 – mCs

+0

然后,可以使用XML配置来注入bean,而不是'getBean()'。但是在那种情况下,你必须声明你的类变量('RepositorySource')。 – user2550754

+0

这就是我所说的代码。由于NPE,它不工作。 – mCs

0

答案在延续评论... 使用此:

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(); 
} 

,并使用XML

+0

这似乎有点丑陋的解决方案。 'RepositorySource'不应该是主应用程序的全局变量。这是解决方法,但它不能解决问题。我不相信这不能通过constructor-arg来解决'ResourceSource',就像他们所建议的[这里](http://stackoverflow.com/a/14656434) – mCs

+0

实际上'RepositorySource'将被用在主应用程序的方法。 – mCs