web-dev-qa-db-fra.com

BeanFactory non initialisé ou déjà fermé - appelez 'refresh' avant d'accéder aux beans

J'essaie d'ajouter une sécurité de ressort à une application JSF normale. Après des tentatives répétées et je ne parviens pas avec l'erreur suivante sur la relance de Tomcat.

Voici la trace entière de la pile. Toute aide est appréciée.

EDIT: après d'autres tests, je remarque la trace de pile suivante, avant le deuxième ensemble d'exceptions SEVERE concernant BeanFactory non initialisé ou déjà fermé

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderList
ener
Java.lang.NoSuchMethodError: org.springframework.web.context.support.XmlWebApplicationContext.getEnvironment()Lorg/springframework/
core/env/ConfigurableEnvironment;
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.Java:87)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicat
ionContext.Java:130)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.Java:46
7)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:397)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.Java:385)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.Java:284)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.Java:111)
        at org.Apache.catalina.core.StandardContext.listenerStart(StandardContext.Java:4205)
        at org.Apache.catalina.core.StandardContext.start(StandardContext.Java:4704)
        at org.Apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.Java:799)
        at org.Apache.catalina.core.ContainerBase.addChild(ContainerBase.Java:779)        at org.Apache.catalina.core.StandardHost.addChild(StandardHost.Java:601)
        at org.Apache.catalina.startup.HostConfig.deployWAR(HostConfig.Java:943)
        at org.Apache.catalina.startup.HostConfig.deployWARs(HostConfig.Java:778)
        at org.Apache.catalina.startup.HostConfig.deployApps(HostConfig.Java:504)
        at org.Apache.catalina.startup.HostConfig.start(HostConfig.Java:1315)
        at org.Apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.Java:324)
        at org.Apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.Java:142)
        at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1061)
        at org.Apache.catalina.core.StandardHost.start(StandardHost.Java:840)
        at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1053)
        at org.Apache.catalina.core.StandardEngine.start(StandardEngine.Java:463)
        at org.Apache.catalina.core.StandardService.start(StandardService.Java:525)
        at org.Apache.catalina.core.StandardServer.start(StandardServer.Java:754)
        at org.Apache.catalina.startup.Catalina.start(Catalina.Java:595)
        at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:39)
        at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:25)
        at Java.lang.reflect.Method.invoke(Method.Java:597)        at org.Apache.catalina.startup.Bootstrap.start(Bootstrap.Java:289)
        at org.Apache.catalina.startup.Bootstrap.main(Bootstrap.Java:414)

After this error, the mojarra 2.1.4 is initialized before the BeanFactory exception shows up

SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
Java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
        at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.Java:171)
        at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.Java:1048)
        at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.Java:1022)
        at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.Java:970)
        at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.Java:556)
        at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.Java:142)
        at org.Apache.catalina.core.StandardContext.listenerStop(StandardContext.Java:4245)
        at org.Apache.catalina.core.StandardContext.stop(StandardContext.Java:4886)
        at org.Apache.catalina.core.StandardContext.start(StandardContext.Java:4750)
        at org.Apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.Java:799)
        at org.Apache.catalina.core.ContainerBase.addChild(ContainerBase.Java:779)
        at org.Apache.catalina.core.StandardHost.addChild(StandardHost.Java:601)
        at org.Apache.catalina.startup.HostConfig.deployWAR(HostConfig.Java:943)
        at org.Apache.catalina.startup.HostConfig.deployWARs(HostConfig.Java:778)
        at org.Apache.catalina.startup.HostConfig.deployApps(HostConfig.Java:504)
        at org.Apache.catalina.startup.HostConfig.start(HostConfig.Java:1317)
        at org.Apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.Java:324)
        at org.Apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.Java:142)
        at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1065)
        at org.Apache.catalina.core.StandardHost.start(StandardHost.Java:840)
        at org.Apache.catalina.core.ContainerBase.start(ContainerBase.Java:1057)
        at org.Apache.catalina.core.StandardEngine.start(StandardEngine.Java:463)
        at org.Apache.catalina.core.StandardService.start(StandardService.Java:525)
        at org.Apache.catalina.core.StandardServer.start(StandardServer.Java:754)
        at org.Apache.catalina.startup.Catalina.start(Catalina.Java:595)
        at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:39)
        at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:25)
        at Java.lang.reflect.Method.invoke(Method.Java:597)
        at org.Apache.catalina.startup.Bootstrap.start(Bootstrap.Java:289)
        at org.Apache.catalina.startup.Bootstrap.main(Bootstrap.Java:414)

mon web.xml est

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://Java.Sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://Java.Sun.com/xml/ns/javaee http://Java.Sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
         /WEB-INF/applicationContext.xml
         /WEB-INF/applicationContext-security.xml
       </param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>false</param-value>
    </context-param>
    <context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>com.icesoft.faces.debugDOMUpdate</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>com.icesoft.faces.concurrentDOMViews</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>com.icesoft.faces.synchronousUpdate</param-name>
        <param-value>true</param-value>
    </context-param>

    <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <!-- <load-on-startup>1</load-on-startup> -->
    </servlet>
    <servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
<!-- Extension Mapping -->
    <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
    <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/icefaces/*</url-pattern>
    </servlet-mapping>
    <session-config>
    <session-timeout>90</session-timeout>
    </session-config>


    <servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/xmlhttp/*</url-pattern>
    <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>


<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Et mon applicationContext-security.xml est

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="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.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.0.xsd">

<!--  key configuration here is an entry point to be used by security intercepts -->
<http realm="Sample Realm" entry-point-ref="authenticationEntryPoint" auto-config="false">

    <custom-filter ref="sessionManagementFilter" before="SESSION_MANAGEMENT_FILTER" />
    <!-- any role that is used to protect a directory, can be multiples -->
    <intercept-url pattern='/secure/**' access='ROLE_READER' />

    <!-- enable form login to use UsernamePasswordAuthenticationFilter [/j_spring_security_check] -->
    <form-login login-page="/general/logins/htmlLogin.faces"  
                    authentication-failure-url="/general/logins/loginFailed.jsf"/>

    <!-- logout page uses the default LogoutFilter, no changes are needed as IT accepts a GET call... -->
    <!-- here is an example logout link:
    <a href="#{request.contextPath}/j_spring_security_logout">Logout</a> -->
        <logout logout-url="/j_spring_security_logout"
                logout-success-url="/general/main.jsf"
                invalidate-session="true"/>
    </http>

    <beans:bean id="authenticationEntryPoint"
        class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <beans:property name="loginFormUrl" value="/general/logins/login.jsf" />
    </beans:bean>

    <!-- test with this before you hook up your LDAP or other Authentication Manager -->
    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="[email protected]" password="pass1234" authorities="ROLE_READER"/>
                <user name="[email protected]" password="pass5678" authorities="ROLE_READER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="sessionManagementFilter" class="org.springframework.security.web.session.SessionManagementFilter">
        <beans:constructor-arg name="securityContextRepository" ref="httpSessionSecurityContextRepository" />
        <beans:property name="invalidSessionUrl" value="/general/logins/sessionExpired.jsf" />
        <!-- this permits redirection to session timeout page from javascript/ajax or http -->
        <beans:property name="redirectStrategy" ref="jsfRedirectStrategy" />
    </beans:bean>

    <beans:bean id="jsfRedirectStrategy" class="com.Palm.safe.spring.security.JsfRedirectStrategy"/>
    <beans:bean id="httpSessionSecurityContextRepository" class="org.springframework.security.web.context.HttpSessionSecurityContextRepository"/>

</beans:beans>
18
LambeauLeap

Je vois ce code dans applicationContext-security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="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.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.0.xsd">

Vous utilisez peut-être une version de printemps incorrecte. Par exemple, si vous utilisez la version 3.1.xx de spring-xxx (spring-security-web.jar ou spring-security-core.jar) dans votre bibliothèque, cette erreur se produira. Essayez de changer le code dans applicationContext-security.xml de 3.0.xsd à 3.1.xsd (le numéro de version dans le code doit correspondre à la version dans le dossier de la bibliothèque).

J'espère que cette aide.

10
huahsin68

J'ai eu le même problème, et il s'est avéré qu'il y avait un problème avec la chaîne de dépendance Maven Spring. Dependency org.springframework spring-context a été importé avec la version 3.0.5, alors que la version de printemps globale que j’utilisais était 3.2.2. L'ajout explicite de la dépendance dans mon pom.xml l'a résolu.

J'ai détecté le problème en utilisant l'argument -verbose: class jvm lors de l'exécution du serveur et en vérifiant les classes jars de l'arbre d'héritage XmlWebApplicationContext.

5
Mihai Tudor

Lors de la suppression des commentaires d'un bloc particulier dans applicationcontext.xml, j'ai oublié de supprimer -> "La fin du commentaire". Je ne l'ai pas remarqué et j'ai eu cette erreur. 

Plus tard enlevé et tout fonctionnait bien !! 

4
om39a

Vous devez ajouter RequestContextListener à votre web.xml afin de lier l’objet de requête HTTP au thread qui traite cette requête. Ainsi, les beans dont la taille des requêtes et celle de la session sont définies sont disponibles plus loin dans la chaîne d’appel.

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
4
Ravi

J'ai eu cette erreur aussi, et le problème s'est avéré que, avec couper-coller, j'avais par inadvertance répertorié le même haricot deux fois dans le fichier spring-servlet.xml

<bean id="pathInfoHelper" class="org.nationalhistory.util.PathInfoHelper">
</bean>

<bean id="imageIndexer" class="org.nationalhistory.util.ImageIndexer"/>

<bean id="pathInfoHelper" class="org.nationalhistory.util.PathInfoHelper">
</bean>

J'ai regardé ici et trouvé ceci avec l'aide de la réponse de om39a ci-dessus (merci!). Je venais de modifier le fichier et je savais que j'avais collé quelque chose de plus (ou de moins) par inadvertance. Cette double entrée, cependant, n'était PAS le haricot auquel l'erreur faisait référence!

2
excyberlabber

J'avais ce problème jusqu'à ce que je supprime le projet en question des déploiements du serveur (dans JBoss Dev Studio, cliquez avec le bouton droit de la souris sur le serveur et "Supprimer" le projet dans la vue Serveurs), puis procédez comme suit: 

  1. Redémarrage du serveur JBoss EAP 6.1 sans aucun projet déployé. 
  2. Une fois le serveur démarré, j'ai ajouté le projet en question au serveur. 

Après cela, redémarrez simplement le serveur (en mode débogage ou exécution) en sélectionnant le serveur, PAS le projet lui-même.

Cela semblait effacer tous les paramètres/états/mémoire/tout ce qui était à l'origine du problème et je n'ai plus eu l'erreur.

0
Fuzzy Analysis

utiliser cette dépendance dans votre pom.xml

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency>

0
vipin gupta