Spring和SpringMVC整合

对于Spring和SpringMVC进行整合不一定需要:

1.对于不需要整合的做法,即不需要在web.xml中配置配置启动 Spring IOC 容器的 ContextLoaderListener ,只需web.xml配置springmvc的DispatcherServlet时,采用通配符的形式导入其他配置符。这个方法简单且不会出现bean对象被初始化两次的结果。其还有一种方式,就是直接在Springmvc的配置文件中通过标签导入其他ioc容器,整合成一个ioc

代码如下:


  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring-*.xml
    
    1
  
  
    SpringMVC
    
    /
  

2.对于spring和springmvc的采取整合的形式,就要在web.xml中配springmvc的DispatcherServlet同时也要配ContextLoaderLister


	
	
	
		contextConfigLocation
		classpath:beans.xml
	

	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		springDispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:springmvc.xml
		
		1
	

	
		springDispatcherServlet
		/
	
	

配置后这里将导入两个ioc容器导入,通常这两个ioc中有些bean是重合的。在启动项目后就会出现,同一个对象被初始化两次,好较大的性能。应就要为不同的ioc容器配置彼此容器的内容界限。其配置如下:

在springmvc配置文件中

		
		
	
beans文件中

		
		
	

	

注:这两个ioc容器间由包含其父子关系,springmvc的ioc相对于beans的ioc为子,beans为父。

因此,父ioc中的bean不能应用springmvcioc中的bean,其用@Autwire将会为null

但springmvc中的bean能引用beans的bean对象

你可能感兴趣的:(spring框架)