关于Spring-mvc的机制以及Ioc、DI的理解

Spring-mvc算得上是ssh框架中的管理者,是用来配置bean,并维护bean之间的关系的容器框架。beans是spring核心配置文件:applicationContext.xml的主角,程序中所有的对象都可以配置为bean并为其注入属性值,是实现IoC和DI的关键,下面先介绍一下Spring的工作机制:
这里写图片描述
一般来说,我们把一个项目分为Web层、业务层、Dao层和持久层,Web层主要是页面的展示以及动作的处理,可以认为涵盖了View层与Control层,业务层包含的主要是对象实体类,也就是常说的bean或者pojo,也有叫domain的,以及Service包;Dao层是定义数据库访问接口层,如果用到了hibernate框架,这里就是hibernateUtil发挥功能的地方。持久层的功能是将数据库中的数据固化在存储设备中,也就是hibernate工作的层次,主要是解决关系模型和对象模型之间的阻抗。业务层+Dao层+持久层就是MVC中常说的Model层,但是并非Model层一定会同时包含这三种层次,可根据自己的需要进行添加。MVC三层架构的理解并不是一概而论,各层的称呼也各有千秋,主要是方便自己理解就好。
Spring的作用主要是用来管理各个层次的组件,因为各层级之间交流依赖的对象,都被以bean的形式配置在spring的核心文件applicationContext.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:p="http://www.springframework.org/schema/p"
    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-3.1.xsd  
  http://www.springframework.org/schema/context  
  http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  http://www.springframework.org/schema/mvc  
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

  <bean id="test" class="com.Spring.Student.stu">
  <property name="name" value="David"></property>
  </bean>

  </beans>


<beans></beans>
标签是定义bean的起止标签,只有在这里定义的bean才有效。

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"

这段定义也是必须的,相当于beans的声明。

<bean id="test" class="com.Spring.Student.Test">
  <property name="name" value="David"></property>
  </bean>

称为一个bean,bean的id必须唯一,这个id名就是在程序中实例化的时候的对象名,class是这个bean的来源,必须带包名否则找不到,
id=”test” class=”com.Spring.Student.Test”这句话的意思就是在com.Spring.Student包下有一个类叫做Test,它被实例化了的时候,它的对象就叫test,也就是说,当程序实例化这个bean时,会产生类似如下效果:Test test=new Test();但程序中不需要这么写,所以这也是控制反转的原理。

 <property name="name" value="David"></property>

这句话的意思是说,这个Test类中有一个name属性,将这个name属性的值设置为Davide,这里需要注意的是,这个Test类中必须要存在name属性,并有这个属性的set和get方法,缺一不可,同理我们还可以注入其他属性,只要该类中存在这个属性及其get、set方法。如:

<property name="age" value="18"></property>
<property name="gender" value="male"></property>

从中我们就能窥到一些控制反转的定义所在了:
Ioc控制反转,即将创建及维护对象(bean)的权利交付给Spring框架而不是像传统的new方式在程序中创建,同时程序也不再负责维护对象,这种控制权的转移就是Spring框架的控制反转机制。
DI依赖注入,依赖注入实质上是和控制反转同样的道理,只是为了更加体现Spring的核心概念。

<property name="propertyname" value="propertyvalue"></property>

这种定义属性的方式就叫做注入,而bean和bean之间的关系不是独立的,可以通过引用而相互依赖,如在上面的bean中若有如下的一条属性注入:

<property name="Davide" ref="course"</property>

同时,配置文件中也要有course的定义:

<bean id="course" class="com.Spring.Student.Course">
  <property name="name" value="C++"></property>
  </bean>

相当于在Student类中引用了Course类的对象,那么test就依赖于course,因此注入依赖和控制反转表达的其实是一个意思只是理解的角度有所不同。
以上就是我在学习过程中对Spring-mvc和控制反转和依赖注入的理解,如果有更好的理解或者不同意见,欢迎交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值