虽然spring 框架包含了spring mvc的框架,可以在一个xml文件中进行配置,但在实际工程应用中往往是分开的,在一个工程中既配置spring的配置文件,又配置spring mvc的配置文件会发生什么事情呢?以示例演示HelloWorld:工程中创建了controller层和service层,发送一个请求,映射到controller进行处理
1、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!--配置启动spring IOC容器的Listener-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<!--配置spring mvc的视图解析器,用于解析映射请求的视图-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2、spring mvc的配置文件springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!--配置spring mvc扫描的包-->
<context:component-scan base-package="com.lzj.springmvc"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--当没有请求映射时,采取直接连接资源模式-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
3、spring的配置文件bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--配置spring扫描的包-->
<context:component-scan base-package="com.lzj.springmvc"></context:component-scan>
</beans>
4、下面模拟一个service层
package com.atguigu.springmvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public UserService() {
System.out.println("UserService Constructor...");
}
}
5、下面模拟一个controller层
package com.atguigu.springmvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
/*把spring IOC容器中的bean注入到spring mvc容器中的bean*/
@Autowired
private UserService userService;
public HelloWorld() {
System.out.println("HelloWorld Constructor...");
}
@RequestMapping("/helloworld")
public String hello(){
System.out.println("success");
System.out.println(userService);
return "success";
}
}
6、新建一个请求index.jsp
<a href="helloworld">Test HelloWorld</a>
当发送index.jsp中的helloworld请求后,controller层的hello会处理请求。但是在发送请求前,启动工程的时候,会默认初始化两次UserService和HelloWorld,因为各执行了两次构造器。输出如下:
因为在springmvc.xml和bean.xml的配置文件中各配了扫描的包,他们扫描的包相同,因此初始化了两次。但实际我只想让springmvc.xml扫描controller层,而bean.xml扫描service层,controller层和service都建在了同一个包下,如何解决初始化两次的问题?
解决方法:
只让springmvc.xml扫描包下的controler层,扫描标签如下配置:
<!--use-default-filters默认为true的,表示扫描包下的所有Bean,如果指定为false,就只能扫描指定的bean了-->
<context:component-scan base-package="com.lzj.springmvc" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
只让bean.xml扫描service层,扫描标签如下:
<!--没有配use-default-filters,则默认为true,可以扫描包下的所有bean,除了controller标注的注解bean-->
<context:component-scan base-package="com.lzj.springmvc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
配置后,spring的IOC容器就和spring mvc的IOC容器不在重合。此时再重新加载工程,发现就只初始化一次bean了。
目录结构:
spring的IOC容器和spring mvc的IOC容器之间的关系:
如果把UserService代码改成如下方式,增加了一个HelloWorld的注入
package com.lzj.springmvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private Helloworld helloWorld;
public UserService(){
System.out.println("UserService...");
}
}
当重新加载工程时,出现异常,提示UserService 找不到依赖的helloWorld Bean。为什么会出现这种问题?
spring 的IOC容器代表父容器,spring mvc的IOC容器代表子容器,子容器是可以访问到父容器中的Bean的,父容器是访问不到子容器中的Bean的。关系如下