2017-07-11 407 views
0

我正在嘗試使用Spring MVC和security來創建小型登錄應用程序。我從錯誤消息中瞭解到,應該初始化bean工廠以訪問beans.I試圖瞭解我應該在哪裏做到這一點?在這個問題上請求你的幫助。BeanFactory未初始化或已關閉 - 在訪問bean之前調用'refresh'。在哪裏初始化beanfactory?

查找下面的jsp & XML文件以供參考。

的web.xml

<?xml version="1.0"?> 

<web-app xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4" id="WebApp_ID"> 
<display-name>Spring MVC</display-name> 

    <welcome-file-list> 
    <welcome-file>/WEB-INF/pages/hello.jsp</welcome-file> 
    </welcome-file-list> 
<servlet> 
<servlet-name>mvc-dispatcher</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
<load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>mvc-dispatcher</servlet-name> 
<url-pattern>/</url-pattern> 
</servlet-mapping> 

<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value> 
     /WEB-INF/mvc-dispatcher-servlet.xml, 
     /WEB-INF/spring-security.xml 
     </param-value> 
</context-param> 
<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> 
</web-app> 

彈簧的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.2.xsd"> 
    <http auto-config="true"> 
     <intercept-url pattern="/welcome*" access="ROLE_USER" /> 
     <http-basic /> 
    </http> 

    <authentication-manager> 
     <authentication-provider> 
      <user-service> 
      <user name="rahul" password="123" authorities="ROLE_USER" /> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> 
</beans:beans> 

MVC-調度-servlet.xml中

<?xml version="1.0"?> 
<beans xsi:schemaLocation=" http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
    xmlns="http://www.springframework.org/schema/beans"> 
<context:component-scan base-package="test"/> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/WEB-INF/pages/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basenames"> 
     <list> 
      <value>mymessages</value> 
     </list> 
    </property> 
</bean> 
</beans> 

的login.jsp

<%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld"%> 
<html> 
<head> 
<title>Login Page</title> 
<style> 
.errorblock { 
    color: #ff0000; 
    background-color: #ffEEEE; 
    border: 3px solid #ff0000; 
    padding: 8px; 
    margin: 16px; 
} 
</style> 
</head> 
<body onload='document.f.j_username.focus();'> 
    <h3>Login with Username and Password (Custom Page)</h3> 

    <c:if test="${SPRING_SECURITY_LAST_EXCEPTION !=null}"><!-- ${not empty error} --> 
     <div class="errorblock"> 
      Your login attempt was not successful, try again.<br /> Caused : 
      ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message} 
     </div> 
    </c:if> 

    <form name='f' action="<c:url value='j_spring_security_check' />" 
     method='POST'> 

     <table> 
      <tr> 
       <td>User:</td> 
       <td><input type='text' name='j_username' value=''> 
       </td> 
      </tr> 
      <tr> 
       <td>Password:</td> 
       <td><input type='password' name='j_password' /> 
       </td> 
      </tr> 
      <tr> 
       <td colspan='2'><input name="submit" type="submit" 
        value="submit" /> 
       </td> 
      </tr> 
      <tr> 
       <td colspan='2'><input name="reset" type="reset" /> 
       </td> 
      </tr> 
     </table> 
    </form> 
</body> 
</html> 

的hello.jsp

<%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld" %> 
<html> 
<body> 
    <h3>message : ${message}</h3> 
    <h3>User name : ${username}</h3> 
    <a href = "<c:url value="/j_spring_security_logout" />">LogOut</a> 
</body> 
</html> 

回到Home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
    <h3>Welcome to spring security</h3> 
    <a href="/SpringSecurity1-FORM/welcome"><b>Click here to logon</b></a> 
</body> 
</html> 

錯誤日誌

WARNING: Exception thrown from LifecycleProcessor on context close 
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date [Wed Jul 12 15:07:11 IST 2017]; root of context hierarchy 
    at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:416) 
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:997) 
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:956) 
    at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:581) 
    at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116) 
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4980) 
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5626) 
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) 
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

Jul 12, 2017 3:07:13 PM org.apache.catalina.core.StandardContext listenerStop 
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:170) 
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1028) 
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1004) 
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:956) 
    at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:581) 
    at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116) 
    at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4980) 
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5626) 
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) 
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
+0

請添加日誌。 –

+0

在spring web應用程序中,contextConfigLocation context-param給出了根上下文的位置。 Spring應用程序中的根上下文是ApplicationContext。所以如果你的問題是在哪裏初始化它,我會說在你的mvc-dispatcher-servlet.xml – RBz

+0

@ Sangam Belose:我附加了日誌。謝謝。 – user1608896

回答

0

@RBz:現在正在工作。如你所說, 1)我在mvc-dispatcher-servlet.xml中初始化了這個bean。

2)添加缺少的jar文件&添加了必要的相同版本的spring jar文件。

Regards

相關問題