0

次のコードで、Spring が Datasource を注入することで NPE を取得し続けます。

私は2つのクラスを取得しました。スーパークラス

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>

そして 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>

メインメソッドの最後の行で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();} 

sayというメインクラスのメソッドに入る必要がinitResourceSource()あるので、appContextをメソッドに渡す必要があるためgetBean()、解決策はありません

4

2 に答える 2

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

于 2013-08-06T14:53:03.667 に答える