【SSM】一、了解Sping 框架

打印 上一主题 下一主题

主题 938|帖子 938|积分 2814

〇、Maven

0.1 什么是Maven?


Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.
一、Spring

1.1什么是Spring?

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform.
Spring是分层的JavaSE/EEfull-stack 轻量级开源框架,Spring的两大特点是:IoC(Inverse of Control 控制反转),AOP(Aspect Oriented Programming 面向切面编程)
1.2 如何使用Maven加载Spring框架?

在pom.xml(maven的配置文件)中添加Maven所依赖的Jar的名称,也就是添加节点。
  1. <dependency>
  2.     <groupId>org.springframework</groupId>
  3.     <artifactId>spring-context</artifactId>
  4.     <version>5.3.16</version>
  5. </dependency>
复制代码
1.3 Spring 的核心容器

Spring容器会负责控制程序之间的关系,而不是由程序代码直接控制。Spring为我们提供了两种核心容器,分别为BeanFactory和ApplicationContext。
1.3.1 BeanFactory

创建BeanFactory实例时,需要提供Spring所管理容器的详细配置信息,这些信息通常采用XML文件形式来管理,其加载配置信息的语法如下:
  1. BeanFactory beanFactory =
  2.            new XmlBeanFactory(new FileSystemResource("F: /applicationContext.xml"));
  3. //F: /applicationContext.xml是XML配置文件的位置
复制代码
这种加载方式在实际开发中并不多用,了解即可。
1.3.2 ApplicationContext

ApplicationContext是BeanFactory的子接口,是另一种常用的Spring核心容器。它由org.springframework.context.ApplicationContext接口定义,不仅包含了BeanFactory的所有功能,还添加了对国际化、资源访问、事件传播等方面的支持。创建ApplicationContext接口实例,通常采用两种方法,具体如下:
1、通过ClassPathXmlApplicationContext创建
  1. ApplicationContext applicationContext =
  2.                                      new ClassPathXmlApplicationContext(String configLocation);
复制代码
ClassPathXmlApplicationContext会从类路径classPath中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。
2、通过FileSystemXmlApplicationContext创建
  1. ApplicationContext applicationContext =
  2.                                  new FileSystemXmlApplicationContext(String configLocation);
复制代码
FileSystemXmlApplicationContext会从指定的文件系统路径(绝对路径)中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。
1.3.3 ContextLoaderListener

Java项目中,会通过ClassPathXmlApplicationContext类来实例化ApplicationContext容器。而在Web项目中,ApplicationContext容器的实例化工作会交由Web服务器来完成。
Web服务器实例化ApplicationContext容器时,通常会使用ContextLoaderListener来实现,此种方式只需要在web.xml中添加如下代码:
  1.        <context-param>
  2.                <param-name>contextConfigLocation</param-name>
  3.                <param-value>
  4.                           classpath:spring/applicationContext.xml
  5.                </param-value>
  6.        </context-param>
  7.        <listener>
  8.                <listener-class>
  9.                          org.springframework.web.context.ContextLoaderListener
  10.                </listener-class>
  11.        </listener>
复制代码
1.4 什么是IoC(控制反转)?

1.4.1 依赖注入

DI的全称是Dependency Injection,中文称之为依赖注入。它与控制反转(IoC)的含义相同,只不过这两个称呼是从两个角度描述的同一个概念。
IoC:在使用Spring框架之后,对象的实例不再由调用者来创建(就是不需要像以前一样new一个对象了),而是由Spring容器来创建,Spring容器会负责控制程序之间的关系,而不是由调用者的程序代码直接控制。这样,控制权由应用代码转移到了Spring容器,控制权发生了反转,这就是控制反转。
DI:从Spring容器的角度来看,Spring容器负责将被依赖对象赋值给调用者的成员变量,这相当于为调用者注入了它依赖的实例(就是相当于为调用者无形中new好了这个对象),这就是Spring的依赖注入。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表