SpringMVC--整合Spring

整合Spring的问题

  1. 是否需要在项目之中添加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)

在这里插入图片描述

  • 示例:
  1. 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>
  1. 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容器的关系

在这里插入图片描述

在这里插入图片描述

  • 示例:现在有两个类

    1. HelloWorld:使用@Controller注解修饰,由SpringMVC容器管理
    2. 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"的实例

在这里插入图片描述

SpringMVC和Struts2对比

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值