【问题标题】:Spring 3: Datasource injection And NullPointerExceptionSpring 3:数据源注入和 NullPointerException
【发布时间】:2013-08-06 12:53:57
【问题描述】:

我在以下代码中不断通过 Spring 注入数据源获得 NPE。

我有两节课。超类

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

这是从中注入 DataSource 的 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>

我一直在 main 方法的最后一行得到 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() 不是解决方案

【问题讨论】:

  • 你还没有注入任何东西。
  • 如果你的意思是 getBean 那么getBean() 不是 DI。 HERE IS WHY

标签: spring dependency-injection nullpointerexception datasource


【解决方案1】:

应该是这样的……

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

【讨论】:

  • getBean() 不是 DI。你应该避免它。我在 Spring 的 DI 之后。请阅读我上面帖子中的链接。
  • 然后,您可以使用 XML 配置代替 getBean() 来注入 bean。但在这种情况下,你也必须声明你的类变量(RepositorySource)。
  • 那是我psoted的代码。由于 NPE,它无法正常工作。
  • 我提到了你上面提到的链接。仔细阅读,他是在说getBean() 不是IOC,但后来他说要使用基于dependency injectionXML
  • 我理解,但这正是我的示例中不起作用的内容。`ResourceSource1 不是应用程序主类。 DI 在这里不工作,为什么?
【解决方案2】:

回答继续评论... 使用这个:

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注入它

【讨论】:

  • 这似乎有点丑陋的解决方案。 RepositorySource 不应该是主应用程序的全局变量。这是解决方法,但不能解决问题。我不相信 constructor-arg 无法解决 ResourceSource 的问题,就像他们建议的 HERE
  • 实际上RepositorySource 将用于主应用程序的方法中。
猜你喜欢
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
相关资源
最近更新 更多