设为首页 加入收藏

TOP

【SSM】一、了解Sping 框架
2023-07-25 21:30:02 】 浏览:33
Tags:SSM 了解 Sping 框架

〇、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的名称,也就是添加 节点。

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.16</version>
</dependency>

1.3 Spring 的核心容器

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

1.3.1 BeanFactory

创建BeanFactory实例时,需要提供Spring所管理容器的详细配置信息,这些信息通常采用XML文件形式来管理,其加载配置信息的语法如下:

BeanFactory beanFactory = 
           new XmlBeanFactory(new FileSystemResource("F: /applicationContext.xml"));
//F: /applicationContext.xml是XML配置文件的位置

这种加载方式在实际开发中并不多用,了解即可。

1.3.2 ApplicationContext

ApplicationContext是BeanFactory的子接口,是另一种常用的Spring核心容器。它由org.springframework.context.ApplicationContext接口定义,不仅包含了BeanFactory的所有功能,还添加了对国际化、资源访问、事件传播等方面的支持。创建ApplicationContext接口实例,通常采用两种方法,具体如下:

1、通过ClassPathXmlApplicationContext创建

ApplicationContext applicationContext =
                                     new ClassPathXmlApplicationContext(String configLocation);

ClassPathXmlApplicationContext会从类路径classPath中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。

2、通过FileSystemXmlApplicationContext创建

ApplicationContext applicationContext =
                                 new FileSystemXmlApplicationContext(String configLocation);

FileSystemXmlApplicationContext会从指定的文件系统路径(绝对路径)中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。

1.3.3 ContextLoaderListener

Java项目中,会通过ClassPathXmlApplicationContext类来实例化ApplicationContext容器。而在Web项目中,ApplicationContext容器的实例化工作会交由Web服务器来完成。

Web服务器实例化ApplicationContext容器时,通常会使用ContextLoaderListener来实现,此种方式只需要在web.xml中添加如下代码:

       <context-param>
               <param-name>contextConfigLocation</param-name> 
               <param-value>
                          classpath:spring/applicationContext.xml
               </param-value>
       </context-param> 
       <listener>
               <listener-class>
                         org.springframework.web.context.ContextLoaderListener
               </listener-class>
       </listener>

1.4 什么是IoC(控制反转)?

1.4.1 依赖注入

DI的全称是Dependency Injection,中文称之为依赖注入。它与控制反转(IoC)的含义相同,只不过这两个称呼是从两个角度描述的同一个概念。

IoC:在使用Spring框架之后,对象的实例不再由调用者来创建(就是不需要像以前一样new一个对象了),而是由Spring容器来创建,Spring容器会负责控制程序之间的关系,而不是由调用者的程序代码直接控制。这样,控制权由应用代码转移到了Spring容器,控制权发生了反转,这就是控制反转。

DI:从Spring容器的角度来看,Spring容器负责将被依赖对象赋值给调用者的成员变量,这相当于为调用者注入了它依赖的实例(就是相当于为调用者无形中new好了这个对象),这就是Spring的依赖注入。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Redis Plus 来了,性能炸裂! 下一篇Java 线程池之ThreadPoolExecutor..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目