Using BeanUtils.copyProperties from a Mybatis created object to a Spring Bean, the properties are null in the Spring bean. Why?

huangapple go评论122阅读模式
英文:

Using BeanUtils.copyProperties from a Mybatis created object to a Spring Bean, the properties are null in the Spring bean. Why?

问题

问题出在我遇到的地方我有一个类似这样的类
@Component
@RequestScope
public class TableModel{

    private Integer tableField1;

    ...
    //other properties, getters and setters here

}

Mybatis从映射到该对象的查询中返回结果我希望将此对象的结果复制到另一个类中的Spring bean就像这样

public class MyClass {

      TableModel model

      @Autowired
      MyClass(TableModel model) {
           this.model = model;
           //...
      }

      //some code

      TableModel result = MyMapperInterface.selectFromTable();
      //这里,result.tableField1不是0或null

      BeanUtils.copyProperties(result, model);

      //在copyProperties()之后,model.tableField1仍然为null
      //...     

}

然而在BeanUtils.copyProperties()调用之后model中的属性仍然为null我仔细检查过**没有**使用Apache Commons版本的copyProperties我正在使用Spring版本的BeanUtils问题是什么
英文:

The problem I am encountering is that I have a class that looks something like this

@Component
@RequestScope
public class TableModel{

    private Integer tableField1;

    ...
    //other properties, getters and setters here

}

Mybatis returns results from queries mapped to this object, and I wish to copy the result from this object to my Spring bean in a different class, like so:

public class MyClass {

      TableModel model

      @Autowired
      MyClass(TableModel model) {
           this.model = model;
           //...
      }

      //some code

      TableModel result = MyMapperInterface.selectFromTable();
      //Here, result.tableField1 is not 0 or null

      BeanUtils.copyProperties(result, model);

      //After copyProperties(), model.tableField1 is still null
      //...     

}

However, after the BeanUtils.copyProperties() call, the properties in model are still null. I double checked, and I am not using the Apache Commons version of copyProperties, I am using the Spring version of BeanUtils. What is the issue?

答案1

得分: 1

@RequestScope 表示每个请求都会获得一个不同的实例。从语义上讲,这与将其作为构造函数参数传递(这意味着它在该 bean 的所有用户之间是共享的)不符。

即便如此,Spring 会尝试通过为你的 TableModel 注入一个 代理 来使其在请求上下文中根据需要进行切换。可能正在发生的情况是,底层实例并不相同。

通常要避免使用请求范围的 bean。不要为每个请求的数据使用注入;而是应该使用方法参数。(我建议使用 MapStruct 而不是 BeanUtils,因为它既更快,又更安全。)

英文:

@RequestScope means that you get a different instance for every request. Semantically, this does not match with passing it as a constructor parameter (meaning it's shared for all users of that bean).

Even so, Spring will try to make that work for you by injecting a proxy for your TableModel that will magically switch it around depending on the request context. What's probably happening is that your underlying instance is not the same.

Avoid request-scoped beans generally. Don't use injection for per-request data; use method parameters instead. (And I suggest using MapStruct instead of BeanUtils, as it's both faster and safer.)

huangapple
  • 本文由 发表于 2020年4月9日 08:43:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61112233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定