Spring Autowired null mapper

时间:2016-07-15 16:05:42

标签: java spring null autowired mapper

我是Spring(Boot,最新版本)的新手,我在REST服务中遇到了映射器问题。 我有一些UserMapper,它将模型用户对象(UserOP)映射到逻辑用户对象(User)。

用户有一个地址,我必须将其与用户一起映射。问题是,我没有成功地在UserMapper中正确地注入我的AddressMapper。

我试过以下但是mapperAddress是null(Spring编译,所以我猜它只是不做部分" mapperAddress = new AddressMapper()")。有人可以解释我为什么以及如何正确地做到这一点?我在网上搜索但不明白。

@Component public class UserMapper {

   public UserMapper() {
       super();
   }

   @Autowired
   private AddressMapper mapperAddress;

   public User modelToIo(UserOP userOp){
       User user = null;
       if (userOp != null){
           User = new User();
           user.setAddress(mapperAddress.modelToIo(userOp.getAddress()));
           ...
       }
   }
   ...
}

@Component
public class AddressMapper {

    public AddressMapper() {
        super();
    }
    ...
}

感谢。

0 个答案:

没有答案