Spring5 框架
目录
2、Spring 可以解决企业应用开发的复杂性3、Spring 有两个核心部分:IOC 和 Aop
1、Spring 框架概述
Spring是由Rod Johnson组织和开发的一个分层的JavaSE/EE full-stack(一站式)轻量级开源框架,它以IoC(Inversion of Control 控制反转)和AOP(Aspect Oriented Programming 面向切面编程)为内核,使用基本的JavaBean来完成以前只可能由EJB(Enterprise Java Beans,即Java企业Bean)完成的工作,取代了EJB的臃肿、低效的开发模式。
Spring致力于Java EE应用各层的解决方案,在表现层它提供了Spring MVC以及与Struts框架的整合功能;在业务逻辑层可以管理事务,记录日志等;在持久层可以整合MyBatis、Hibernate、JdbcTemplate等技术。因此,可以说Spring是企业应用开发很好的“一站式”选择。虽然Spring贯穿于表现层、业务逻辑层和持久层,但它并不想取代那些已有的框架,而是以高度的开放性与它们进行无缝整合。
1、Spring 是轻量级的开源的 JavaEE 框架
2、Spring 可以解决企业应用开发的复杂性
3、Spring 有两个核心部分:IOC 和 Aop
(1)IOC:控制反转,把创建对象过程交给 Spring 进行管理
(2)Aop:面向切面,不修改源代码进行功能增强
4、Spring 特点
(1)方便解耦、简化开发
Spring就是一个大工厂,可以将所有对象的创建和依赖关系的维护工作都交给Spring容器管理,大大的降低了组件之间的耦合性。
(2)支持AOP
Spring提供了对AOP的支持,它允许将一些通用任务,如安全、事务、日志等进行集中式处理,从而提高了程序的复用性。
(3)方便程序的测试
Spring提供了对Junit4的支持,可以通过注解方便的测试Spring程序。
(4)方便集成各种优秀框架
Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持。
(5)方便进行事务操作
(6)降低 API 开发难度
5、体系结构
Spring框架采用的是分层架构,它一系列的功能要素被分成20个模块,这些模块大体分为Core Container、Data Access/Integration、Web、AOP(Aspect Oriented Programming)、Instrumentation、Messaging和Test,如图1所示。
图1 Spring的体系结构
在图1中,包含了Spring框架的所有模块,其中,灰色背景模块为本书中所涉及的主要模块。接下来分别对体系结构中的模块作用进行简单介绍,具体如下。
1、 Core Container(核心容器)
Spring的核心容器是其他模块建立的基础,它主要由Beans模块、Core模块、Context模块、Context-support模块和SpEL(Spring Expression Language,Spring表达式语言)模块组成,具体介绍如下:
● Beans模块:提供了BeanFactory,是工厂模式的经典实现,Spring将管理对象称为Bean。
● Core核心模块:提供了Spring框架的基本组成部分,包括IoC和DI功能。
● Context 上下文模块:建立在Core和Beans模块的基础之上,它是访问定义和配置的任何对象的媒介。其中ApplicationContext接口是上下文模块的焦点。
● Context-support模块:提供了对第三方库嵌入Spring应用的集成支持,比如缓存(EhCache、Guava、JCache)、邮件服务(JavaMail)、任务调度(CommonJ、Quartz)和模板引擎(FreeMarker、JasperReports、速率)。
● SpEL模块:是Spring3.0后新增的模块,它提供了Spring Expression Language支持,是运行时查询和操作对象图的强大的表达式语言。
2、 Data Access/Integration(数据访问/集成)
数据访问/集成层包括JDBC、ORM、OXM、JMS和Transactions模块,具体介绍如下:
● JDBC模块:提供了一个JDBC的抽象层,大幅度的减少了在开发过程中对数据库操作的编码。
● ORM模块:对流行的对象关系映射API,包括JPA、JDO和Hibernate提供了集成层支持。
●OXM模块:提供了一个支持对象/ XML映射的抽象层实现,如JAXB、Castor、XMLBeans、JiBX和XStream。
● JMS模块:指Java消息传递服务,包含使用和产生信息的特性,自4.1版本后支持与Spring-message模块的集成。
● Transactions事务模块:支持对实现特殊接口以及所有POJO类的编程和声明式的事务管理。
3、 Web
Spring的Web层包括WebSocket、Servlet、Web和Portlet模块,具体介绍如下:
● WebSocket模块:Spring4.0以后新增的模块,它提供了WebSocket 和SockJS的实现,以及对STOMP的支持。
● Servlet 模块:也称为Spring-webmvc模块,包含了Spring的模型—视图—控制器(MVC)和REST Web Services实现的Web应用程序。
● Web模块:提供了基本的Web开发集成特性,例如:多文件上传功能、使用Servlet监听器来初始化IoC容器以及Web应用上下文。
● Portlet 模块:提供了在portlet环境中使用MVC实现,类似Servlet模块的功能。
4、 其他模块
Spring的其他模块还有AOP、Aspects 、Instrumentation 以及Test模块,具体介绍如下:
● AOP模块:提供了面向切面编程实现,允许定义方法拦截器和切入点,将代码按照功能进行分离,以降低耦合性。
● Aspects 模块:提供了与AspectJ的集成功能,AspectJ是一个功能强大且成熟的面向切面编程(AOP)框架。
● Instrumentation 模块:提供了类工具的支持和类加载器的实现,可以在特定的应用服务器中使用。
● Messaging模块:Spring4.0以后新增的模块,它提供了对消息传递体系结构和协议的支持。
● Test模块:提供了对单元测试和集成测试的支持。
2.Spring5 入门案例
1.创建一个maven无模板项目
2.导入spring依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context
Spring提供了在基础IoC功能上的扩展服务,还提供了许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core
包含Spring框架基本的核心工具类,Spring其它组件都要用到这个包里的类,是其它组件的基本核心。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans
所有应用都要用到的JAR包,它包含访问配置文件、创建和管理Bean以及进行Inversion of Control(IoC)或者Dependency Injection(DI)操作相关的所有类。-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression
定义了Spring的表达式语言。-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.3.19</version>
</dependency>
3.创建普通类,在这个类创建普通方法
public class User {
public void add() {
System.out.println("add......");
}
}
4.创建spring配置文件
在resources文件夹创建applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1.导入外部的jdbc.properties文件 -->
<context:property-placeholder location="classpath*:jdbc.properties"/>
<!-- 配置文件方式配置实体类对象-->
<bean class="pojo.User" id="user"/>
<!-- <!– 2.定义数据源 –>-->
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">-->
<!-- <property name="url" value="${jdbc.url}"/>-->
<!-- <property name="username" value="${jdbc.username}"/>-->
<!-- <property name="password" value="${jdbc.password}"/>-->
<!-- <property name="driverClassName" value="${jdbc.driver}"/>-->
<!-- <property name="maxActive" value="10"/>-->
<!-- <property name="minIdle" value="5"/>-->
<!-- </bean>-->
<!-- <!–3.这里使用MP提供的MybatisSqlSessionFactoryBean,完成了Spring与MP的整合–>-->
<!-- <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">-->
<!-- <property name="dataSource" ref="dataSource"/>-->
<!-- </bean>-->
<!-- <!–4.扫描mapper接口,使用的依然是Mybatis原生的扫描器–>-->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!-- <property name="basePackage" value="DAO"/>-->
<!-- </bean>-->
</beans>
5.测试方法
public class UserTest {
/**
* 使用配置文件创建的实体类对象
*/
@Test
public void testAdd() {
//通过ApplicationContext类加载配置文件
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.利用配置文件创建的对象的id属性获取对象
User user=context.getBean("user",User.class);//配置文件初始化的类对象id和实体类类型
System.out.println(user);
user.add();
}
}
结果: