2015-10-02 54 views
1

我想將具有不同applicationContext(BeanLocation.xml)的springProject集成到具有另一個applicationContext的Web項目中。但是我在部署我的Web應用程序時無法這樣做。我收到以下錯誤。嘗試從另一個項目導入應用程序上下文時出現BeanDefinitionParsingException

SEVERE: Context initialization failed 
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/spring/config/BeanLocations.xml] 
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring/config/BeanLocations.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/config/BeanLocations.xml] cannot be opened because it does not exist 
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70) 
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85) 
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76) 
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:235) 
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:186) 
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:171) 
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:144) 
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:100) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:510) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) 
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) 
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) 
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) 
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5003) 

這裏是我的不同的文件:

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5" metadata-complete="true"> 
     <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.jsf</url-pattern> 
     <!-- Extension of file to be used like localhost:..../helloWorld.jsf --> 

    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>faces/index.xhtml</welcome-file> 
    </welcome-file-list> 


    <context-param> 
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name> 
    <param-value>.xhtml</param-value> 
</context-param> 

<context-param> 
     <param-name>javax.faces.application.CONFIG_FILES</param-name> 
     <param-value>/WEB-INF/faces-config.xml</param-value> 
</context-param> 

    <context-param> 
     <param-name>primefaces.THEME</param-name> 
     <param-value>glass-x</param-value> 
    </context-param> 

    <!-- Adding for spring application Context loading --> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
<listener-class> 
    org.springframework.web.context.ContextLoaderListener 
</listener-class> 
</listener> 
<listener> 
<listener-class> 
    org.springframework.web.context.request.RequestContextListener 
</listener-class> 
</listener> 


</web-app> 

faces-config: 

<?xml version="1.0" encoding="UTF-8"?> 
<faces-config 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" 
    version="2.2"> 

    <application> 
     <el-resolver> 
      org.springframework.web.jsf.el.SpringBeanFacesELResolver 
     </el-resolver> 
    </application> 


</faces-config> 

ApplicationContext對象(Web的應用程序):

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 

    <context:component-scan base-package="com.*" /> 


    <import resource="classpath:/spring/config/BeanLocations.xml"/> 

</beans> 

Test.pom:

<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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.mytutorial</groupId> 
    <artifactId>WebPrrimeFaces</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <name>simpleWeb Maven Webapp</name> 
    <description>Simple Web Test</description> 

    <repositories> 
     <repository> 
      <id>prime-repo</id> 
      <name>PrimeFaces Maven Repository</name> 
      <url>http://repository.primefaces.org</url> 
      <layout>default</layout> 
     </repository> 
    </repositories> 

    <dependencies> 
     <!-- Adding dependency for another project --> 
     <dependency> 
      <groupId>com.mkyong.common</groupId> 
      <artifactId>SpringExample</artifactId> 
      <version>1.0-SNAPSHOT</version> 
      <type>jar</type> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 

     <!-- Servlet --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <!-- Faces Implementation --> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-impl</artifactId> 
      <version>2.2.4</version> 
     </dependency> 
     <!-- Faces Library --> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>2.2.4</version> 
     </dependency> 
     <!-- Primefaces Version 5 --> 
     <dependency> 
      <groupId>org.primefaces</groupId> 
      <artifactId>primefaces</artifactId> 
      <version>5.0</version> 
     </dependency> 
     <!-- JSP Library --> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>javax.servlet.jsp-api</artifactId> 
      <version>2.3.1</version> 
     </dependency> 
     <!-- JSTL Library --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.1.2</version> 
     </dependency> 
     <!-- Primefaces Theme Library --> 
     <dependency> 
      <groupId>org.primefaces.themes</groupId> 
      <artifactId>glass-x</artifactId> 
      <version>1.0.6</version> 
     </dependency> 
     <!-- Spring Web dependency --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.1.4.RELEASE</version> 
     </dependency> 


    </dependencies> 

    <build> 
     <sourceDirectory>src</sourceDirectory> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <warSourceDirectory>WebContent</warSourceDirectory> 
        <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

的HelloWorld:

package com.java.view; 

import java.io.Serializable; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ManagedProperty; 
import javax.faces.bean.SessionScoped; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Scope; 
import org.springframework.stereotype.Component; 

import com.mkyong.stock.bo.StockBo; 
import com.mkyong.stock.model.Stock; 

@ManagedBean(name = "helloWorld") 
@SessionScoped 
public class HelloWorld implements Serializable { 

    private static final long serialVersionUID = -6913972022251814607L; 

    private String s1 = "Hello World!!"; 

    @ManagedProperty("#{stockBo}") 
    private StockBo stockBo; 

    public StockBo getStockBo() { 
     return stockBo; 
    } 

    public void setStockBo(StockBo stockBo) { 
     this.stockBo = stockBo; 
    } 

    public String getS1() { 
     System.out.println(s1); 
     return s1; 
    } 

    public void setS1(String s1) { 
     this.s1 = s1; 
    } 

    Stock stck=stockBo.findByStockCode("7668"); 



} 

的index.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:f="http://java.sun.com/jsf/core"> 
<h:head> 
    <title>Hello World JSF Example</title> 
</h:head> 
<h:body> 

    <h1>Hello World PrimeFaces</h1> 
    <f:view> 
     <h:form> 

      <h:outputLabel value="#{stck.stockName}" /> 

     </h:form> 

    </f:view> 

</h:body> 
</html> 

BeanLocation.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 

    <!-- Database Configuration --> 
    <import resource="../database/DataSource.xml"/> 
    <import resource="../database/Hibernate.xml"/> 

    <!-- Auto scan the components --> 
    <context:component-scan 
     base-package="com.mkyong.stock" /> 

</beans> 

我已經嘗試了所有可能的組合導入BeanLocation.xml但不成功。 我幫我找到我的錯誤,我經歷了各種帖子在stackoverflow集成不同的applicationContext,但我無法解決這個問題。我也附上我的項目的一些屏幕截圖。

+0

我無法上傳圖片,不知道爲什麼它沒有得到更新。 – vibhas

+0

查看消息:'path資源[spring/config/BeanLocations.xml]無法打開,因爲它不存在' – Jens

+0

'BeanLocation.xml'不是'BeanLocations.xml'在文件名末尾看到's' – Jens

回答

2

請確保您有BeanLocations.xml下的文件src/main/resources/spring/config文件夾中的項目,並編輯進口資源標記爲:

<import resource="classpath*:/spring/config/BeanLocations.xml"/> 

否則如果是在同一文件夾中的applicationContext文件,然後編輯導入標籤如下:

<import resource="BeanLocations.xml"/> 

+0

感謝arpit第一個爲我工作。但後來我遇到另一個問題:org.springframework.beans.factory.BeanCreationException:創建名爲'stockBo'的bean時出錯:注入自動裝配依賴失敗;嵌套異常是org.springframework.beans.factory。BeanCreationException:無法自動裝入字段:com.mkyong.stock.dao.StockDao com.mkyong.stock.bo.impl.StockBoImpl.stockDao;嵌套異常是org.springframework.beans.factory.BeanCreationException:創建名爲'stockDao'的bean時出錯:注入自動裝配依賴失敗; – vibhas

+0

我的dao releted文件存在於BeanLocations.xml中,理想情況下它應該能夠注入。 – vibhas

+0

用'com.mkyong.stock'包中的代碼打開一個新問題。 – Arpit

相關問題