您的位置:首页 > 编程语言 > Java开发

spring技术核心概念纪要

2016-09-10 14:56 218 查看

一、背景

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

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

二、spring架构





核心容器层

Core模块

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

Bean模块

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

Context模块

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

SpEL模块

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

数据访问/集成

JDBC模块

用于替代繁琐的JDBCAPI的抽象层。

ORM模块

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

OXM模块

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

JMS模块

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

Transaction模块

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

Web应用

Web模块

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

servlet模块

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

Web-Portlet

实现在portlet环境中实现MVC。

Web-Socket模块

为WebSocket连接提供支持。

其他模块

AOP模块

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

Aspects模块

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

Instrumentation模块

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

Messaging模块

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

测试

支持JUnit、TestNG框架的集成

三、基础工程

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

创建Maven项目;

配置Spring依赖;

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.3.2.RELEASE</version>

</dependency>


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

core-beans.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<beanid="randomID"class="org.springfoo.core.bean.RandomID"scope="prototype"
init-method="init"destroy-method="destroy">
</bean>

<beanid="message"class="org.springfoo.core.bean.Message"scope="prototype">
<propertyname="content"value="Hellosam"/>
<propertyname="sender"value="bob"/>
<propertyname="reciever"value="sam"/>
</bean>
</beans>



POJO定义

publicclassMessage{

privateStringcontent;

privateStringsender;

privateStringreciever;

publicStringgetContent(){
returncontent;
}

publicvoidsetContent(Stringcontent){
this.content=content;
}
...


测试代码

privatestaticvoidtryAppContext(){
ApplicationContextcontext=newClassPathXmlApplicationContext("core-beans.xml");

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


四、IOC容器





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

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

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

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

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

五、Bean管理

5.1作用域

singleton

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

prototype

bean的实例可为任意数量。

request

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

session

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

global-session

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

5.2生命周期

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

代码片段:

publicvoidafterPropertiesSet()throwsException{
System.out.println(this+"--propertiesset");
}

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

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


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

context.registerShutdownHook();


init/destroy拦截

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

<beanclass="xxx.MyBeanPostProcessor"/>


5.3bean模板

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

<!--template-->
<beanid="template"abstract="true">
<propertyname="support"value="true"/>
<propertyname="count"value="10"/>
</bean>

<beanid="tplbean"class="org.springfoo.core.bean.TplBean"parent="template">
<propertyname="message"value="I'minherittedfromtemplate"/>
</bean>


POJO定义

publicclassTplBean{

privateStringmessage;
privatebooleansupport;
privateIntegercount;
...


六、依赖注入

6.1简单例子

People包含Hand/Foot/Body;

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

Body通过属性参数注入;

beans.xml

<beanid="people"class="org.springfoo.di.bean.People"scope="prototype">
<constructor-argref="foot"/>
<constructor-argref="hand"/>

<propertyname="body"ref="body"/>
</bean>

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



People.java

publicclassPeople{

privateFootfoot;
privateHandhand;

privateBodybody;

publicPeople(){

}

publicPeople(Footfoot,Handhand){
super();
this.foot=foot;
this.hand=hand;
}

publicFootgetFoot(){
returnfoot;
}

publicvoidsetFoot(Footfoot){
this.foot=foot;
}

publicHandgetHand(){
returnhand;
}

...


其余略

6.2注入集合

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

集合POJO

@SuppressWarnings("rawtypes")
publicclassCollectionBean{

privateListlist;
privateSetset;
privateMapmap;
privatePropertiesprop;

publicListgetList(){
returnlist;
}

publicvoidsetList(Listlist){
this.list=list;
}

publicSetgetSet(){
returnset;
}

publicvoidsetSet(Setset){
this.set=set;
}

publicMapgetMap(){
returnmap;
}

publicvoidsetMap(Mapmap){
this.map=map;
}

publicPropertiesgetProp(){
returnprop;
}

publicvoidsetProp(Propertiesprop){
this.prop=prop;
}

}


beans.xml

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

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

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

<propertyname="map">
<map>
<entrykey="b"value="BEER"/>
<entrykey="j"value="JUICE"/>
</map>
</property>

<propertyname="prop">
<props>
<propkey="sp">SinglePlayer</prop>
<propkey="tp">TwoPlayer</prop>
</props>
</property>
</bean>


6.3自动装配

POJO定义

publicclassAutoWireBean{

privateStringmessage;
privateBodybody;

publicStringgetMessage(){
returnmessage;
}
publicvoidsetMessage(Stringmessage){
this.message=message;
}
publicBodygetBody(){
returnbody;
}
publicvoidsetBody(Bodybody){
this.body=body;
}
}


beans.xml

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

<propertyname="message"value="okokautowiregoing..."/>

</bean>


autowire类型

byName,通过属性名称与配置中bean名称配对

byType,通过属性类型与配置中bean类型配对

constructor,通过构造函数中bean类型配对

七、总结

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

相信只要理解了基础理念,在后续的项目中自然会得心应手,毕竟万变不离其宗。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: