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

相關文章
相關標籤/搜索