Java学习笔记-Day63 Spring 框架(一)



一、Spring 框架简介


SSM框架集(Spring、Spring MVC、MyBatis)

JavaEE(Enterprice Edition)被更名为 jakartaEE(雅加达)。

1、简介


Spring是一个开源框架,是于2003 年兴起的一个开源、轻量级的 Java 开发框架。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。

Spring框架主要由七部分组成,分别是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web 和 Spring Web MVC。

Spring Core:Core是核心工具包。
Spring AOP:在Spring中,AOP提供面向切面编程的支持。
Spring ORM:Spring管理其他数据访问框架模块的技术。

Spring核心接口或类:BeanFactory、BeanWrapper、ApplicationContext。

简单来说,Spring是一个轻量级的控制反转(IOC/DI)和面向切面(AOP)的容器框架。 同时还提供了如下功能:
在这里插入图片描述

2、核心容器


(1)Core模块:封装了框架依赖的最底层部分,包括资源访问、类型转换及一些常用工具类。

(2)Beans模块:提供了框架的基础部分,包括反转控制和依赖注入。其中Bean Factory是容器核心(工厂),本质是“工厂设计模式”的实现,而且无需编程实现“单例设计模式”,单例完全由容器控制,而且提倡面向接口编程,而非面向实现编程;所有应用程序对象及对象间关系由框架管理,从而真正把你从程序逻辑中把维护对象之间的依赖关系提取出来,所有这些依赖关系都由BeanFactory来维护。

(3)Context模块:以Core和Beans为基础,集成Beans模块功能并添加资源绑定、数据验证、国际化、Java EE支持、容器生命周期、事件传播等;核心接口是ApplicationContext。

(4)EL模块:提供强大的表达式语言支持,支持访问和修改属性值,方法调用,支持访问及修改数组、容器和索引器,命名变量,支持算数和逻辑运算,支持从Spring 容器获取Bean,它也支持列表投影、选择和一般的列表聚合等。

(5)AOP模块:Spring AOP模块提供了符合 AOP Alliance规范的面向方(切)面的编程(aspect-oriented programming)实现,提供比如日志记录、权限控制、性能统计等通用功能和业务逻辑分离的技术,并且能动态的把这些功能添加到需要的代码中;这样各专其职,降低业务逻辑和通用功能的耦合。

(6)DataACCESS模块:Spring本身的 JDBC 和其他的框架整合 ORM(Mybatis Hibernate)。

(7)WEB模块:Web的部分(Spring MVC)和其他框架(如Struts2)整合的部分。

3、Spring的作用


Spring能让程序员简化开发:

(1)Spring根据配置文件来进行创建及组装对象间依赖关系(IOC),只需要改配置文件即可,无需重新编译。Spring能帮我们根据配置文件创建及组装对象之间的依赖关系。

(2)重复业务逻辑的处理,在AOP思想中,Spring 面向切面编程能帮助我们无耦合的实现日志记录,性能统计,安全控制等。

(3)原始的支持jdbc事务处理繁琐,Spring能非常简单的帮我们管理数据库事务。

(4)Spring还提供了与第三方ORM框架无缝集成,而且自己也提供了一套JDBC访问模板,来方便数据库访问。

(5)Spring还提供与第三方Web(如Struts、JSF)框架无缝集成,而且自己也提供了一套Spring MVC框架,来方便web层搭建。

(6)Spring能方便的与Java EE(如Java Mail、任务调度)整合,与更多技术整合(比如缓存框架)。

4、Spring的优点


非常轻量级的容器,低侵入,代码污染低。独立于各种应用服务器。DI(IOC)降低了业务对象替换复杂性。spring调度。

5、Spring的下载


Spring官网下载:https://repo.spring.io/libs-release-local/org/springframework/

github下载:https://github.com/spring-projects/spring-framework/releases

(1)进入spring目录。

在这里插入图片描述
(2)进入 5.1.1.RELEASE 目录。
在这里插入图片描述
(3)选择 spring-framework-5.1.1.RELEASE-dist.zip 压缩包文件。
在这里插入图片描述
(4)解压 spring-framework-5.1.1.RELEASE-dist.zip 。
在这里插入图片描述

在这里插入图片描述

二、Spring 框架的操作步骤


(1)建立新的项目,导入需要的jar包(commons-logging-1.1.3.jar 可以去 mvn 网站下载)。

在这里插入图片描述
(2)创建实体类,实体类需要实现Serializable接口。

package com.etc.blog.entity;
import java.io.Serializable;

public class Blog implements Serializable {
    
    
	private int id;
	private String title;
	private String content;
	public Blog(int id, String title, String content) {
    
    
		super();
		this.id = id;
		this.title = title;
		this.content = content;
	}
	public Blog() {
    
    
		super();
		System.out.println("blog无参数构造");
	}
	public int getId() {
    
    
		return id;
	}
	public void setId(int id) {
    
    
		System.out.println("setId=" + id);
		this.id = id;
	}
	public String getTitle() {
    
    
		return title;
	}
	public void setTitle(String title) {
    
    
		this.title = title;
	}
	public String getContent() {
    
    
		return content;
	}
	public void setContent(String content) {
    
    
		this.content = content;
	}
	@Override
	public String toString() {
    
    
		return "Blog [id=" + id + ", title=" + title + ", content=" + content + "]";
	}
}

(3)创建Spring的全局配置文件,将该文件放置src目录中。src目录右键 -> New -> Other -> Spring Bean Configuration file (插件)。

  • xsd文件 (XML结构定义,描述XML文档的结构)
  • XML文件(可扩展标记语言)

创建该类对象时会调用该类的无参构造方法。属性赋值会调用该类属性的setter方法。class属性的值要完整的包名+类名。

标签中的属性id与name作用是一样,区别在于id中不可以含有特殊字符,而name中可以有特殊字符。id是确定该bean的唯一属性,而name可以指定一个或者多个名称,各个名称之间用逗号或分号分开。后面的为bean的别名。

标签中value是字面值,ref是引用值(引用对象)。

<?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">
	<!-- class="com.etc.blog.entity.Blog" class中要求写完整的包名和类名 -->
	<bean class="com.etc.blog.entity.Blog" id="blog1">
		<!-- 属性赋值 ,在bean类中要有setter方法和gtter方法 -->
		<property name="id" value="1"></property>
		<property name="title" value="特里克堡"></property>
		<property name="content" value="特里克堡....."></property>
		<property name="author" ref="author1"></property>
	</bean>
	<bean class="com.etc.blog.entity.Blog" id="blog2">
		<property name="id" value="2"></property>
		<property name="title" value="确诊病例"></property>
		<property name="content" value="确诊病例....."></property>
		<property name="author" ref="author1"></property>
	</bean> -->
	<bean class="com.etc.blog.entity.Author" id="author1">
		<!-- 属性赋值 ,在bean类中有setter -->
		<property name="id" value="1"></property>
		<property name="name" value="小明"></property>
	</bean>
</beans>

(4)通过java代码获取bean对象

1、实例化ApplicationContext(应用上下文),用其获取Bean对象。
方式一: ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

方式二:BeanFactory context = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
此处的classpath代表的是src目录。

2、通过ApplicationContext接口getBean方法从Spring容器中获取Bean对象。
① 通过 Bean对象的id或者name来获取:
Object getBean(String name) 根据名称返回一个Bean,客户端需要自己进行类型转换;参数name可以是bean的属性id的值或者是name的值。

	Blog blog = (Blog) context.getBean("blog1");

② 通过 Bean对象的类名来获取:
T getBean(Class<T> requiredType) 根据指定的类型返回一个Bean,客户端无需自己进行类型转换,但是如果全局配置文件中没有或多于一个Bean存在将会抛出异常。

	Blog blog = context.getBean(Blog.class);

③ 通过 Bean对象的id或者name和类名来获取:
T getBean(String name, Class<T> requiredType) 根据名称和指定的类型返回一个Bean,客户端无需自己进行类型转换,如果类型转换失败,容器抛出异常。返回的是指定名称同时指定类型的bean对象。

	Blog blog = context.getBean("blog1", Blog.class);

猜你喜欢

转载自blog.csdn.net/qq_42141141/article/details/112915100