简体   繁体   English

Spring-如何以编程方式引用bean

[英]Spring - How to reference a bean programmatically

I have my application config set-up programmatically, and I am importing a bean like this: 我以编程方式设置了应用程序配置,并且正在导入如下所示的bean:

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

And in myBean.xml I have this: myBean.xml我有这个:

  <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 . 这可以正常工作,并将sessionFactory注入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. 但是,如果我尝试以编程方式实例化该bean,将ImportResource更改为Import ,则会出现“找不到依赖项类型为[org.hibernate.SessionFactory]的匹配bean ...”错误。

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

Bean Config Class: Bean Config类别:

@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. 编辑:如果我在@Autowired中添加了required = false,则肯定会创建sessionFactory bean,然后在所有内容加载完毕后手动注入sessionFactory。 It works fine. 工作正常。

edit 2: I don't have a web.xml, I am using servlet 3 so have declared everything programmatically. 编辑2:我没有web.xml,我使用的是Servlet 3,因此已以编程方式声明了所有内容。 This is my web.xml equivalent 这是我的web.xml等效项

@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 我在另一个类HibernateConfig中的配置包中配置了我的SessionFactory,该类是从配置包中获取的

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;
  }
}

This problem may be related to How to make factoryBeans work... . 此问题可能与如何使factoryBeans工作...有关 Although it's not an exact match Configuring Hibernate Session Factory may give insights to a work around. 尽管不完全匹配,但是配置Hibernate Session Factory可能会为解决问题提供一些见识。

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. 可能还有其他方法可以解决此问题,我没有跟踪spring源论坛中提到的JIRA问题,它们可能指向“标准”方法。 Alternatively the Spring documentation may give some insight into the handling of FactoryBeans in java config. 另外,Spring文档可能会提供一些有关java config中FactoryBeans处理的见解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM