Spring容器注入内部类

That's one caveat of Java - inner classes default constructor isn't a no-arg constructor. Their default constructor takes 1 parameter of type - the outer class.

So, use <constructor-arg> to pass a bean of type ExampleBean

Of course, use non-static inner classes only if this is necessary. If the class is only what you have shown, then make it static. 
Or move it to a new file. Then you won't have the above restriction. Preferring static inner classes is a good practice not only for spring beans, but in Java in general.



@Controller
@RequestMapping(value="/demo")
public class AppController{

 
@RequestMapping(value="/demo-method")
public String processer(HttpServletRequest  request , @ModelAttribute("demoDTO") DemoDTO demoDto){

  //Do something

}
//如上方法中需要应用内部类DemoDTO作为参数接收容器,因而xxxxx^^^ just do it like this.
    @Getter
    @Setter
    @Accessors(chain = true)
    @Access(AccessType.FIELD)
    @NoArgsConstructor
    static class DemoDTO {

        private Long id;
        private String address;
        private String tel;

    }

}

将内部类定义为static


另,若使用BeanUtils.copyProperties或PropertyUtils.copyProperties方法,建议使用Spring提供的,而非apach


另,若使用BeanUtils.copyProperties或PropertyUtils.copyProperties方法,建议使用Spring提供的,而非apache commons包下的,

因apache所提供的BeanUtils对内部类支持不是很好,无法正常拷贝相关属性;



转载于:https://my.oschina.net/xiehongfei/blog/650707

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值