通過 IoC 容器架構程序的分層實現,有兩種方式:1.基於 XML 配置文件 2.基於註解釋
使用典型的 MVC 模式後臺代碼分層結構:Controller 層調用 Service 層,Service 層調用 DAO 層,並且 Service 層和 DAO 層設計爲接口
1.基於 XML 配置文件
(1)創建 UserController 類:
public class UserController {
private UserService userService;
public User getUserById(int id){
return userService.getUserById(id);
}
}
題外話:Java中Class Type 類類型
java中數據類型分兩類: 基本類型,引用類型
(2)創建 UserService 接口以及實現類 UserServiceImpl:
public interface UserService {
public User getUserById(int id);
}
public class UserServiceImpl implements UserService{
private UserDAO userDAO;
@Override
public User getUserById(int id) {
// TODO Auto-generated method stub
return userDAO.getUserById(id);
}
}
未完待續