有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java查询bean为空,如何让它填充?

所以我试图在这个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();
}

我的刀只是:

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

我的问题。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>

应用程序上下文中dao的bean。xml是:

<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"/>

它在我的网站上声明。xml也是如此

我试着把所有可能相关的东西都包括进去。我尝试过在ScreenDaoImpl中自动连接bean。但那没用。我真的不知道接下来该怎么办,或者我可能做错了什么

编辑: 我得到的例外是:

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

我的screenDaoImpl在使用前声明为:

private static ScreenDao screenDao = new ScreenDaoImpl();

共 (2) 个答案

  1. # 1 楼答案

    我修正了它,为了子孙后代,我将在这里发布我的解决方案:

    首先,我在调用类中自动连接了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>
    

    这两个变化解决了我的问题

  2. # 2 楼答案

    Springbean 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上下文xml创建(并注入)bean。不要通过new创建bean对象。Spring无法在这些对象中注入属性

    如果有困难,那么尝试在Spring网站上找到应用程序的示例。也许可以试试spring boot(不含xml)

    2-如果你想在非spring对象中使用screenDaoImpl,你可以通过“桥”从spring上下文中获取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;
      }
    }
    

    在应用程序上下文中定义bean。xml:

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

    Spring创建这个bean,但是这个bean的方法getApplicationContext()(和context属性)是静态的。我们可以在任何方法中使用getApplicationContext()

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