管理什么?(Service 与 Dao)
如何将被管理的对象告知IoC容器?(配置)
被管理的对象交给IoC容器,如何获取到IoC容器?(接口)
IoC容器得到后,如何从容器中获取bean?(接口方法)
两个实现类包的文件java代码为:
测试类文件中的java代码为:
package Application;
import dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import service.Impl.BookServiceImpl;
public class App_02 {
public static void main(String[] args) {
// 加载配置文件
ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取资源
// BookDaoImpl bookDao = (BookDaoImpl) apt.getBean("BookDao");
// bookDao.run();
BookServiceImpl bookService = (BookServiceImpl) apt.getBean("BookService");
bookService.run();
}
}
============================================
this is BookService
this is BookDao
Process finished with exit code 0
基于IoC管理bean
Service中使用new形式创建的Dao对象是否保留?(否)
Service中需要Dao对象如何进入到Service中?(提供方法)
在test文件中的java代码不需要做变化
package Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.Impl.BookServiceImpl;
public class App_02 {
public static void main(String[] args) {
// 加载配置文件
ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取资源
BookServiceImpl bookService = (BookServiceImpl) apt.getBean("bookService");
bookService.run();
}
}
=========================================
this is BookService
this is BookDao
Process finished with exit code 0
能够输出两条信息说明在BookDaoImpl与BookServiceImpl之间建立的连接没有问题