真正的問題是,爲什麼我能夠在這兩個方法來軟件寫此代碼:@autowired註釋如何工作?
@Controller
public class PostController {
@Autowired
private PostService postService;
@Autowired
private CommentService commentService;
....
}
和
@Controller
public class PostController {
private PostService postService;
private CommentService commentService;
@Autowired
public PostController(PostService postService, CommentService commentService){
this.postService = postService;
this.commentService = commentService;
}
....
}
是那些片段相同?推薦的是什麼?