【发布时间】:2019-02-18 08:39:50
【问题描述】:
我有两个控制器。
我写了这个控制器,我必须在哪里写配置来纠正工作。
@Controller
public class BookController {
private BookService bookService;
@Autowired(required = true)
@Qualifier(value = "bookService")
public void setBookService(BookService bookService) {
this.bookService = bookService;
}
@RequestMapping(value = "books", method = RequestMethod.GET)
public String listBooks(Model model){
model.addAttribute("book", new Book());
model.addAttribute("listBooks", this.bookService.listBooks());
return "books";
}
}
@Controller("controller1")
public class AuthorController {
private AuthorService authorService;
@Autowired(required = true)
@Qualifier(value = "authorService")
public void setBookService(AuthorService authorService) {
this.authorService = authorService;
}
@RequestMapping
(value = "authors", method = RequestMethod.GET)
public String listAuthors(Model model){
model.addAttribute("author", new Author());
model.addAttribute("listAuthors", this.authorService.list());
return "";
}
}
【问题讨论】:
-
不需要写配置。您只需要
@Controller注释,并在您的dispatcher-servlet.xml中进行组件扫描。检查你的xml中是否有这个<context:component-scan base-package="path.to.your.controllers"/>。
标签: spring spring-mvc