使用setApplicationContext出现运行错误,求解!
澳门博彩导航网址
澳门博彩导航网址
当前位置 : 澳门博彩导航网址 > 澳门博彩导航

使用setApplicationContext出现运行错误,求解!

因为想在controller以外的类里使用bean,所以在网上找了个实现ApplicationContextAware接口的类,如下

public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext; // Spring应用上下文环境

    // 下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法
    @Override
    public void setApplicationContextApplicationContext applicationContext
            throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext {
        return applicationContext;
    }

    public static Object getBeanString name throws BeansException {
        return applicationContext.getBeanname;
    }

    @SuppressWarnings"unchecked"
    public static Object getBeanString name, Class requiredType
            throws BeansException {
        return applicationContext.getBeanname, requiredType;
    }

    public static boolean containsBeanString name {
        return applicationContext.containsBeanname;
    }

    public static boolean isSingletonString name throws NoSuchBeanDefinitionException {
        return applicationContext.isSingletonname;
    }

    public static Class getTypeString name    throws NoSuchBeanDefinitionException {
        return applicationContext.getTypename;
    }

    public static String[] getAliasesString name throws NoSuchBeanDefinitionException {
        return applicationContext.getAliasesname;
    }
}

并在datasource.xml中加入了bean:

<bean id="SpringContextUtil" class="com.streetLight.util.SpringContextUtil"  scope="singleton"/>

但网上的那个文章没有介绍具体要怎么做,只是说在要引用bean的类里面添加以下代码,即可:


ConcentratorService concentratorService =ConcentratorService SpringContextUtil.getBean"concentratorService";

但是出来了NULLPOINTER的运行错误,大概是因为先要用setApplicationContext布置一下context,所以我在代码前面加了以下代码:

    SpringContextUtil scu=new SpringContextUtil;
    scu.setApplicationContextnew FileSystemXmlApplicationContext"/WebRoot/WEB-INF/config/datasource.xml";

但是这次又出现了一些运行错误:

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 8 in XML document from file [/Users/apple/Documents/Eclipse/workspace/streetLight/WebRoot/WEB-INF/config/datasource.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 74; cvc-elt.1: 找不到元素 beans 的声明。
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitionsXmlBeanDefinitionReader.java:398
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitionsXmlBeanDefinitionReader.java:335
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitionsXmlBeanDefinitionReader.java:303
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitionsAbstractBeanDefinitionReader.java:180
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitionsAbstractBeanDefinitionReader.java:216
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitionsAbstractBeanDefinitionReader.java:187
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitionsAbstractBeanDefinitionReader.java:251
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitionsAbstractXmlApplicationContext.java:127
    at org.apache.xbean.spring.context.FileSystemXmlApplicationContext.loadBeanDefinitionsFileSystemXmlApplicationContext.java:168
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactoryAbstractRefreshableApplicationContext.java:129
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactoryAbstractApplicationContext.java:540
    at org.springframework.context.support.AbstractApplicationContext.refreshAbstractApplicationContext.java:454
    at org.apache.xbean.spring.context.FileSystemXmlApplicationContext.<init>FileSystemXmlApplicationContext.java:149
    at org.apache.xbean.spring.context.FileSystemXmlApplicationContext.<init>FileSystemXmlApplicationContext.java:48
    at com.streetLight.util.ReceiveThread.initReceiveThread.java:46
    at websocket.runSocket.mainrunSocket.java:26
Caused by: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 74; cvc-elt.1: 找不到元素 beans 的声明。
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseExceptionErrorHandlerWrapper.java:198
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.errorErrorHandlerWrapper.java:134
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportErrorXMLErrorReporter.java:437
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportErrorXMLErrorReporter.java:368
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportErrorXMLErrorReporter.java:325
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElementXMLSchemaValidator.java:1906
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElementXMLSchemaValidator.java:746
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElementXMLNSDocumentScannerImpl.java:379
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHookXMLNSDocumentScannerImpl.java:605
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.nextXMLDocumentFragmentScannerImpl.java:3138
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.nextXMLDocumentScannerImpl.java:880
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.nextXMLDocumentScannerImpl.java:606
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.nextXMLNSDocumentScannerImpl.java:117
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocumentXMLDocumentFragmentScannerImpl.java:510
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parseXML11Configuration.java:848
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parseXML11Configuration.java:777
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parseXMLParser.java:141
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parseDOMParser.java:243
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parseDocumentBuilderImpl.java:347
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocumentDefaultDocumentLoader.java:76
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocumentXmlBeanDefinitionReader.java:428
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitionsXmlBeanDefinitionReader.java:390
    ... 15 more

之后我在网上找了一下,说是datasource.xml文件里面的xsd文件未能被找到。以下是该xml文件的部分代码:

<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"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc spring-mvc-3.2.xsd
  http://www.springframework.org/schema/beans spring-beans-4.2.xsd
  http://www.springframework.org/schema/util spring-util-3.2.xsd
  http://www.springframework.org/schema/context spring-context-4.2.xsd">
...
</beans>

的确,http://www.springframework.org/schema/beans spring-beans-4.2.xsd地址要改成http://www.springframework.org/schema/context/spring-context-4.2.xsd才能在网页上被查找出来,但之后发现这个格式是没有错的,改了反而在工程被运行时产生错误。

搞了好几个小时真的不知道是哪里出了问题了,所以来请教各位仁兄~先谢过~~

============================================================

我自己解决了。。。。datasource.xml要改成以下方式:

<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"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  http://www.springframework.org/schema/util 
  http://www.springframework.org/schema/util/spring-util-3.2.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.2.xsd">

要怎样才能使用本地的xsd啊= =。。。

其实可以不用用到这个工具类,在需要getBean的类注入一个ApplicationContext即可。

@Autowired
private ApplicationContext context;

...

context.getBean"bean name";
package com.alibaba.tboss.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
/**
 * 
 * ClassName: SpringContextUtil <br/>
 * Function: 在applicationContext.xml中加入配置 
 *           <bean id="SpringContextUtil" class="com.alibaba.tboss.util.SpringContextUtil"/>
 *           用来得到spring配置的Bean <br/>
 * date: 2015年12月31日 <br/>
 */
public class SpringContextUtil implements ApplicationContextAware {  
   private static ApplicationContext applicationContext;     //Spring应用上下文环境  
    
   /** 
   * 实现ApplicationContextAware接口的回调方法,设置上下文环境    
   * @param applicationContext 
   * @throws BeansException 
   */  
   public void setApplicationContextApplicationContext applicationContext throws BeansException {  
       SpringContextUtil.applicationContext = applicationContext;  
   }  
    
   /** 
   * @return ApplicationContext 
   */  
   public static ApplicationContext getApplicationContext {  
     return applicationContext;  
   }  
    
   /** 
   * 获取对象    
   * @param name 
   * @return Object 一个以所给名字注册的bean的实例 
   * @throws BeansException 
   */  
   public static Object getBeanString name throws BeansException {  
     return applicationContext.getBeanname;  
   } 
   
   /**
    * 
    * getBean: 获取类型为requiredType的对象 . <br/>
    *
    * @param requiredType 返回对象类型 
    * @return 返回requiredType类型对象 
    */
   public static <T> T getBeanClass<T> requiredType {
       return applicationContext.getBeanrequiredType;
   }
    
   /** 
   * 获取类型为requiredType的对象 
   * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException) 
   * @param name       bean注册名 
   * @param requiredType 返回对象类型 
   * @return Object 返回requiredType类型对象 
   * @throws BeansException 
   */  
public static <T> T getBeanString name, Class<T> requiredType throws BeansException {  
     return applicationContext.getBeanname, requiredType;  
   }  
    
   /** 
   * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true  
   * @param name 
   * @return boolean 
   */  
   public static boolean containsBeanString name {  
     return applicationContext.containsBeanname;  
   }  
    
   /** 
   * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 
   * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)    
   * @param name 
   * @return boolean 
   * @throws NoSuchBeanDefinitionException 
   */  
   public static boolean isSingletonString name throws NoSuchBeanDefinitionException {  
     return applicationContext.isSingletonname;  
   }  
    
   /** 
   * @param name 
   * @return Class 注册对象的类型 
   * @throws NoSuchBeanDefinitionException 
   */  
   @SuppressWarnings"rawtypes"
public static Class getTypeString name throws NoSuchBeanDefinitionException {  
     return applicationContext.getTypename;  
   }  
    
   /** 
   * 如果给定的bean名字在bean定义中有别名,则返回这些别名    
   * @param name 
   * @return 
   * @throws NoSuchBeanDefinitionException 
   */  
   public static String[] getAliasesString name throws NoSuchBeanDefinitionException {  
     return applicationContext.getAliasesname;  
   }  
 }
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call refresh before accessing beans via the ApplicationContext
    at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactoryAbstractRefreshableApplicationContext.java:170
    at org.springframework.context.support.AbstractApplicationContext.destroyBeansAbstractApplicationContext.java:921
    at org.springframework.context.support.AbstractApplicationContext.doCloseAbstractApplicationContext.java:895
    at org.springframework.context.support.AbstractApplicationContext.closeAbstractApplicationContext.java:841
    at org.springframework.web.context.ContextLoader.closeWebApplicationContextContextLoader.java:579
    at org.springframework.web.context.ContextLoaderListener.contextDestroyedContextLoaderListener.java:115
    at org.apache.catalina.core.StandardContext.listenerStopStandardContext.java:4858

楼主的项目根本没启动成功吧,应该还有其它的错误信息。仔细看上面的堆栈 listenerStop -> closeWebApplicationContext 这个错误是在停止Tomcat的时候发生的错误。

将datasource.xml中xsi补全了就好了。

栏目列表

最新文章

广告位

澳门博彩导航网址