MyBatis+Spring - Retrieve VOs as "beans"

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

MyBatis+Spring - Retrieve VOs as "beans"

Vladimir Alarcon
My sqlSession.selectList() method is retrieving a list of VOs.

Do you guys know if it's possible for these VOs to be instantiated by
Spring? I would like them to be able to control Spring SQL
transactions.

So far my VOs look like:

  @Component
  @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  public class AccountVO {

    @Autowired
    private InvoiceController invoiceController;

  }

When I run the SELECT using MyBatis+Spring, the returned AccountVO
"autowired properties" are not populated as I wished, but always null.

Any insight would be greatly appreciated.

Thanks in advance.
Vlad

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAP3Y0VaHJDctWxFo5wJRtKO0QDsAAaRdEpNHuupreu5bf-QhqA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: MyBatis+Spring - Retrieve VOs as "beans"

Iwao AVE!

Hi Vlad,

I would like them to be able to control Spring SQL transactions.

I am not sure what that means, but it is possible to let MyBatis use Spring managed beans.

You need to create a custom ObjectFactory that looks up ApplicationContext.
The create method would look as follows.

public <T> T create(Class<T> type) {
  try {
    return applicationContext.getBean(type);
  } catch (NoSuchBeanDefinitionException e) {
    return delegate.create(type);
  }
}

delegate is an instance of DefaultObjectFactory.

Set this ObjectFactory to your SqlSessionFactoryBean.

@Bean
public SqlSessionFactoryBean sqlSessionFactory() {
  SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  sessionFactory.setObjectFactory(new SpringBeanObjectFactory(applicationContext));
  ...

Here is an executable demo:
https://github.com/harawata/mybatis-issues/tree/master/ml-20190524T021118

Regards,
Iwao


On Fri, May 24, 2019 at 11:11 PM Vladimir Alarcon <[hidden email]> wrote:
My sqlSession.selectList() method is retrieving a list of VOs.

Do you guys know if it's possible for these VOs to be instantiated by
Spring? I would like them to be able to control Spring SQL
transactions.

So far my VOs look like:

  @Component
  @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  public class AccountVO {

    @Autowired
    private InvoiceController invoiceController;

  }

When I run the SELECT using MyBatis+Spring, the returned AccountVO
"autowired properties" are not populated as I wished, but always null.

Any insight would be greatly appreciated.

Thanks in advance.
Vlad

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CAP3Y0VaHJDctWxFo5wJRtKO0QDsAAaRdEpNHuupreu5bf-QhqA%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CA%2Buep2Qeb9cX2oWsno0Sh_AmVo%2BzP_frq8kBkCP9sJPhJ8M_ng%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: MyBatis+Spring - Retrieve VOs as "beans"

Kazuki Shimizu
Be careful a performance degradation when use the spring managed prototype bean.

On Tuesday, May 28, 2019 at 10:52:10 PM UTC+9, Iwao AVE! wrote:

Hi Vlad,

I would like them to be able to control Spring SQL transactions.

I am not sure what that means, but it is possible to let MyBatis use Spring managed beans.

You need to create a custom <a href="https://github.com/mybatis/mybatis-3/blob/master/src/main/java/org/apache/ibatis/reflection/factory/ObjectFactory.java" style="background-color:transparent;text-decoration:none" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fmybatis%2Fmybatis-3%2Fblob%2Fmaster%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fibatis%2Freflection%2Ffactory%2FObjectFactory.java\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFntmf2dj-3F6fVSlagQRCDb2pN-w&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fmybatis%2Fmybatis-3%2Fblob%2Fmaster%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fibatis%2Freflection%2Ffactory%2FObjectFactory.java\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFntmf2dj-3F6fVSlagQRCDb2pN-w&#39;;return true;">ObjectFactory that looks up ApplicationContext.
The create method would look as follows.

public <T> T create(Class<T> type) {
  try {
    return applicationContext.getBean(type);
  } catch (NoSuchBeanDefinitionException e) {
    return delegate.create(type);
  }
}

delegate is an instance of DefaultObjectFactory.

Set this ObjectFactory to your SqlSessionFactoryBean.

@Bean
public SqlSessionFactoryBean sqlSessionFactory() {
  SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  sessionFactory.setObjectFactory(new SpringBeanObjectFactory(applicationContext));
  ...

Here is an executable demo:
<a href="https://github.com/harawata/mybatis-issues/tree/master/ml-20190524T021118" style="background-color:transparent;text-decoration:none" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fharawata%2Fmybatis-issues%2Ftree%2Fmaster%2Fml-20190524T021118\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHToOCQv25Ddw5KYORGwD3xz0QlTA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fharawata%2Fmybatis-issues%2Ftree%2Fmaster%2Fml-20190524T021118\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHToOCQv25Ddw5KYORGwD3xz0QlTA&#39;;return true;">https://github.com/harawata/mybatis-issues/tree/master/ml-20190524T021118

Regards,
Iwao


On Fri, May 24, 2019 at 11:11 PM Vladimir Alarcon <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ewKdEZiyAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">vladi...@...> wrote:
My sqlSession.selectList() method is retrieving a list of VOs.

Do you guys know if it's possible for these VOs to be instantiated by
Spring? I would like them to be able to control Spring SQL
transactions.

So far my VOs look like:

  @Component
  @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
  public class AccountVO {

    @Autowired
    private InvoiceController invoiceController;

  }

When I run the SELECT using MyBatis+Spring, the returned AccountVO
"autowired properties" are not populated as I wished, but always null.

Any insight would be greatly appreciated.

Thanks in advance.
Vlad

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ewKdEZiyAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybati...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/mybatis-user/CAP3Y0VaHJDctWxFo5wJRtKO0QDsAAaRdEpNHuupreu5bf-QhqA%40mail.gmail.com" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/mybatis-user/CAP3Y0VaHJDctWxFo5wJRtKO0QDsAAaRdEpNHuupreu5bf-QhqA%40mail.gmail.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/mybatis-user/CAP3Y0VaHJDctWxFo5wJRtKO0QDsAAaRdEpNHuupreu5bf-QhqA%40mail.gmail.com&#39;;return true;">https://groups.google.com/d/msgid/mybatis-user/CAP3Y0VaHJDctWxFo5wJRtKO0QDsAAaRdEpNHuupreu5bf-QhqA%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/07b124d1-4525-4270-a657-caa26c63f5f6%40googlegroups.com.