简体   繁体   English

查询bean为空,如何获取它?

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

so I'm trying to run a sql query within this java app. 因此,我试图在此Java应用程序中运行sql查询。 I think I have the DAO set up correctly but it can't find the XML file which contains my queries. 我想我已经正确设置了DAO,但是找不到包含查询的XML文件。 The code in question for my DAO implementation is: 我的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();
}

My DAO is just: 我的DAO就是:

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

My queries.xml file looks like: 我的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>

The bean for the dao in the applicationContext.xml is: 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>

And my declaration of the queries file in the applicationContext is: 我在applicationContext中查询文件的声明是:

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

It's declared in my web.xml in a similar fashion. 它以类似的方式在我的web.xml中声明。

I tried to include everything that could possibly be relevant. 我试图包括所有可能相关的内容。 I've tried autowiring the bean in ScreenDaoImpl.java but that didn't work. 我试过在ScreenDaoImpl.java中自动装配Bean,但这没有用。 I'm really not sure where to go from here, or what I might have done wrong. 我真的不确定从这里去哪里,或者我做错了什么。

EDIT: The exception I'm getting is: 编辑:我得到的异常是:

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

And my screenDaoImpl is declared before use as: 我的screenDaoImpl在使用前声明为:

private static ScreenDao screenDao = new ScreenDaoImpl();

Spring-Bean screenDaoImpl must be created through Spring context, in this case Spring can inject required properties ( dataSource and queries ) in created bean. Spring-Bean screenDaoImpl必须通过Spring上下文创建,在这种情况下,Spring可以在创建的bean中注入所需的属性( dataSourcequeries )。 I don't know your architecture of application. 我不知道您的应用程序架构。 But I can offer you a couple of ways. 但我可以为您提供几种方法。

1 - If you want use screenDaoImpl in spring-bean which declared in spring-xml then you can do it like this: 1-如果要在spring-bean中使用在spring-xml中声明的screenDaoImpl,则可以这样操作:

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

The better way is make all your application in Spring. 更好的方法是在Spring中创建所有应用程序。 And create (and inject) beans by spring-context xml. 并通过spring-context xml创建(并注入)bean。 Do not create bean-objects by new . 不要通过new创建bean对象。 Spring can not inject properties in these objects. Spring无法在这些对象中注入属性。

If it is difficult then try to find examples of applications on the Spring site. 如果很难,请尝试在Spring站点上找到应用程序示例。 Maybe try spring-boot (without xml). 也许尝试spring-boot(不带xml)。

2 - If you want use screenDaoImpl in non-spring object you can get screenDaoImpl from spring-context by "bridge". 2-如果要在非弹簧对象中使用screenDaoImpl,则可以通过“桥”从弹簧上下文中获取screenDaoImpl。 Create class: 创建课程:

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

Define bean in application-context.xml: 在application-context.xml中定义bean:

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

Spring create this bean, but method getApplicationContext() (and context property) of this bean is static. Spring创建了这个bean,但是这个bean的方法getApplicationContext() (和context属性)是静态的。 And we can use getApplicationContext() in any methods: 我们可以在任何方法中使用getApplicationContext()

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

I fixed it, and for posterity's sake I'll post my solution here: 我已修复它,为了后代,我将在此处发布我的解决方案:

First I autowired my screenDao bean in the invoking class, and then I created a static method to set screenDao. 首先,我将我的screenDao bean自动连接到调用类中,然后创建一个静态方法来设置screenDao。

@Autowired
private static ScreenDao screenDao;

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

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

I'm not really sure if getScreenDao does anything but I added it as well. 我不太确定getScreenDao是否可以执行任何操作,但是我也添加了它。

Then in my application context I created a bean I called initialize to invoke the static method. 然后在我的应用程序上下文中,我创建了一个名为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>

These two changes resolved my issue. 这两个更改解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM