(修改完成)sping 梳理2--IoC控制反转 + spring简单体验 + 简单的代码解释
IOC 容器
首先聊聊控制反转
- 这不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控制权,交由Spring框架来管理。
- 以往的思路:若要使用某个对象,需要自己去负责对象的创建
- 反转的思路:若要使用某个对象,只需要从 Spring 容器中获取需要使用的对象,不关心对象的创建过程,也就是把创建对象的控制权反转给了Spring框架
一个例子
家政服务为例:
过年了,今天张三需要给家里打扫个卫生,于是想请几个钟点工来擦擦玻璃。
解决方案有几种:
1、自己主动的去,询问查找,看看谁认识钟点工,有了联系方式后,自己打电话邀约,谈价钱。
2、直接打电话给家政公司,提出要求即可。 第一种方式,就是我们之前接触的创建对象的方式,自己主动new,而第二种就是spring给我们提供的另外一种方 式叫IOC,家政公司就像是一个大容器,能为我们提供很多的服务。
聊聊例子中的问题
一定会有人问,那家政公司从哪里来啊!
1、自行构建 我们可以使用配置文件,或者注解的方式定义一下咱们自己的容器里存放的东西。
2、使用别人的 一定会有很多有钱人,成立自己的各类公司,他们的这些服务都可以集成在咱们的容器里,为我们提供很多强大的 功能,比如spring自带的就很多template模板类。
操练一把
目录:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>ssm</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- 测试相关 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- springmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!--文件上传--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <!-- fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.68</version> </dependency> <!-- mybatis 相关 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!-- 数据库连接驱动 相关 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- 提供了对JDBC操作的完整封装 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.10.RELEASE</version> </dependency> <!-- 织入 相关 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <!-- spring,mybatis整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> <!-- 集成德鲁伊使用 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.18</version> </dependency> <!--集成日志--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.11.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> </dependencies> <build> <plugins> <!-- 配置jdk --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> <!-- 资源路径 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
beans.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="user" class="com.xinzhi.entity.User"> <property name="name" value="孙锐"/> </bean> </beans>
User.java
package com.xinzhi.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * @author sr * @date 2021/1/24 */ @Data @AllArgsConstructor @NoArgsConstructor public class User { private String name; }
测试 TestFirst.java
从容器里面拿,而不是new
import com.xinzhi.entity.User; import javafx.application.Application; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.jws.soap.SOAPBinding; /** * @author sr * @date 2021/1/24 */ public class TestFirst { @Test public void testCreateBean(){ //加载配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); User user = applicationContext.getBean("user", User.class); System.out.println(user); } }
注:
ClassPathXmlApplicationContext("beans.xml") 是在这里找的。
结果:
XML Schema命名空间作用:
1. 避免命名冲突,像Java中的package一样
2. 将不同作用的标签分门别类(像Spring中的tx命名空间针对事务类的标签,context命名空间针对组件的标签)
代码解释:
- xmlns="http://www.springframework.org/schema/beans" 声明xml文件默认的命名空间,表示未使用其他 命名空间的所有标签的默认命名空间。
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 声明XML Schema 实例,声明后就可以使用 schemaLocation 属性了
- xmlns:aop="http://www.springframework.org/schema/aop" 声明前缀为aop的命名空间,后面的URL用于 标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。当命名空 间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。
- xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.or g/schema/beans/spring-beans-3.0.xsd
这个从命名可以看出个大概,指定Schema的位置这个属性必须结合命名空间使用。这个属性有两个值,第一个值 表示需要使用的命名空间。第二个值表示供命名空间使用的 XML schema 的位置
详细说说其中的几个对象
ApplicationContext是spring继BeanFactory之外的另一个核心接口或容器,允许容器通过应用程序上下文环境创建、获取、管理bean。为应用程序提供配置的中央接口。在应用程序运行时,是只读的。
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver { }
一个ApplicationContext 提供
- 访问应用程序组件的Bean工厂方法。从org.springframework.beans.factory.ListableBeanFactory继承。
- 以通用方式加载文件资源的能力。继承自ResourcePatternResolver 接口。
- 向注册侦听器发布事件的能力。继承自ApplicationEventPublisher接口。
- 解析消息的能力,支持国际化。继承自MessageSource接口。
ConfigurableApplicationContext:
该 接口 提供了根据配置创建、获取bean的一些方法,其中主要常用的实现包括: ClassPathXmlApplicationContext、FileSystemXmlApplicationContext等。
提供了通过各种途径去加载实例化 bean的手段。
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable { }
ClassPathXmlApplicationContext是ConfigurableApplicationContext的一个实现
该类提供了很多记载资源的方式,该类可以加在类路劲下的xml资源文件,当然如果想加在其他地方的资源,可以 使用FileSystemXmlApplicationContext这个类。
public ClassPathXmlApplicationContext(String configLocation) throws BeansException { this(new String[] {configLocation}, true, null); }