title: failed to lazily initialize a collection of role custom UserDetailsService
body: <p>I am trying to customize <code>UserDetailsService</code>, my class is:</p>
  
  <pre><code>@Service
  public class UserService implements UserDetailsService {
      @Override
  //    @Transactional
      public UserPrincipal loadUserByUsername(String username) {
          UserPrincipal principal = userRepository.getByUsername(username);
          // make sure the authorities and password are loaded
          principal.getAuthorities().size();
          principal.getPassword();
          return principal;
      }
  }
  </code></pre>
  
  <p>and my <code>UserPrincipal</code> is:</p>
  
  <pre><code>@Entity
  @Table
  public class UserPrincipal implements UserDetails, CredentialsContainer, Cloneable {
  ...
      private Set&lt;UserAuthority&gt; authorities = new HashSet&lt;&gt;();
  
      @Override
      @ElementCollection(fetch = FetchType.LAZY)
      @CollectionTable(name = "UserPrincipal_Authority", joinColumns = {
              @JoinColumn(name = "UserId", referencedColumnName = "UserId")
      })
      public Set&lt;UserAuthority&gt; getAuthorities()
      {
          return this.authorities;
      }
  ...
  }
  </code></pre>
  
  <p>Config:</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:context="http://www.springframework.org/schema/context"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xmlns:jpa="http://www.springframework.org/schema/data/jpa"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-4.0.xsd
              http://www.springframework.org/schema/jee
              http://www.springframework.org/schema/jee/spring-jee.xsd
              http://www.springframework.org/schema/data/jpa
              http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"&gt;
  
      &lt;import resource="securityConfig.xml" /&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com"&gt;
          &lt;context:exclude-filter type="annotation"
                  expression="org.springframework.stereotype.Controller" /&gt;
      &lt;/context:component-scan&gt;
  
      &lt;!-- Configure the data source bean --&gt;
      &lt;jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/test"/&gt;
  
      &lt;!-- Create default configuration for Hibernate --&gt;
      &lt;bean id="hibernateJpaVendorAdapter"
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
  
      &lt;!-- Configure the entity manager factory bean --&gt;
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
          &lt;property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/&gt;
          &lt;!-- Set JPA properties --&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5InnoDBDialect&lt;/prop&gt;
                  &lt;prop key="javax.persistence.schema-generation.database.action"&gt;none&lt;/prop&gt;
                  &lt;!--&lt;prop key="hibernate.ejb.use_class_enhancer"&gt;true&lt;/prop&gt;--&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;!-- Set base package of your entities --&gt;
          &lt;property name="packagesToScan" value="com.security"/&gt;
          &lt;!-- Set share cache mode --&gt;
          &lt;property name="sharedCacheMode" value="ENABLE_SELECTIVE"/&gt;
          &lt;!-- Set validation mode --&gt;
          &lt;property name="validationMode" value="NONE"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Configure the transaction manager bean --&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Enable annotation driven transaction management --&gt;
      &lt;tx:annotation-driven/&gt;
  
      &lt;!--
        Configure Spring Data JPA and set the base package of the
        repository interfaces
      --&gt;
      &lt;jpa:repositories base-package="com.security"
                        entity-manager-factory-ref="entityManagerFactory"
                        transaction-manager-ref="transactionManager"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>When the <code>@Transactional</code> is commented out I get</p>
  
  <blockquote>
    <p>Reason: failed to lazily initialize a collection of role: <code>com.security.UserPrincipal.authorities</code>, could not initialize proxy - no Session"</p>
  </blockquote>
  
  <p>which makes sense. When I add the <code>@Transactional</code> I get this error:</p>
  
  <blockquote>
    <p>No qualifying bean of type [<code>com.security.UserService</code>] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.</p>
  </blockquote>
  
  <p>The code used to work, I can't figure out what I have changed that broke the code.</p>
  
  <p>This is the full exception:</p>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityController' defined in file [C:\Users\nfallah\Documents\workspace-intelliJ\Spring-Hybrid-Config - MVC - CSR - Secure - DB\target\spring-hybrid-config-1.0.0-SNAPSHOT\WEB-INF\classes\com\security\SecurityController.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [com.security.UserService]: : No qualifying bean of type [com.security.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.security.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
      at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:742) ~[ConstructorResolver.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:196) ~[ConstructorResolver.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114) ~[AbstractAutowireCapableBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1017) ~[AbstractAutowireCapableBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) ~[AbstractAutowireCapableBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) ~[AbstractAutowireCapableBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) ~[AbstractBeanFactory$1.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[DefaultSingletonBeanRegistry.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) ~[AbstractBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) ~[AbstractBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700) ~[DefaultListableBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) ~[AbstractApplicationContext.class:4.0.1.RELEASE]
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) ~[AbstractApplicationContext.class:4.0.1.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658) ~[FrameworkServlet.class:4.0.1.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624) ~[FrameworkServlet.class:4.0.1.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672) ~[FrameworkServlet.class:4.0.1.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543) ~[FrameworkServlet.class:4.0.1.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) [FrameworkServlet.class:4.0.1.RELEASE]
      at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) [HttpServletBean.class:4.0.1.RELEASE]
      at javax.servlet.GenericServlet.init(GenericServlet.java:158) [servlet-api.jar:3.1.FR]
      at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1241) [catalina.jar:8.0.8]
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1154) [catalina.jar:8.0.8]
      at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041) [catalina.jar:8.0.8]
      at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4922) [catalina.jar:8.0.8]
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5204) [catalina.jar:8.0.8]
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.0.8]
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724) [catalina.jar:8.0.8]
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700) [catalina.jar:8.0.8]
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697) [catalina.jar:8.0.8]
      at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1588) [catalina.jar:8.0.8]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_05]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_05]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_05]
      at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_05]
      at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) [tomcat-coyote.jar:8.0.8]
      at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [?:1.8.0_05]
      at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [?:1.8.0_05]
      at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:463) [catalina.jar:8.0.8]
      at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:413) [catalina.jar:8.0.8]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_05]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_05]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_05]
      at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_05]
      at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) [tomcat-coyote.jar:8.0.8]
      at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) [?:1.8.0_05]
      at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) [?:1.8.0_05]
      at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466) [?:1.8.0_05]
      at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) [?:1.8.0_05]
      at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307) [?:1.8.0_05]
      at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1399) [?:1.8.0_05]
      at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:828) [?:1.8.0_05]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_05]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_05]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_05]
      at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_05]
      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323) [?:1.8.0_05]
      at sun.rmi.transport.Transport$1.run(Transport.java:178) [?:1.8.0_05]
      at sun.rmi.transport.Transport$1.run(Transport.java:175) [?:1.8.0_05]
      at java.security.AccessController.doPrivileged(Native Method) [?:1.8.0_05]
      at sun.rmi.transport.Transport.serviceCall(Transport.java:174) [?:1.8.0_05]
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:557) [?:1.8.0_05]
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:812) [?:1.8.0_05]
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:671) [?:1.8.0_05]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_05]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_05]
      at java.lang.Thread.run(Thread.java:745) [?:1.8.0_05]
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.security.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100) ~[DefaultListableBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960) ~[DefaultListableBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855) ~[DefaultListableBeanFactory.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:806) ~[ConstructorResolver.class:4.0.1.RELEASE]
      at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:734) ~[ConstructorResolver.class:4.0.1.RELEASE]
      ... 65 more
  </code></pre>