package model;
public class DemoDAO {
public DemoDAO() {
System.out.println("DemoDAO's constructor");
}
}
範例一:
package model;
public class DemoService {
private DemoDAO dao =new DemoDAO();
}
範例一:
package org.iiiedu.samuel.ProjSpringDemo;
import model.DemoService;
public class TestDemoService {
public static void main(String[] args) {
DemoService service = new DemoService();
}
}
範例二:
package model;
public class DemoService {
private DemoDAO dao =null;
public DemoService(DemoDAO dao){
//我是建構子
this.dao=dao;
}
}
範例二:
package org.iiiedu.samuel.ProjSpringDemo;
import model.DemoDAO;
import model.DemoService;
public class TestDemoService {
public static void main(String[] args) {
DemoDAO dao = new DemoDAO();
DemoService service = new DemoService(dao);
}
}
範例三;
package model;
public class DemoService {
//範例二
//private DemoDAO dao =null;
//public DemoService(DemoDAO dao){
// //我是建構子
// this.dao=dao;
//}
private DemoDAO dao = null;
public void setDAO(DemoDAO dao) {
// 我是建構子
this.dao = dao;
}
}
範例三;
package org.iiiedu.samuel.ProjSpringDemo;
import model.DemoDAO;
import model.DemoService;
public class TestDemoService {
public static void main(String[] args) {
// 範例二
// DemoDAO dao = new DemoDAO();
// DemoService service = new DemoService(dao);
DemoDAO dao = new DemoDAO();
DemoService service = new DemoService();
service.setDAO(dao);
}
}
留言列表