title: No bean named 'myUserDetailsService' is defined Spring Security with hibernate and Spring MVC
body: <p>I am following <a href="http://www.mkyong.com/spring-security/spring-security-hibernate-xml-example/" rel="nofollow">this tutorial</a> but i need to make querys without the hbm.xml files and instead use dao and service layers. I have already generated models for both users and user_roles tables using hibernate. I also created the dao's and service layers.</p>
  
  <p>The problem is i get an error that says No bean named 'myUserDetailsService' is defined. I know i have to create a bean but i can't figure out how to do it.</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myUserDetailsService' is defined
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
      at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
      ... 93 more
  </code></pre>
  
  <p>I have uploaded the full project to github. Any help/ advice is apreciated.</p>
  
  <p><a href="https://github.com/kunal7L/Spring-MVC-Security-Hibernate" rel="nofollow">https://github.com/kunal7L/Spring-MVC-Security-Hibernate</a></p>
  
  <p>My beans:</p>
  
  <pre><code> &lt;bean id="userDao" class="com.mkyong.users.dao.UserDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /&gt;
      &lt;/bean&gt;
      &lt;bean id="userService" class="com.mkyong.users.service.UserServiceImpl"&gt;
          &lt;property name="userDao" ref="userDao"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;!--    &lt;bean id="myUserDetailsService" class="com.mkyong.users.service.MyUserDetailsService"&gt; --&gt;
  &lt;!--        &lt;property name="userDao" ref="userDao" /&gt; --&gt;
  &lt;!--    &lt;/bean&gt; --&gt;
  </code></pre>
  
  <p>Full MyUserDetailsService Class:</p>
  
  <p>public class MyUserDetailsService implements UserDetailsService {</p>
  
  <pre><code>private UserService userservice;
  
  @Override
  public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
  
      // Programmatic transaction management
      /*
      return transactionTemplate.execute(new TransactionCallback&lt;UserDetails&gt;() {
  
          public UserDetails doInTransaction(TransactionStatus status) {
              com.mkyong.users.model.User user = userDao.findByUserName(username);
              List&lt;GrantedAuthority&gt; authorities = buildUserAuthority(user.getUserRole());
  
              return buildUserForAuthentication(user, authorities);
          }
  
      });*/
  
      Users user = userservice.findByUserName(username);
      List&lt;GrantedAuthority&gt; authorities = buildUserAuthority(user.getUserRoleses());
  
      return buildUserForAuthentication(user, authorities);
  
  
  }
  
  // Converts com.mkyong.users.model.User user to
  // org.springframework.security.core.userdetails.User
  private User buildUserForAuthentication(Users user, List&lt;GrantedAuthority&gt; authorities) {
      return new User(user.getUsername(), user.getPassword(), user.isEnabled(), true, true, true, authorities);
  }
  
  private List&lt;GrantedAuthority&gt; buildUserAuthority(Set&lt;UserRoles&gt; userRoles) {
  
      Set&lt;GrantedAuthority&gt; setAuths = new HashSet&lt;GrantedAuthority&gt;();
  
      // Build user's authorities
      for (UserRoles userRole : userRoles) {
          setAuths.add(new SimpleGrantedAuthority(userRole.getRole()));
      }
  
      List&lt;GrantedAuthority&gt; Result = new ArrayList&lt;GrantedAuthority&gt;(setAuths);
  
      return Result;
  }
  </code></pre>
  
  <p>}</p>