spring技术核心概念纪要

简介: 一、背景 springframework 从最初的2.5版本发展至今,期间已经发生了非常多的修正及优化。许多新特性及模块的出现,使得整个框架体系显得越趋庞大,同时也带来了学习及理解上的困难。 本文阐述了一些要点,并配合一些代码样例,这有助于快速理解 spring 框架。

一、背景

springframework 从最初的2.5版本发展至今,期间已经发生了非常多的修正及优化。许多新特性及模块的出现,使得整个框架体系显得越趋庞大,同时也带来了学习及理解上的困难。

本文阐述了一些要点,并配合一些代码样例,这有助于快速理解 spring 框架。

二、spring架构

核心容器层

Core 模块

提供了框架的基本组成部分,包括 IoC 及依赖注入功能。

Bean 模块

实现 Bean 管理,包括自动装配机制等功能; 其中BeanFactory是一个工厂模式的实现。

Context 模块

建立在 Core 和 Bean 模块基础上,通常用于访问配置及定义的任何对象。ApplicationContext 是上下文模块的重要接口。

SpEL 模块

表达式语言模块提供了运行时进行查询及操作一个对象的表达式机制。

数据访问/集成

JDBC 模块

用于替代繁琐的 JDBC API 的抽象层。

ORM 模块

对象关系数据库映射抽象层,可集成JPA,JDO,Hibernate,iBatis。

OXM 模块

XML消息绑定抽象层,支持JAXB,Castor,XMLBeans,JiBX,XStream。

JMS 模块

Java消息服务模块,实现消息生产-消费之类的功能。

Transaction 模块

事务模块为各种 POJO 支持编程式和声明式事务管理。

Web应用

Web 模块

Web MVC 提供了基于 模型-视图-控制器 的基础web应用框架。

servlet 模块

实现了统一的监听器以及和面向web应用的上下文,用以初始化 IoC 容器。

Web-Portlet

实现在 portlet 环境中实现 MVC。

Web-Socket 模块

为 WebSocket连接 提供支持。

其他模块

AOP 模块

提供了面向切面的编程实现,允许开发者通过定义方法拦截器及切入点对代码进行无耦合集成,它实现了关注点分离。

Aspects 模块

提供了与 AspectJ 的集成,这是一个功能强大且成熟的面向切面编程(AOP)框架。

Instrumentation 模块

实现instrumentation支持,一般用以应用服务器的监测。

Messaging 模块

为STOMP 提供了支持,STOMP协议是一种简单的文本定向消息协议,是 WebSocket 的子协议。

测试

支持 JUnit 、TestNG 框架的集成


三、基础工程

后续的工作将基于样例工程展开,首先需要准备JDK、Java IDE如Eclipse、Maven环境,此类工作较为简单,在此不作赘述。

  1. 创建Maven项目;
  2. 配置Spring依赖;
<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>4.3.2.RELEASE</version>

</dependency>

 

    3. 编写配置文件及测试代码;

core-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="randomID" class="org.springfoo.core.bean.RandomID" scope="prototype" 
    init-method="init" destroy-method="destroy">
    </bean>

    <bean id="message" class="org.springfoo.core.bean.Message" scope="prototype">
        <property name="content" value="Hello sam" />
        <property name="sender" value="bob" />
        <property name="reciever" value="sam" />
    </bean>
</beans>
 

POJO定义

public class Message {

    private String content;

    private String sender;

    private String reciever;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
    ...

 

测试代码

private static void tryAppContext() {
    ApplicationContext context = new ClassPathXmlApplicationContext("core-beans.xml");

    Message message = context.getBean(Message.class);
    System.out.println(message);
}

 


四、IOC 容器

IOC 即控制反转,将对象的生命周期管理、关系依赖通过容器实现,实现解耦。

ApplicationContext是最关键的入口,其包括几种实现:

  1. FileSystemXmlApplicationContext,从 XML 文件中加载被定义的 bean对象,基于文件系统路径加载配置;

  2. ClassPathXmlApplicationContext,从 XML 文件中加载被定义的 bean对象,基于类路径加载配置;

  3. WebXmlApplicationContext,从 XML 文件中加载被定义的 bean对象,基于 web 应用程序范围加载配置;

五、Bean 管理

5.1 作用域

singleton

每一个 Spring IoC 容器中保持一个单一实例(默认)。

prototype

bean 的实例可为任意数量。

request

该作用域将 bean 的定义限制为 HTTP 请求。只在 web-aware Spring ApplicationContext 的上下文中有效。

session

该作用域将 bean 的定义限制为 HTTP 会话。 只在web-aware Spring ApplicationContext的上下文中有效。

global-session

该作用域将 bean 的定义限制为全局 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中有效。

5.2 生命周期

Bean 的初始化及销毁对应 init 及 destroy 两个行为,可通过实现 InitializingBean/DisposableBean 接口观察对象的初始化及销毁时机。

代码片段:

public void afterPropertiesSet() throws Exception {
    System.out.println(this + "-- properties set");
}

public void init() {
    System.out.println(this + "-- init");
}

public void destroy() {
    System.out.println(this + "-- destroy"); }

 

为了使spring获得 destroy 行为的监视机会,需要注册JVM关闭回调:

context.registerShutdownHook();

 

init/destroy拦截

实现 BeanPostProcessor 接口,并注册到配置文件

<bean class="xxx.MyBeanPostProcessor" />

 

5.3 bean模板

通常可将一组属性归集为bean模板以实现复用

<!-- template -->
<bean id="template" abstract="true">
    <property name="support" value="true" />
    <property name="count" value="10" />
</bean>

<bean id="tplbean" class="org.springfoo.core.bean.TplBean" parent="template">
    <property name="message" value="I'm inheritted from template" />
</bean>

POJO 定义

public class TplBean {

    private String message;
    private boolean support;
    private Integer count;
    ...

 


六、依赖注入

6.1 简单例子

  1. People 包含 Hand/Foot/Body;

  2. Hand/Foot 通过构造参数注入;

  3. Body通过属性参数注入;

beans.xml

<bean id="people" class="org.springfoo.di.bean.People" scope="prototype">
    <constructor-arg ref="foot"/>
    <constructor-arg ref="hand"/>

    <property name="body" ref="body"/>
</bean>


<bean id="foot" class="org.springfoo.di.bean.Foot" scope="prototype">
    <property name="label" value="FOOT" />
</bean>
<bean id="hand" class="org.springfoo.di.bean.Hand" scope="prototype">
    <property name="label" value="HAND" />
</bean>
<bean id="body" class="org.springfoo.di.bean.Body" scope="prototype">
    <property name="label" value="BODY---BB" />
</bean>
 

People.java

public class People {

    private Foot foot;
    private Hand hand;

    private Body body;

    public People(){

    }

    public People(Foot foot, Hand hand) {
    super();
    this.foot = foot;
    this.hand = hand;
    }

    public Foot getFoot() {
        return foot;
    }

    public void setFoot(Foot foot) {
        this.foot = foot;
    }

    public Hand getHand() {
        return hand;
    }

    ...

 

其余略

6.2 注入集合

可通过配置一组值的方式实现集合注入

集合POJO 

@SuppressWarnings("rawtypes")
public class CollectionBean {

private List list;
private Set set;
private Map map;
private Properties prop;

public List getList() {
    return list;
}

public void setList(List list) {
    this.list = list;
}

public Set getSet() {
    return set;
}

public void setSet(Set set) {
    this.set = set;
}

public Map getMap() {
    return map;
}

public void setMap(Map map) {
    this.map = map;
}

public Properties getProp() {
    return prop;
}

public void setProp(Properties prop) {
    this.prop = prop;
}

}

 

beans.xml

<bean id="collection" class="org.springfoo.di.bean.CollectionBean">

    <property name="list">
        <list>
            <value>APPLE</value>
            <value>ORANGE</value>
            <value>PINAPPLE</value>
        </list>
    </property>

    <property name="set">
        <set>
            <value>TABLE</value>
            <value>CHAIR</value>
        </set>
    </property>

    <property name="map">
        <map>
            <entry key="b" value="BEER" />
            <entry key="j" value="JUICE" />
        </map>
    </property>

    <property name="prop">
        <props>
            <prop key="sp">Single Player</prop>
            <prop key="tp">Two Player</prop>
        </props>
    </property>
</bean>

 

6.3 自动装配

POJO定义

public class AutoWireBean {

    private String message;
    private Body body;

    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Body getBody() {
        return body;
    }
    public void setBody(Body body) {
        this.body = body;
    }
}

 

beans.xml

<bean id="autowire" class="org.springfoo.di.bean.AutoWireBean"
 autowire="byName" scope="prototype">

    <property name="message" value="okok autowire going..."/>

</bean>

 

autowire类型

  1. byName, 通过属性名称与配置中bean名称配对
  2. byType, 通过属性类型与配置中bean类型配对
  3. constructor, 通过构造函数中bean类型配对

 

七、总结

至此,关于 spring 的核心概念已经介绍完毕,接下来就是如何在实践中深化了。

相信只要理解了基础理念,在后续的项目中自然会得心应手,毕竟万变不离其宗。

img_9b09a36f6de95886f52ce82fa1e89c88.jpe

作者: zale

出处: http://www.cnblogs.com/littleatp/, 如果喜欢我的文章,请关注我的公众号

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接  如有问题, 可留言咨询.

目录
相关文章
|
7月前
|
SQL Java 数据库连接
Spring Data JPA 技术深度解析与应用指南
本文档全面介绍 Spring Data JPA 的核心概念、技术原理和实际应用。作为 Spring 生态系统中数据访问层的关键组件,Spring Data JPA 极大简化了 Java 持久层开发。本文将深入探讨其架构设计、核心接口、查询派生机制、事务管理以及与 Spring 框架的集成方式,并通过实际示例展示如何高效地使用这一技术。本文档约1500字,适合有一定 Spring 和 JPA 基础的开发者阅读。
748 0
|
8月前
|
前端开发 Java API
利用 Spring WebFlux 技术打造高效非阻塞 API 的完整开发方案与实践技巧
本文介绍了如何使用Spring WebFlux构建高效、可扩展的非阻塞API,涵盖响应式编程核心概念、技术方案设计及具体实现示例,适用于高并发场景下的API开发。
640 0
|
7月前
|
监控 安全 Java
Spring Cloud 微服务治理技术详解与实践指南
本文档全面介绍 Spring Cloud 微服务治理框架的核心组件、架构设计和实践应用。作为 Spring 生态系统中构建分布式系统的标准工具箱,Spring Cloud 提供了一套完整的微服务解决方案,涵盖服务发现、配置管理、负载均衡、熔断器等关键功能。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
470 1
|
7月前
|
监控 Kubernetes Cloud Native
Spring Batch 批处理框架技术详解与实践指南
本文档全面介绍 Spring Batch 批处理框架的核心架构、关键组件和实际应用场景。作为 Spring 生态系统中专门处理大规模数据批处理的框架,Spring Batch 为企业级批处理作业提供了可靠的解决方案。本文将深入探讨其作业流程、组件模型、错误处理机制、性能优化策略以及与现代云原生环境的集成方式,帮助开发者构建高效、稳定的批处理系统。
763 1
|
7月前
|
Java 数据库连接 开发者
Spring Framework 核心技术详解
本文档旨在深入解析 Java Spring Framework 的核心技术原理与应用。与侧重于快速开发的 Spring Boot 不同,本文将聚焦于 Spring 框架本身的设计理念、核心容器、控制反转(IoC)、面向切面编程(AOP)、数据访问与事务管理等基础且强大的模块。通过理解这些核心概念,开发者能够更深刻地领悟 Spring 生态系统的设计哲学,并具备解决复杂企业级应用开发问题的能力。
494 4
|
8月前
|
Java 应用服务中间件 开发者
Spring Boot 技术详解与应用实践
本文档旨在全面介绍 Spring Boot 这一广泛应用于现代企业级应用开发的框架。内容将涵盖 Spring Boot 的核心概念、核心特性、项目自动生成与结构解析、基础功能实现(如 RESTful API、数据访问)、配置管理以及最终的构建与部署。通过本文档,读者将能够理解 Spring Boot 如何简化 Spring 应用的初始搭建和开发过程,并掌握其基本使用方法。
633 2
|
7月前
|
监控 Java API
Spring WebFlux 响应式编程技术详解与实践指南
本文档全面介绍 Spring WebFlux 响应式编程框架的核心概念、架构设计和实际应用。作为 Spring 5 引入的革命性特性,WebFlux 提供了完全的响应式、非阻塞的 Web 开发栈,能够显著提升系统的并发处理能力和资源利用率。本文将深入探讨 Reactor 编程模型、响应式流规范、WebFlux 核心组件以及在实际项目中的最佳实践,帮助开发者构建高性能的响应式应用系统。
1216 0
|
7月前
|
监控 Cloud Native Java
Spring Integration 企业集成模式技术详解与实践指南
本文档全面介绍 Spring Integration 框架的核心概念、架构设计和实际应用。作为 Spring 生态系统中的企业集成解决方案,Spring Integration 基于著名的 Enterprise Integration Patterns(EIP)提供了轻量级的消息驱动架构。本文将深入探讨其消息通道、端点、过滤器、转换器等核心组件,以及如何构建可靠的企业集成解决方案。
729 0
|
7月前
|
Kubernetes Java 微服务
Spring Cloud 微服务架构技术解析与实践指南
本文档全面介绍 Spring Cloud 微服务架构的核心组件、设计理念和实现方案。作为构建分布式系统的综合工具箱,Spring Cloud 为微服务架构提供了服务发现、配置管理、负载均衡、熔断器等关键功能的标准化实现。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
665 0
|
7月前
|
安全 Java 数据安全/隐私保护
Spring Security 核心技术解析与实践指南
本文档深入探讨 Spring Security 框架的核心架构、关键组件和实际应用。作为 Spring 生态系统中负责安全认证与授权的关键组件,Spring Security 为 Java 应用程序提供了全面的安全服务。本文将系统介绍其认证机制、授权模型、过滤器链原理、OAuth2 集成以及最佳实践,帮助开发者构建安全可靠的企业级应用。
413 0

热门文章

最新文章