Spring 框架

1.1.1 1. Spring 框架的概述

Spring 是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的 松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。

Spring 的核心是控制反转(IoC 控制反转)和面向切面(AOP)。简单来说,Spring 是一个分层的 JavaSE/EEfull-stack(一站式) 轻量级开源框架。
1.1.2 2. Spring 框架的优点

1.方便解耦,简化开发,Spring 就是一个大工厂,可以将所有对象创建和依赖关 系维护,交给 Spring 管理。IOC 的作用。

2.AOP 编程的支持,Spring 提供面向切面编程,可以方便的实现对程序进行权限 拦截、运行监控等功能。(可扩展性)

3.声明式事务的支持,只需要通过配置就可以完成对事务的管理,而无需手动编 程。

4.方便程序的测试,Spring 对 Junit4 支持,可以通过注解方便的测试 Spring 程 序。

5.方便集成各种优秀框架,Spring 不排斥各种优秀的开源框架,其内部提供了对 各种优秀框架(如:Struts2 、Hibernate 、MyBatis 、Quartz 等)的直接支持。

6.降低 JavaEE API 的使用难度,Spring 对 JavaEE 开发中非常难用的一些 API (JDBC、JavaMail、远程调用等),都提供了封装,使这些 API 应用难度大大降 低。

1.2 Spring IOC 核心技术

1.2.1 1. 什么是 IOC

IOC -- Inverse of Control ,控制反转,将对象的创建权力反转给 Spring 框架!!

控制反转(Inversion of Control ,缩写为 IoC),是面向对象编程中的一种设计 原则,可以用来减低计算机代码之间的耦合度。

解决问题:使用IOC 可以解决的程序耦合性高的问题!!Spring 的工厂读取配 置文件。

1.2.2 2. IOC 的程序入门

创建 Java 工程,导入坐标依赖

java 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>j unit</groupId>
        <artifactId>j unit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

编写接口和实现类,编写具体的实现方法

java 复制代码
package com.qcby jy.service;

public interface UserService {

    public void hello();

}


package com.qcby jy.service;

public class UserServiceImpl implements UserService {
    @Override
    public void hello() {
        System.out.println("Hello IOC!!");
    }
}

编写 Spring 核心的配置文件,在 src 目录下创建 applicationContext.xml 的配置 文件,名称是可以任意的,但是一般都会使用默认名称。

java 复制代码
<?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.xsd">

<!--IOC 管理 bean-->
        <bean id="userService" class="com.qcbyjy.service.UserServiceImpl"
    />

</beans>

把 log4j.properties 的配置文件拷贝到 resources 目录下,做为 log4j 的 日志配置 文件。

编写测试方法。

java 复制代码
package com.qcby jy.test;

import com.qcby jy.service.UserService;
import org.j unit.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo1 {

/**
* 入门程序
*/
    @Test
    public void run1(){
        // 使用 Spring 的工厂
        ApplicationContext applicationContext = new
        ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过工厂获得类 :
        UserService userService = (UserService)
        applicationContext.getBean("userService");
        userService.hello();
    }

}

1.2.3 3. IOC 技术总结

ApplicationContext 接 口,工厂的接 口,使用该接口可以获取到具体的 Bean 对

象。该接口下有两个具体的实现类。 springCould 配置中心

ClassPathXmlApplicationContext ,加载类路径下的 Spring 配置文件。

FileSystemXmlApplicationContext ,加载本地磁盘下的 Spring 配置文件。

相关推荐
demo007x5 小时前
如何让 Podman 使用国内镜像源,这是我见过最牛的方法
后端·程序员
再睡一夏就好5 小时前
【C++闯关笔记】STL:deque与priority_queue的学习和使用
java·数据结构·c++·笔记·学习·
疯狂踩坑人5 小时前
别再说我不懂Node"流"了
后端·node.js
aricvvang5 小时前
🚀 NestJS 使用 cache-manager-redis-store 缓存无效?真相在这里!
javascript·后端·nestjs
SimonKing5 小时前
【开发者必备】Spring Boot 2.7.x:WebMvcConfigurer配置手册来了(一)!
java·后端·程序员
oak隔壁找我5 小时前
Java Collection 包使用指南
java·后端
Hero | 柒5 小时前
设计模式之单例模式
java·单例模式·设计模式
哈哈哈哈~5 小时前
Java中的单例模式
java·单例模式