Spring開發web專案 web專案初始化Spring IOC容器: 當服務啟動時,通過監聽器初始化一次(Spring-web.jar已經提供), 需要7個jar:spring-java6個jar+Spring-web.jar, web專案啟動時,會自動加載web.xml,因此在web.xml中加載監聽器,(IOC容器初始化)
<!--IOC容器的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--通過監聽器初始化一次(Spring-web.jar已經提供)-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
拆分Spring組態檔 將多個組態檔加載: (1)
<!--IOC容器的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/applicationContext.xml
WEB-INF/applicationContext-Dao.xml
WEB-INF/applicationContext-Service.xml
WEB-INF/applicationContext-Controller.xml
</param-value>
</context-param>
(2)推薦
<context-param>
<param-name>contextConfigLocation</param-name>
<!--加載多個組態檔-->
<param-value>
WEB-INF/applicationContext.xml
WEB-INF/applicationContext-*.xml
</param-value>
</context-param>
(3)在主組態檔中加載其他檔案
<import resource="applicationContext-Controller.xml"/> <import resource="applicationContext-Service.xml"/> <import resource="applicationContext-Dao.xml"/>
servlet容器與ioc容器連接問題,

//在初始化時獲取ioc容器中的物件
@Override
public void init() throws ServletException {
// ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-Service.xml");
//web專案獲取背景關系物件
ApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
studentService= (IStudentService) context.getBean("studentService");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/158665.html
標籤:Java
