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