springioc容器(
application context。xml中配置bean
@controller/service/resposityory/component
)
springioc初始化:将ioc容器中的所以bean实例化为对象(new对象)
将各个bean依赖的属性值注入进去(属性赋值)
java版:ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
spring开发web项目:
web项目如何初始化SpringIOC容器(因为web项目有多入口,所以不能用java方法):
思路:当服务启动时(tomcat),通过监听器将springIOC容器初始化一次(springweb.jar提供)
web项目启动时,自动加载web.xml,因此需要在web.xml加载监听器(ioc容器)
web的jar时存入web-inf的lib里的
配置文件的拆分
1.一个一个写
classpath:applicationContext.xml
classpath:applicationContext-Dao.xml
classpath:applicationContext-Service.xml
classpath:applicationContext-Controller.xml
classpath:applicationContext-*.xml
主context中<import resource="applicationContext-Service.xml"/>
1.三层结构:
ui(html/css/jsp、servlet)
Service
Dao
公共 数据库
2.功能结构:
学生
班级。。。。
两个容器:
IOC容器:
一群群bean
studentServlet(属性:xxService)
studentSetvice(属性:sutdentDao)
Servlet容器
web容器,tomcat容器
通过href标签访问Servlet容器:发送request请求(所以在servlet容器有,ioc没值)
bean的实例化、DI时保存在SpringIOC容器中
但是每一次请求request,时请求servlet容器
把ioc容器中的bean拿给servlet容器用(init方法)