【问题标题】:Spring 3.2.9; Tiles 2.0.7; Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No DataSource specified春天 3.2.9;瓷砖 2.0.7;调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: No DataSource specified
【发布时间】:2015-06-21 16:35:46
【问题描述】:

我正在尝试将我们的 Spring 项目从 2.5.6 更新到 3.2.9。我们正在使用 Tiles 2.0.7 和 Spring Web Flow 2.3。我遇到了在 Stack Overflow 或其他论坛中没有相关解决方案的错误: (部分堆栈跟踪):

####<Apr 13, 2015 4:11:53 PM GMT-06:00> <Warning> <HTTP> <xxxxxxxxxx.xxx.xxx.xxx.com> <managedserver1> <[ACTIVE] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1428963113472> <BEA-101162> <User defined listener org.springframework.web.context.ContextLoaderListener failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lkTimeZones' defined in ServletContext resource [/WEB-INF/webObjectBeans.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No DataSource specified.  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lkTimeZones' defined in ServletContext resource [/WEB-INF/webObjectBeans.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No DataSource specified at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)

我的 applicationContext.vxml:

<?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:p="http://www.springframework.org/schema/p"                           xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean name="jndiName" class="java.lang.String">
    <constructor-arg value="wevsplatds"/>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiEnvironment">
        <props>
            <prop key="java.naming.factory.initial">
                weblogic.jndi.WLInitialContextFactory
            </prop>
        </props>
    </property>
    <property name="jndiName" ref="jndiName"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg><ref bean="dataSource"/></constructor-arg>
</bean>

这是我的 webObjectBeans.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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<!-- Formatting Beans -->
<bean name="loc" class="java.util.Locale">
    <constructor-arg index="0" type="java.lang.String"
                     value="${languageId}"/>
    <constructor-arg index="1" type="java.lang.String"
                     value="${countryId}"/>
</bean>
<bean name="dateFormatSymbols" class="java.text.DateFormatSymbols">
    <constructor-arg type="java.util.Locale" ref="loc"/>
</bean>
<bean name="calendarTagDateFormat" class="java.text.SimpleDateFormat">
    <constructor-arg value="MM-dd-yyyy"/>
</bean>
<bean name="monthlyLinksDateFormat" class="java.text.SimpleDateFormat">
    <constructor-arg value="MMMM yyyy"/>
</bean>
<bean name="weeklyLinksDateFormat" class="java.text.SimpleDateFormat">
    <constructor-arg value="MMM d"/>
</bean>


<!-- System Beans -->
<bean id="cmAlarms" 
      class="com.fruit.apples.alarm.Alarm"
      p:errorCode="CALLMGRWEB001"/>

<!-- Collections Beans -->  
<bean id="lkTimeZones" class="com.fruit.apples.customer.webdisplay.collections.LkTimeZones"
      init-method="loadActiveTimeZones"
      p:timeZonesDao-ref="lkTimeZonesDAO" />
<bean id="lkHelps"
      class="com.fruit.apples.customer.webdisplay.collections.LkHelps"
      init-method="loadActiveHelp" 
      p:lkHelpDao-ref="lkHelpDAO" />
<bean id="lkActivityAreas"
      class="com.fruit.apples.customer.webdisplay.collections.LkActivityAreas"
      init-method="loadActiveActivityArea"
      p:lkActivityAreaDAO-ref="lkActivityAreaDAO" />

最后是 LkTimezonesDAO.java 的一部分:

package com.fruit.apples.customer.dao;

import com.fruit.apples.customer.webdisplay.LkTimeZone;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.dao.EmptyResultDataAccessException;

public class LkTimezonesDAO extends CustomerWebDaoImpl
   {
private static final long serialVersionUID = 2367566965661648309L;

/**
 * getAllLkTimeZones retrieves all data for LkTimeZoneObjects. The data
 * populates the List Object passed to this method with LkTimeZone objects
 * as appropriate for the data retrieved
 *
 * @param  listToLoad  the List Object to be populated
 */
public void getAllLkTimeZones( List<LkTimeZone> listToLoad )
{
    listToLoad.clear();
    JdbcTemplate jcTemplate = new JdbcTemplate();
    List<Map<String, Object>> rows = jcTemplate.queryForList(queries.get( "allLkTimezonesQuery" ) );

    if ( (rows!=null) || (0 < rows.size() ) )
    {
        for ( Map<String,Object> row : rows )
        {
            listToLoad.add( initializeLkTimeZone( row ) );
        }
    }
}

/**
 * getAllLkTimeZones retrieves only active data for LkTimeZoneObjects. The
 * data populates the List Object passed to this method with LkTimeZone
 * objects as appropriate for the data retrieved
 *
 * @param  listToLoad  the List Object to be populated
 */
public void getActiveLkTimeZones( List<LkTimeZone> listToLoad )
{
    listToLoad.clear();
    JdbcTemplate jcTemplate = new JdbcTemplate();

    List<Map<String, Object>> rows = jcTemplate.queryForList(queries.get( "activeLkTimezonesQuery" ) );

    if ( ( rows != null ) || ( 0 < rows.size() ) )
    {
        for ( Map<String,Object> row : rows )
        {
            listToLoad.add( initializeLkTimeZone( row ) );
        }
    }
}

【问题讨论】:

    标签: java spring spring-mvc spring-webflow-2


    【解决方案1】:

    LkTimezonesDAO

    JdbcTemplate jcTemplate = new JdbcTemplate();
    

    您正在使用new 关键字自己实例化jdbcTemplate,它如何知道dataSource

    您应该通过@Autowiredxml 注入在上下文中定义的那个。

    【讨论】:

    • 好的,我注入了@Autowire这样的数据源实例:private DataSource dataSource; @Autowired private DataSource setDataSource() { return dataSource; }这仍然返回相同的错误:nested exception is java.lang.IllegalArgumentException: No DataSource specified.
    • 你不需要注入DataSource本身,你需要注入JdbcTemplate。 JdbcTemplate 需要 dataSource,你已经在 xml 中配置了它。您需要 @Autowired JdbcTemplate jdbcTemplate; 中的 LkTimezonesDAO 并删除使用 new 关键字实例化 JdbcTemplate 的行。
    • 请参阅我上面的回答,了解我的最后一次尝试。抱歉,我的评论太长了(超过 1747 个字符)。
    • @Autowired 不适用于方法,它应该只在字段上。你得到了NullPointerException,因为你很可能正在使用 new 关键字初始化LkTimezonesDAO,你应该再次将它 @Autowire 它放在某个地方或从 Spring Context 手动获取。您不会实例化由 Spring 管理的 bean。
    • 不幸的是,我没有使用 new 关键字初始化 LkTimesDAO。我在 'webDataAccessBeans.xml' 中实例化 bean:&lt;bean id="lkTimeZonesDAO" parent="abstractCustomerDao" class="com.att.callmanager.customer.dao.LkTimezonesDAO"&gt; 并连接到 'webObjectBeans.xml' 中的 'LkTimeZones' 和 'buisnessObjectBeans,xml' 中的 DAO 集合。
    【解决方案2】:

    好的,我将dataSource 注入到 DAO 文件中,但再次出现“NullPointerException”错误。不幸的是,我认为部分问题在于我有 14 个不同的 DAO 文件将各种查询连接到数据库。我通过@Autowire 连接到 LkTimezonesDAO.java 的一个实例如下:

    `import javax.sql.DataSource;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.jdbc.core.support.JdbcDaoSupport;
     import org.springframework.stereotype.Repository;
     import org.springframework.stereotype.Service;
    
     /**
      * ClmgrLkTimezonesTableDAO is a data access object specifically designed to
      * retrieve and set LkTimeZone object data.
      *
      */
      @Service("timeZonesService")
      @Repository
      public class LkTimezonesDAO extends CustomerWebDaoImpl
      {
          private static final long serialVersionUID = 2367566965661648309L;
          private JdbcTemplate jdbcTemplate ;
    
          @Autowired
          public void init(DataSource dataSource) {
             this.jdbcTemplate = new JdbcTemplate(dataSource);
          }
    

    ``
    这是一个示例堆栈跟踪:

    `####<Apr 21, 2015 11:50:30 AM GMT-06:00> <Warning> <HTTP>            <xxxxxxxx.xx.xxx.xxx.com> <managedserver1> 
     <[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1429638630468> 
     <BEA-101162> <User defined listener org.springframework.web.context.ContextLoaderListener failed: 
     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lkTimeZones' defined in ServletContext resource 
     [/WEB-INF/webObjectBeans.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException.
     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lkTimeZones' defined in ServletContext resource 
     [/WEB-INF/webObjectBeans.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
        at  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
        at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:376)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    

    【讨论】:

    • 好的,我在@Autowired JdbcTemplate 实例从 applicationContext.xml 到 DAO 的尝试出现 NPE (NullPointerException) 错误。是否有人需要查看更多代码才能找到解决方案?
    猜你喜欢
    • 2019-04-16
    • 2013-07-18
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多