kwh
kwh

Reputation: 664

Spring - How to reference a bean programmatically

I have my application config set-up programmatically, and I am importing a bean like this:

@Configuration
@ImportResource( value= { "classpath:myBean.xml"})
public class AppConfig extends WebMvcConfigurerAdapter
{

And in myBean.xml I have this:

  <bean id="myBeanId" class="my.domain.myBeanClass">
    <property name="sessionFactory" ref="my_session_factory" />
    <property name="someOtherProperty"...
  </bean>

This works fine and sessionFactory is injected into myBeanClass.

However if I try and instantiate that same bean programmatically, chanhing ImportResource to Import, I get "No matching bean of type [org.hibernate.SessionFactory] found for dependency..." error.

@Configuration
@Import(BeanConfig.class)
public class AppConfig extends WebMvcConfigurerAdapter
{

Bean Config Class:

@Configuration
public class BeanConfig
{
  @Autowired
  private SessionFactory sessionFactory;

  @Bean(name="myBeanId")
  public MyBeanClass createMyBeanClass()
  {
    MyBeanClass mbc = new MyBeanClass();
    mbc.setSessionFactory(sessionFactory);
    ....
    return mbc;

edit: The sessionFactory bean is definitely being created, if I add a required = false to @Autowired, and then manually inject the sessionFactory once everything is loaded. It works fine.

edit 2: I don't have a web.xml, I am using servlet 3 so have declared everything programmatically. This is my web.xml equivalent

@Configuration
public class WalletInitialiser implements WebApplicationInitializer
{

  @Override
  public void onStartup(ServletContext aServletContext) throws ServletException
  {
    AnnotationConfigWebApplicationContext mvcContext
      = new AnnotationConfigWebApplicationContext();

    mvcContext.register(AppConfig.class);
    mvcContext.scan("config.packages", "class.packages");
    aServletContext.addListener(new ContextLoaderListener(mvcContext));
    //add security filters, dispatcher to servlet, logback

I have my SessionFactory configured in another class, HibernateConfig in the config package, which is being picked up from

mvcContext.scan("config.packages", "class.packages");

Excerpt from this class is:

@Configuration
@EnableTransactionManagement
public class HibernateConfig
{
  @Bean(name="my_session_factory")
  public LocalSessionFactoryBean baseSessionFactory()
  {
    LocalSessionFactoryBean lsfb= new LocalSessionFactoryBean();
    lsfb.setPackagesToScan("class.packages");
    lsfb.setAnnotatedPackages("class.packages");

    //add hibernate props for datasource
    return lsfb;
  }
}

Upvotes: 1

Views: 6889

Answers (1)

Pyranja
Pyranja

Reputation: 3599

This problem may be related to How to make factoryBeans work.... Although it's not an exact match Configuring Hibernate Session Factory may give insights to a work around.

I would suggest trying this:

@Configuration
public class BeanConfig {
   @Autowired
   private LocalSessionFactoryBean sessionFactoryBean;

   @Bean(name="myBeanId")
   public MyBeanClass createMyBeanClass() {
      MyBeanClass mbc = new MyBeanClass();
      mbc.setSessionFactory((SessionFactory) sessionFactoryBean.getObject());
     ....
     return mbc;
   }
}

There may be other ways to fix this, I didn't tracked the JIRA issues mentioned in the spring source forum, they may point to a "standard" way. Alternatively the Spring documentation may give some insight into the handling of FactoryBeans in java config.

Upvotes: 1

Related Questions