Spring study notes 3

Spring Web development project and split the Spring configuration file
Spring Web Development Project

  • How Web project initialization SpringIOC container: idea: when the service starts (tomcat), by listening will SpringIOC container initialized once (the listener spring-web.jar already provided)

  • Thus a spring web projects at least 7 jar: spring-java 6 jar + spring-web.jar, Note: web project packet is stored into the jar WEB-INF / lib in

  • When the web project starts, it will automatically load web.xml, and therefore need to load the listener (ioc container initialization) in web.xml.

  • When the Web project started, start the instance of Ioc container:

<!-- 指定 Ioc容器(applicationContext.xml)的位置-->
  <context-param>
  		<!--  监听器的父类ContextLoader中有一个属性contextConfigLocation,该属性值 保存着 容器配置文件applicationContext.xml的位置 -->
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext.xml</param-value>
  </context-param>  
  <listener>
  	<!-- 配置spring-web.jar提供的监听器,此监听器 可以在服务器启动时 初始化Ioc容器。
  		初始化Ioc容器(applicationContext.xml) ,
  			1.告诉监听器 此容器的位置:context-param
  			2.默认约定的位置	:WEB-INF/applicationContext.xml
  	 -->
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

Split Spring configuration file
- java project:

	applicationContext1.xml
		applicationContext2.xml
		applicationContext3.xml

ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext3.xml") ;
  • Web Project:
    According to What split?
    i three-tier structure.
    the UI (HTML / CSS / JSP, Servlet) applicationController.xml
    Service: applicationService.xml
    Dao: applicationDao.xml
    public database: applicationDB.xml
    . ii functional structure of
    student-related configuration applicationContextStudent.xml
    class configuration applicationContextClass.xml

    Merge: How to load multiple profiles
    (1)

	  <context-param>
  		<!--  监听器的父类ContextLoader中有一个属性contextConfigLocation,该属性值 保存着 容器配置文件applicationContext.xml的位置 -->
  <param-name>contextConfigLocation</param-name>
  		<param-value>
  			classpath:applicationContext.xml,
  			classpath:applicationContext-Dao.xml,
  			classpath:applicationContext-Service.xml,
  			classpath:applicationContext-Controller.xml
  		</param-value>
  </context-param>

(2) Recommended

  <context-param>
  		<!--  监听器的父类ContextLoader中有一个属性contextConfigLocation,该属性值 保存着 容器配置文件applicationContext.xml的位置 -->
  		<param-name>contextConfigLocation</param-name>
  		<param-value>
  			classpath:applicationContext.xml,
  			classpath:applicationContext-*.xml
  		</param-value>
  </context-param>

(3) Only the main configuration file is loaded in web.xml

classpath: applicationContext.xml then ask Canada in the main configuration, load other configuration files
Published 41 original articles · won praise 1 · views 552

Guess you like

Origin blog.csdn.net/qq_41620020/article/details/105109482