整合Spring的问题
- 是否需要在项目之中添加Spring的IOC容器?
- 需要:可以将一些配置写在Spring的配置文件中(例如:数据源的配置,Service或DAO的Bean),于SpringMVC区分开来.
- 不需要:可以定义多个SpringMVC的配置文件,而后在web.xml文件中使用通配符"*",一起导入SpringMVC多个配置文件.
- 结论:最还是导入Spring的原生IOC容器,将Service和DAO的Bean配置写在Spring原生的IOC容器中,而SpringMVC容器只配置Handler的Bean,这样结构更加的清晰
整合SpringMVC解决方法
发现问题
-
当spring的IOC容器配置和SpringMVC的IOC容器配置的基包重复时,包中的类会被重复创建多次,为了避免这样的情况发生,可以利用以下标签解决:
- <context:exclude-filter>标签:不扫描某些注解
- <context:includel-filter>标签:扫描某些注解
-
因为大部分情况下,SpringMVC最大的作用就是用于页面的跳转,所以SpringMVC容器只需要使用@Controller"标签即可
-
因此springmvc的配置中,需要设置 “use-default-filters=“false””,表示不使用默认的注解(例如:@Session,@Component)
- 示例:
- spring,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.xsd">
<context:component-scan base-package="mao.shu.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>
</beans>
- springmvc_config.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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="mao.shu.springmvc">
<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>
- 在web.xml文件中加入Spring的原生IOC容器
- 在web.xml文件中还需要配置Spring的监听器加载,否则无法启动Spring的IOC容器
<!--配置Spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置Spring的IOC容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
SpringMVC容器和Spring容器的关系
-
示例:现在有两个类
- HelloWorld:使用@Controller注解修饰,由SpringMVC容器管理
- UserService:使用@Service 注解修饰:由Spring容器管理.
-
在HelloWorl中添加一个 UserService的引用,使用@Autowired注解自动注入
-
并在sayHellWorld()方法中访问这个实例
@Controller
public class Helloworld {
public Helloworld() {
System.out.println("Helloworld Constructor() ");
}
@Autowired
private UserService userService;
@RequestMapping("/sayHelloWorld")
public ModelAndView sayHelloWorld(@RequestParam("info")String info){
System.out.println(this.userService);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("helloworld");
return modelAndView;
}
}
- 测试结果:可以正常访问
- 示例:由UserService访问HelloWorld实例
@Service
public class UserService {
@Autowired
private Helloworld helloworld;
public UserService() {
System.out.println(helloworld);
System.out.println("UserService Constructor()");
}
}
- 测试结果:出现异常,无法找到"userService"的实例