2012-11-05 124 views
0

我只是想將spring框架添加到我的項目中,但是當我嘗試在jboss 6.1.0 Final中部署我的war文件時,它提供了一個錯誤。我得到以下錯誤,jboss錯誤中的彈簧部署

11:01:46,556 INFO [STDOUT] ERROR: org.springframework.web.context.ContextLoader - Context initializ 
ation failed 
11:01:46,557 INFO [STDOUT] org.springframework.beans.factory.BeanCreationException: Error creating 
bean with name 'com.tapgift.merchant.integration.spring.DaoFactoryPostProcessor#0' defined in class 
path resource [config/dao-defs.xml]: Cannot resolve reference to bean 'DAOFactory' while setting bea 
n property 'daoFactory'; nested exception is org.springframework.beans.factory.BeanCreationException 
: Error creating bean with name 'DAOFactory' defined in class path resource [config/dao-defs.xml]: I 
nvocation of init method failed; nested exception is java.lang.NullPointerException 

這道def.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 

    <!-- All DAO bean definitions goes here --> 
    <bean id="DAOFactory" 
     class="com.tapgift.merchant.integration.db.dao.jdbc.JdbcDaoFactoryImpl" /> 

    <bean class="com.tapgift.merchant.integration.spring.DaoFactoryPostProcessor"> 
     <property name="daoFactory" ref="DAOFactory" /> 
    </bean> 

    <bean id="merchantDao" 
     class="com.tapgift.merchant.integration.db.dao.jdbc.JdbcMerchantDao"> 
     <property name="dataSource" ref="dataSource" />  
     <property name="queryFactory" ref="QueryFactory" />   
    </bean> 

</beans> 

這是應用程序的context.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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:annotation-config /> 
    <context:component-scan base-package="com.tapgift.merchant" /> 

    <import resource="classpath:config/spring-integration.xml" /> 
    <import resource="classpath:config/dao-defs.xml" /> 
    <import resource="classpath:config/query-defs.xml" /> 
    <import resource="classpath:config/spring-support.xml" /> 


</beans> 

最後這是web.xml中

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:config/application-context.xml 
       /WEB-INF/spring/root-context.xml</param-value> 
</context-param> 

哎呀,差點錯過了pom,

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.tapgift</groupId> 
    <artifactId>merchant</artifactId> 
    <name>merchant-server</name> 
    <packaging>war</packaging> 
    <version>1.0.0-BUILD-SNAPSHOT</version> 
    <properties> 
     <java-version>1.7</java-version> 
     <org.springframework-version>3.1.0.RELEASE</org.springframework-version> 
     <org.aspectj-version>1.6.9</org.aspectj-version> 
     <org.slf4j-version>1.5.10</org.slf4j-version> 
    </properties> 
    <dependencies> 
     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${org.springframework-version}</version>    
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jdbc</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 

     <dependency> 
        <groupId>commons-collections</groupId> 
        <artifactId>commons-collections</artifactId> 
        <version>3.2.1</version> 
        <scope>compile</scope> 
      </dependency> 
      <dependency> 
        <groupId>commons-lang</groupId> 
        <artifactId>commons-lang</artifactId> 
        <version>2.6</version> 
        <scope>compile</scope> 
      </dependency> 
      <dependency> 
       <groupId>commons-io</groupId> 
       <artifactId>commons-io</artifactId> 
       <version>2.0.1</version> 
       <scope>compile</scope> 
      </dependency> 
      <dependency> 
       <groupId>commons-dbcp</groupId> 
       <artifactId>commons-dbcp</artifactId> 
       <version>1.4</version> 
       <scope>compile</scope> 
      </dependency> 

     <!-- AspectJ --> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>${org.aspectj-version}</version> 
     </dependency> 

     <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>${org.slf4j-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>${org.slf4j-version}</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.15</version>   
      <exclusions> 
       <exclusion> 
        <groupId>javax.mail</groupId> 
        <artifactId>mail</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>javax.jms</groupId> 
        <artifactId>jms</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jdmk</groupId> 
        <artifactId>jmxtools</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>com.sun.jmx</groupId> 
        <artifactId>jmxri</artifactId> 
       </exclusion> 
      </exclusions> 
      <scope>compile</scope> 
     </dependency> 

     <!-- @Inject --> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 

     <!-- Servlet --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <!-- JSON view resolver --> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>1.9.2</version> 
     </dependency> 

     <!-- Test --> 
     <!-- <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.7</version> 
      <scope>test</scope> 
     </dependency> -->  
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <artifactId>maven-eclipse-plugin</artifactId> 
       <version>2.9</version> 
       <configuration> 
        <additionalProjectnatures> 
         <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 
        </additionalProjectnatures> 
        <additionalBuildcommands> 
         <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 
        </additionalBuildcommands> 
        <downloadSources>true</downloadSources> 
        <downloadJavadocs>true</downloadJavadocs> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <compilerArgument>-Xlint:all</compilerArgument> 
        <showWarnings>true</showWarnings> 
        <showDeprecation>true</showDeprecation> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>exec-maven-plugin</artifactId> 
       <version>1.2.1</version> 
       <configuration> 
        <mainClass>org.test.int1.Main</mainClass> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

我可以看到編譯和彙編到war文件的所有文件。感謝,如果春天的專家能指出我這個部署錯誤的原因。

編輯:新增DaoFactoryImpl

package com.tapgift.merchant.integration.db.dao.jdbc; 

import java.lang.reflect.ParameterizedType; 
import java.util.HashMap; 
import java.util.Map; 

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

import com.tapgift.merchant.integration.db.dao.BaseDao; 
import com.tapgift.merchant.integration.db.dao.DaoFactory; 
import com.tapgift.merchant.integration.db.dao.EntityBaseDao; 

/** 
* Class <code>CaseServiceImpl</code> is an implementation of the <tt>DaoFactory</tt> interface. 
*/ 
public class JdbcDaoFactoryImpl implements DaoFactory, InitializingBean, ApplicationContextAware 
{ 
    private ApplicationContext m_appCtx; 
    private Map<String, EntityBaseDao<?>> m_daoMap = new HashMap<String, EntityBaseDao<?>>(); 

    /** 
    * {@inheritDoc}. 
    */ 
    @Override 
    @SuppressWarnings("rawtypes") 
    public void afterPropertiesSet() throws Exception 
    { 
     Map<String, BaseDao> beanMap = m_appCtx.getBeansOfType(BaseDao.class); 
     for (final BaseDao dao : beanMap.values()) 
     { 
      m_daoMap.put(((EntityBaseDao) dao).getEntityType().getName(), (EntityBaseDao) dao); 
     } 
    } 

    /** 
    * {@inheritDoc}. 
    */ 
    @Override 
    public BaseDao getDao(String name) 
    { 
     return this.m_daoMap.get(name); 
    } 

    /** 
    * {@inheritDoc}. 
    */ 
    @Override 
    public <T> T getEntityDao(Class<T> clazz) 
    { 
     String entityName = ((Class) ((ParameterizedType) clazz.getGenericInterfaces()[0]).getActualTypeArguments()[0]) 
      .getSimpleName().toUpperCase(); 
     return clazz.cast(this.m_daoMap.get(entityName)); 
    } 

    /** 
    * Set method for m_appCtx 
    */ 
    @Override 
    public void setApplicationContext(ApplicationContext appCtx) throws BeansException 
    { 
     this.m_appCtx = appCtx; 
    } 

} 

更新2 這個問題似乎被創建JdbbcDaoFactoryimpl例如,當我刪除了 <bean id="DAOFactory" class="com.tapgift.merchant.integration.db.dao.jdbc.JdbcDaoFactoryImpl" />與它的依賴,我可以沒有任何問題進行部署。我檢查了戰爭文件,所有彈簧罐都屬於同一版本。拼命地需要幫助。

回答

0

沒關係,我能夠解決它,與Ajinkya顯示的有點相關。 對此代碼的JdbcDaoFactoryImpl問題是m_daoMap.put(((EntityBaseDao) dao).getEntityType().getName()((EntityBaseDao) dao).getEntityType()沒有返回任何值,但爲空。我忘了重寫/刪除實際的dao實現類的特定方法。一旦我刪除該方法,我就能夠部署它,而不會面臨任何問題。

無論如何感謝您的支持。

1

問題:

無法解析參考豆 'DAOFactory' 同時設定豆 財產 'daoFactory';

原因:

錯誤創建名爲 'DAOFactory' 類路徑限定 資源[配置/ DAO-defs.xml]豆:init方法的調用失敗; 嵌套的例外是顯示java.lang.NullPointerException

春天不能創建的DAOFactoryJdbcDaoFactoryImpl對象,因爲它遇到了NullPointerException在初始化對象。
添加一些有助於找出確切原因的相關代碼。


更新: 看起來像下面的行導致了問題

Map<String, BaseDao> beanMap = m_appCtx.getBeansOfType(BaseDao.class); 

春節沒能找到BaseDao類型的豆。確保你已經定義了一個類型的豆BaseDao

+0

嗨Ajinkya,謝謝,用'JdbcDaoFactoryImpl'來源更新了這個問題。 – bluelabel

+0

@bluelabel:我已經更新了答案。 – xyz