framework学习笔记day05---Spring基础

本文详细介绍了Spring框架,包括其作为轻量级JavaEE框架的核心优势,如控制反转(IOC)和面向切面编程(AOP)。通过实例讲解了解耦策略,如工厂模式、反射和XML配置,并探讨了Spring的bean生命周期、多bean处理以及scope属性的应用。此外,还涵盖了Spring整合日志框架logback的配置和使用。
摘要由CSDN通过智能技术生成

Spring

Spring简介

spring是分层的JavaSE及JavaEE应用于全栈的轻量级开源框架,以IoC(Inverse Of Control:控制反转/反转控制)和AOP(Aspact Oriented Programming:面向切面编程)为核心,提供了表现层SpringMVC和持久层Spring
JDBC以及业务层事务管理等众多模块的企业级应用技术,还能整合开源世界中众多著名的第三方框架和类库,逐渐成为使用最多的JavaEE企业应用开源框架。

SSH(struts2 spring hibernate)

SSM(springmvc spring mybatis)

Spring的本质是管理软件中的对象,即创建对象维护对象之间的关系

Spring的优势(特点)

  • (1)方便解耦,简化开发
  • (2)AOP编程的支持
  • (3)声明式事务的支持
  • (4)方便程序的测试
  • (5)方便集成各种优秀框架
  • 6)降低JavaEEAPI的使用难度
  • (7)Spring框架原码是经典学习范例

spring的架构

Spring 最初的目标就是要整合一切优秀资源,然后对外提供一个统一的服务。Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式,如下图所示:

    • image-20211224101457106

组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下:

模块 说明
核心容器Spring Core 核心容器,提供Spring框架的基本功能。核心容器的主要组件是BeanFactory,它是工厂模式的实现。BeanFactory 使用控制反转(IOC)模式,将应用程序的配置和依赖性规范与实际的应用程序代码分开。
Spring Context Spring上下文,是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。
Spring AOP 通过配置管理特性,Spring AOP 模块直接将面向切面的编程功能集成到了 Spring 框架中。可以很容易地使 Spring框架管理的任何对象支持AOP。Spring AOP模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,就可以将声明性事务管理集成到应用程序中。
Spring DAO JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。
Spring ORM Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括JDO、Hibernate和iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。
Spring Web Web上下文模块建立在应用程序上下文模块之上,为基于 Web 的应用程序提供了上下文。所以Spring 框架支持与 Jakarta Struts的集成。Web模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。
Spring MVC框架 MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP、Velocity、Tiles、iText 和 POI。

程序的耦合

耦合性(Coupling),也叫耦合度,是对模块间关联程度的度量。耦合的强弱取决于模块间接口的复杂性、调用模块的方式以及通过界面传送数据的多少。模块间的耦合度是指模块之间的依赖关系,包括控制关系、调用关系、数据传递关系。模块间联系越多,其耦合性越强,同时表明其独立性越差(降低耦合性,可以提高其独立性)。耦合性存在于各个领域,而非软件设计中独有的,但是我们只讨论软件工程中的耦合。

总结:在软件工程中,耦合指的就是指对象之间的依赖关系。对象之间的依赖程度越高,耦合度就越高。对象之间的耦合越高,维护成本越高。因此对象的设计应使类和构件之间的耦合最小。

耦合降低程序之间的依赖程度,即降低程序之间的耦合度的过程就叫做解耦

三层结构的耦合性问题

代码实现

public class UserController {
   

    public static void main(String[] args) throws Exception {
   
        UserService userService = new UserServiceImpl();
        User user = userService.selectUserById(1);
        System.out.println("user = " + user);
    }

}
public class UserServiceImpl implements UserService {
   
    

    public User selectUserById(int userId) throws Exception {
   
        UserMapper userMapper = new UserMapperImpl();
        return userMapper.selectUserById(userId);
    }
}
public class UserMapperImpl implements UserMapper {
   
    public User selectUserById(int userId) throws Exception {
   
        return new User(userId, "zhangsan", "zhangsan");
    }
}
  • 存在问题
    • ①在UserController中创建UserService对象,如果UserService类的构造器发生变化,UserController也会随着变化;
    • ②通过new方式创建对象需要导入正确的包路径,否则编译不通过就报错。
  • 解决方案
    • ①使用工厂模式;
    • ②使用反射、XML配置文件

工厂模式解耦

  • 解耦程序编写步骤:
    • 1、 创建一个Maven的Java工程
    • 2、创建持久层接口和接口实现类
    • 3、创建表现层测试程序并运行测试程序
    • 4、创建表现层测试程序(com.tedu.controller.EmpController)并运行测试程序
    • 5、通过工程+配置文件+接口(已有)方式解耦
    • 6、使用工厂获取service接口和dao接口的实例,替换使用new的方式获取接口的实例。

代码实现

public class MyApplicationContext {
   


    public  Object getBean(String beanName) {
   
        if (beanName.equals("userService")) {
   
            return new UserServiceImpl();
        } else if (beanName.equals("userMapper")) {
   
            return new UserMapperImpl();
        } else if (beanName.equals("user")) {
   
            return new User(1, "zhangsan", "zhangsan");
        }
        return null;
    }

}
public class UserController {
   

    public static void main(String[] args) throws Exception {
   
        UserService userService = (UserService) new MyApplicationContext()
                .getBean("userService");
        User user = userService.selectUserById(1);
        System.out.println("user = " + user);
    }

}```

```bash
public class UserServiceImpl implements UserService {
   


    public User selectUserById(int userId) throws Exception {
   

        UserMapper userMapper = (UserMapper) new MyApplicationContext()
                .getBean("userMapper");
        return userMapper.selectUserById(userId);
    }
}
public class UserMapperImpl implements UserMapper {
   
    public User selectUserById(int userId) throws Exception {
   
        return (User) new MyApplicationContext()
                .getBean("user");
    }
}

存在问题

  • if…else…if太多了,难以维护;
  • 通过new方式创建对象需要导入正确的包路径,否则编译不通过就报错。

解耦方案使用反射

  • 代码实现
public class MyApplicationContext {
   


    public  Object getBean(String className) {
   
        Object obj = null;
        try {
   
            obj = Class.forName(className).newInstance();
        } catch (Exception e) {
   
            e.printStackTrace();
        }
        return obj;
    }

}
public class UserController {
   

    public static void main(String[] args) throws Exception {
   
        UserService userService = (UserService) new MyApplicationContext()
                .getBean("com.atguigu.service.impl.UserServiceImpl");
        User user = userService.selectUserById(1);
        System.out.println("user = " + user);
    }

}
public class UserServiceImpl implements UserService {
   


    public User selectUserById(int userId) throws Exception {
   

        UserMapper userMapper = (UserMapper) new MyApplicationContext()
                .getBean
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值