簡體   English   中英

查詢bean為空,如何獲取它?

[英]Queries bean is null, how do I get it to populate?

因此,我試圖在此Java應用程序中運行sql查詢。 我想我已經正確設置了DAO,但是找不到包含查詢的XML文件。 我的DAO實現中有問題的代碼是:

private Properties queries;

public void setQueries(Properties queries) {
    this.queries = queries;
}
public Boolean checkAssigned(String Id) {
    String sql = queries.getProperty("CHECK_IF_ASSIGNED");

    Map<String,Object> params = new HashMap<>();
    List<String> assignedList;
    params.put(":Id",Id);

    LOG.info("Checking to see if already assigned \n" + "sql=" + sql
            + "\n" + "params=" + params);

    assignedList = getNamedParameterJdbcTemplate().query(sql,params,
            new assignedMapper());
    if (assignedList == null || assignedList.size() == 0) {
        ScreenVo.setSwitch(false);
    }
    else {
        ScreenVo.setSwitch(true);
    }
    return ScreenVo.getSwitch();
}

我的DAO就是:

public interface ScreenDao {
    Boolean checkAssigned(String Id);
}

我的query.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util.xsd">

    <util:properties id="queries">
        <prop key="CHECK_IF_ASSIGNED">
            <![CDATA[
                --Long query
            ]]>
        </prop>
    </util:properties>
</beans>

applicationContext.xml中用於dao的bean是:

<bean id="screenDaoImpl" class="com.corp.apps.actionator.dao.ScreenDaoImpl">
    <property name="dataSource" ref="datasource"/>
    <property name="queries" ref="queries"/>
</bean>

我在applicationContext中查詢文件的聲明是:

<import resource="classpath:queries.xml"/>

它以類似的方式在我的web.xml中聲明。

我試圖包括所有可能相關的內容。 我試過在ScreenDaoImpl.java中自動裝配Bean,但這沒有用。 我真的不確定從這里去哪里,或者我做錯了什么。

編輯:我得到的異常是:

javax.faces.event.MethodExpressionActionListener.processAction java.lang.NullPointerException

我的screenDaoImpl在使用前聲明為:

private static ScreenDao screenDao = new ScreenDaoImpl();

Spring-Bean screenDaoImpl必須通過Spring上下文創建,在這種情況下,Spring可以在創建的bean中注入所需的屬性( dataSourcequeries )。 我不知道您的應用程序架構。 但我可以為您提供幾種方法。

1-如果要在spring-bean中使用在spring-xml中聲明的screenDaoImpl,則可以這樣操作:

<bean id="screenServiceImpl" class="com.corp.apps.actionator.service.ScreenServiceImpl">
    <property name="screenDao" ref="screenDaoImpl"/>
</bean>

更好的方法是在Spring中創建所有應用程序。 並通過spring-context xml創建(並注入)bean。 不要通過new創建bean對象。 Spring無法在這些對象中注入屬性。

如果很難,請嘗試在Spring站點上找到應用程序示例。 也許嘗試spring-boot(不帶xml)。

2-如果要在非彈簧對象中使用screenDaoImpl,則可以通過“橋”從彈簧上下文中獲取screenDaoImpl。 創建課程:

package com.corp.apps.actionator.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class AppSpringBridge implements ApplicationContextAware {

  private static ApplicationContext context;

  public void setApplicationContext(ApplicationContext context) throws BeansException {
    this.context = context;
  }

  public static ApplicationContext getApplicationContext() {
    return context;
  }
}

在application-context.xml中定義bean:

<bean id="springBridge" class="com.corp.apps.actionator.util.AppSpringBridge />

Spring創建了這個bean,但是這個bean的方法getApplicationContext() (和context屬性)是靜態的。 我們可以在任何方法中使用getApplicationContext()

ScreenDao screenDao = (ScreenDao)AppSpringBridge.getApplicationContext().getBean("screenDaoImpl");

我已修復它,為了后代,我將在此處發布我的解決方案:

首先,我將我的screenDao bean自動連接到調用類中,然后創建一個靜態方法來設置screenDao。

@Autowired
private static ScreenDao screenDao;

@PostConstruct
public static void setScreenDao(ScreenDao newScreenDao) {
    screenDao = newScreenDao;
}

@PostConstruct
public ScreenDao getScreenDao() {
    return screenDao;
}

我不太確定getScreenDao是否可以執行任何操作,但是我也添加了它。

然后在我的應用程序上下文中,我創建了一個名為initialize的bean來調用靜態方法。

<bean id="initialize" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="com.corp.apps.consolidator.backing.ScreenBean"/>
    <property name="targetMethod" value="setScreenDao"/>
    <property name="arguments">
        <list>
            <ref bean="screenDao"/>
        </list>
    </property>
</bean>

這兩個更改解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM