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包下的, java

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

相关文章
相关标签/搜索