Tag Archives: spring

Spring security remember me

Follow the post about Spring Security, in this post I will guide how to implement Spring Security remember me to your spring web application. Remember me is a convenient feature that allow user automatically logged to application without need to enter username and password.

Spring security provide two difference implement of remember me service Token-based remember me and Persistent remember me service Continue reading Spring security remember me

Maven+spring MVC: org.springframework.web.servlet.DispatcherServlet that does not implement interface javax.servlet.Servlet

1. Problem

When build maven web application, and after that added spring mvc dispatcherServlet for spring 4 i got this issue

"org.springframework.web.servlet.DispatcherServlet" that does not implement 
 interface javax.servlet.Servlet

This is web.xml file

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>SpringMVC4-Hibernate4</display-name>
    <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>spring</servlet-name>
      <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

2. Cause

You are missing servlet-api in maven dependency

3. Solution
Add following lines to your pom.xml

<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

the element <scope>provided</scope> that mean: this lib will provide at runtime by container check this link for more detail

spring: java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor

1- Problem

Using spring 4 and hibernate 3.6.10 and got this

Caused by: java.lang.NoClassDefFoundError: org/hibernate/engine/spi/SessionFactoryImplementor
  at org.springframework.orm.hibernate4.SessionFactoryUtils.(SessionFactoryUtils.java:92) ~[spring-orm-4.0.2.RELEASE.jar:4.0.2.RELEASE]
  at org.springframework.orm.hibernate4.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:335) ~[spring-orm-4.0.2.RELEASE.jar:4.0.2.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) ~[spring-beans-4.0.2.RELEASE.jar:4.0.2.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ~[spring-beans-4.0.2.RELEASE.jar:4.0.2.RELEASE]
  ... 28 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.spi.SessionFactoryImplementor
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) ~[catalina.jar:7.0.50]
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) ~[catalina.jar:7.0.50]
  ... 32 common frames omitted

2-Solution

Using right version Hibernate for transaction manager and Local session factory bean

  • org.springframework.orm.hibernate3.HibernateTransactionManager
  • org.springframework.orm.hibernate3.LocalSessionFactoryBean

 

<beans:bean id="sessionFactory" name="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <beans:property name="configLocation">
           <beans:value>classpath:hibernate.cfg.xml</beans:value>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>
<beans:bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>

Run Spring application offline

Dear all,

Today i will guide you about how to run Spring application offline or without internet connection. When you develop a spring application you will face with issue resolve, parser failure with spring xml config file when you don’t have internet connection. You  will get stuck here if not find the way to solve this.

Let me guide you through

1. Symptom

org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 147; schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans-3.1.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .
  at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
  at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaWarning(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument1(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
  at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
  at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)

Have you ever seen the above error? This is the issue i mention above when run Spring application offline.

2. Cause

This issue cause on your local application don’t have the file http://www.springframework.org/schema/beans/spring-beans-3.1.xsd, and application try to get this file on internet but you don’t have internet connection.

3. Solution

There are two option to resolve this issue:
a.Change namespace on your xml config
Change from this

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mail="http://www.springframework.org/schema/integration/mail"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail-2.2.xsd">
....
</beans>

to

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mail="http://www.springframework.org/schema/integration/mail"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:beans="http://www.springframework.org/schema/beans"
  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.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail-2.2.xsd">
....
</beans>

b.Download  your xml schema( the xsd file missing and put to classpath) change xml config file

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mail="http://www.springframework.org/schema/integration/mail"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xsi:schemaLocation="http://www.springframework.org/schema/beans <strong>classpath:spring-beans-3.1.xsd</strong>
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd">