title: Unable to implement SpringSecurity-Hibernate simplest example
body: <p>I was going through following post <a href="https://stackoverflow.com/questions/4613678/spring-login-form-example">Spring login form example</a> and many other post of Spring Security to create a login example but I was not able to prepare any simple example.</p>
  
  <p>I was trying to incorporate solution from this post <a href="https://stackoverflow.com/questions/4613678/spring-login-form-example">Spring login form example</a>
  but problem is that I want Hibernate session factory to be injected in the UserDAO so that I can write query to get userName from table. @Autowire is not working
  so I used </p>
  
  <pre><code>&lt;context:annotation-config /&gt;
      &lt;context:component-scan
          base-package="com.tcs.ceg" /&gt;
  </code></pre>
  
  <p>but now i am getting runtime exception as cannot autowire sessionFactory as no matching bean found.
  But a bean with this name I have created in my applicationContext.xml .
  Can anyone tell me how can I inject sessionFactory?
  my application-security.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          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.1.xsd
          "&gt;
     &lt;context:annotation-config /&gt;
      &lt;context:component-scan
          base-package="com.tcs.ceg" /&gt;
  
  
      &lt;global-method-security pre-post-annotations="enabled" /&gt;
  
      &lt;http pattern="/css/**" security="none"/&gt;
      &lt;http pattern="/images/**" security="none"/&gt;
      &lt;http pattern="/js/**" security="none"/&gt;
      &lt;http pattern="/index.jsp" security="none"/&gt;
      &lt;http pattern="/loggedout.jsp" security="none"/&gt;
  
      &lt;http use-expressions="true"&gt;
          &lt;!--
               Allow all other requests. In a real application you should
               adopt a whitelisting approach where access is not allowed by default
            --&gt;
          &lt;intercept-url pattern="/**" access="isAuthenticated()" /&gt;
          &lt;form-login /&gt;
          &lt;logout logout-success-url="/loggedout.jsp" delete-cookies="JSESSIONID"/&gt;
          &lt;remember-me /&gt;
  
      &lt;/http&gt;
  
      &lt;beans:bean id="myUserService" class="com.tcs.ceg.services.impl.UserServiceImpl" /&gt;
      &lt;authentication-manager&gt;
      &lt;authentication-provider user-service-ref="myUserService" /&gt;
      &lt;/authentication-manager&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>and my applicationContext.xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:lang="http://www.springframework.org/schema/lang"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:util="http://www.springframework.org/schema/util"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" &gt;
  
      &lt;context:annotation-config /&gt;
   &lt;mvc:annotation-driven /&gt;
      &lt;context:component-scan
          base-package="com.tcs.ceg" /&gt;
  
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass"&gt;
          &lt;value&gt;
              org.springframework.web.servlet.view.tiles2.TilesView
          &lt;/value&gt;
      &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="tilesConfigurer"
      class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"&gt;
      &lt;property name="definitions"&gt;
          &lt;list&gt;
              &lt;value&gt;/WEB-INF/tiles.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
      &lt;property name="basename" value="classpath:messages" /&gt;
      &lt;property name="defaultEncoding" value="UTF-8"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="localeChangeInterceptor"
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
      &lt;property name="paramName" value="lang" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver"&gt;
      &lt;property name="defaultLocale" value="en"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="handlerMapping"
      class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt;
      &lt;property name="interceptors"&gt;
          &lt;ref bean="localeChangeInterceptor" /&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  
  &lt;jee:jndi-lookup id="dataSource1" jndi-name="jdbc/PmdDS"/&gt;
  
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource1" /&gt;
          &lt;property name="configLocation"&gt;
              &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="configurationClass"&gt;
              &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="current_session_context_class"&gt;thread&lt;/prop&gt;
                  &lt;prop key="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/prop&gt;
  
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
  
      &lt;tx:annotation-driven /&gt;
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
      &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
          &lt;!-- one of the properties available; the maximum file size in bytes --&gt;
          &lt;property name="maxUploadSize" value="1000000000000"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Code for UserServiceImpl</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.userdetails.User;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  
  import com.tcs.ceg.dao.intrface.UserDao;
  @Service
  public class UserServiceImpl implements UserDetailsService {
      @Autowired
      private transient UserDao userDao;//userDao is null
  
      public void setUserDao(UserDao userDao) {
              this.userDao = userDao;
          }
  
      @Override
      @Transactional
      public UserDetails loadUserByUsername(String username)
              throws UsernameNotFoundException {
          // TODO Auto-generated method stub
          User user=null;
          try{
              user = userDao.getUser(username);
          }catch(Exception err){
              err.printStackTrace();
          }
          if (user != null) {
  
              // convert roles
  
              // initialize user
            return user;
          } else {
              throw new UsernameNotFoundException("No user with username '" + username + "' found!");
          }
      }
  
  }
  </code></pre>
  
  <p>Code for UserDaoImpl</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collection;
  
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.GrantedAuthorityImpl;
  import org.springframework.security.core.userdetails.User;
  import org.springframework.stereotype.Repository;
  
  
  import com.tcs.ceg.dao.intrface.UserDao;
  import com.tcs.ceg.util.lib.DbComparisonException;
  @Repository 
  public class UserDaoimpl implements UserDao {
      @Autowired
      private SessionFactory sessionFactory;//sessionfactory is null
      @Override
      public User getUser(String username) throws DbComparisonException {
          String password = "rajesh";
          boolean enabled = true;
          boolean accountNonExpired = true;
          boolean credentialsNonExpired = true;
          boolean accountNonLocked = true;
  
          Collection&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;GrantedAuthority&gt;();
  
            authorities.add(new GrantedAuthorityImpl("admin"));
  
  
          User user = new User(username, password, enabled,
            accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
          return user;
      }
  
  }
  </code></pre>
  
  <p>Please note i havent written following line in UserDaoimpl because sessionFactory is null</p>
  
  <pre><code>sessionFactory.getCurrentSession().createQuery("from User").list();//throws null pointer exception
  </code></pre>