title: Spring @Bean Name, is not working with @Qualifier
body: <p>I have a spring boot application. It creates LocalSessionFactoryBean Object and inject into session factory variable. It works fine as follow:</p>
  <pre><code>@Autowired
    @Qualifier(value=&quot;sessionFactory&quot;)
    private SessionFactory sessionFactory;
  
  
    @Primary
    @Bean
    public LocalSessionFactoryBean sessionFactory() {
      LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
      factory.setDataSource(dataSource);
      factory.setPackagesToScan(
          new String[] {
            &quot;com.abc..entities&quot;,
            &quot;com.abc.xyz.entities&quot;,
            &quot;
          });
      factory.setHibernateProperties(hibernateProperties());
      return factory;
    }
  </code></pre>
  <p>However, I need to change bean Name of sessionFactory, which is default as function name, from sessionFactory to entityManagerFactory, so I did following:</p>
  <pre><code>  @Autowired
    @Qualifier(value=&quot;entityManagerFactory&quot;)
    private SessionFactory sessionFactory;
  
  
    @Primary
    @Bean(name=&quot;entityManagerFactory&quot;)
    public LocalSessionFactoryBean sessionFactory() {
      LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
      factory.setDataSource(dataSource);
      factory.setPackagesToScan(
          new String[] {
            &quot;com.abc..entities&quot;,
            &quot;com.abc.xyz.entities&quot;,
            &quot;
          });
      factory.setHibernateProperties(hibernateProperties());
      return factory;
    }
  </code></pre>
  <p>But it throws error:</p>
  <pre><code>&gt; Error creating bean with name 'XYZ': Unsatisfied dependency expressed
  &gt; through field 'sessionFactory'; nested exception is
  &gt; org.springframework.beans.factory.NoSuchBeanDefinitionException: No
  &gt; qualifying bean of type 'org.hibernate.SessionFactory' available:
  &gt; expected at least 1 bean which qualifies as autowire candidate.
  &gt; Dependency annotations:
  &gt; {@org.springframework.beans.factory.annotation.Autowired(required=true),
  &gt; @org.springframework.beans.factory.annotation.Qualifier(value=entityManagerFactory)}
  </code></pre>
  <p>I believe i did it correctly, what did I miss here?</p>
  <p>Update :-</p>
  <p>I tried following as mentioned in one of answer but it did not work:</p>
  <pre><code>@Autowired
    @Qualifier(value = &quot;entityManagerFactory&quot;)
    private SessionFactory sessionFactory; 
  </code></pre>
  <p>and changed method as</p>
  <pre><code>@Primary
  @Bean
  
  public LocalSessionFactoryBean entityManagerFactory()
   { ..........},
  
  
  &gt;  Unsatisfied dependency expressed through field 'sessionFactory';
  &gt; nested exception is
  &gt; org.springframework.beans.factory.NoSuchBeanDefinitionException: No
  &gt; qualifying bean of type 'org.hibernate.SessionFactory' available:
  &gt; expected at least 1 bean which qualifies as autowire candidate.
  &gt; Dependency annotations:
  &gt; {@org.springframework.beans.factory.annotation.Autowired(required=true),
  &gt; @org.springframework.beans.factory.annotation.Qualifier(value=entityManagerFactory)}
  </code></pre>