一、摘要
本文主要概要介绍Spring Framework的优缺点、版本演进历程及未来发展路线图;一些比较好的spring学习资源推荐;最后以spring-3.2.5.RELEASE版本为例,讲述基于Eclipse IDE的maven依赖开发环境搭建。
二、Spring概述
Spring是一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架,旨在解决业务逻辑层和其他各层的松耦合问题,因此它将面向接口编程思想贯穿整个系统应用。
1、版本迭代
Spring从2003年开始兴起1.x到最新的5.x,已经历了好几个重大版本迭代,尤其4和5更是发生了革命性的变化。
1)spring 0.x
Rod Johnson在2002年编著的《Expert one on one J2EE design and development》一书中,对Java EE系统框复杂、低效、开发效率低等诸多方面提出了批判,并积极寻求解决之道,为此编写了interface21框架,即spring前身。
2)spring 1.0
2004年3月正式推出1.0版本,并发布经典力作《Expert one-on-one J2EE Development without EJB》,该版本包括core,bean管理,aop,jdbc,事务管理,mail等框架的核心功能,只支持基于xml配置。
3)spring 2.0
- 2006,Security 1.0、Spring webflow 1.0发布;Spring 2.0发布
- 支持Java5(Tiger);
- 增加对注解的支持,支持基于注解的配置;
- 重新发布了对AspectJ的内部支持,允许用户自定义切面,提供声明式企业服务(最重要的是声明式事务管理);
- 更多新特性请参考 spring2.0参考手册
4)spring 2.5
- 2007,Spring Batch、WebService、Integration发布;Spring 2.5发布;
- 2008,Spring Integration 1.0,Spring 2.5.6,Spring Batch 1.0;
- 新的bean作用域:增加request、session、global session三个在web环境下使用的bean作用域,且提供了hooks,用户创建自己的作用域;
- xml配置更简单,更新了DTD,并增加namespace支持
- 引入完整的Annotation集合如:@Resource、@Autowired
- 异步JMS
- 更多新特性请参考 spring2.5.x参考手册
5)spring 3.0
- 2009,Spring Python、STS发布、3.0发布(将包拆开,不提供超级包);
- Spring Expression Language (Spring表达式语言);
- 增强的IOC,基于java类配置,JavaConfig;
- Comprehensive REST support (广泛的REST支持)
- Embedded database support (嵌入式数据库的支持);
- Declarative model validation(声明试模型验证);
- 更多新特性请参考 spring3.0.0参考手册
6) spring4.0
- 2013,Spring 4.0、Spring Boot发布;
- 全面支持Java 8,支持Lambda表达式的使用;
- 提供了对@Scheduled和@PropertySource重复注解的支持,提供了空指针终结者Optional;
- IOC增加:支持泛型的依赖注入、Map的依赖注入、Lazy延迟依赖的注入、List注入、Condition条件注解注入、对CGLib动态代理类进行了增强;
- 支持了基于Groovy DSL的配置,提高Bean配置的灵活性;
- Spring MVC基于Servlet 3.0 开发,增加RestController注解器注解,同时还增加了一个AsyncRestTemplate支持Rest客户端的异步无阻塞请求
- 还有NB的Spring Boot
- 更多新特性请参考 spring4.0.0参考手册
6) spring5.0
- 2017,Spirng 5.x
- 基准升级,JDK基准版本为Java8,兼容9,使用了很多java8和9的新特;
- 响应式编程支持;
- 函数式web框架;
- Kotlin支持;
- 取消了对Portlet、Velocity、JasperReports、XMLBeans等框架的支持;
- 更多新特性请参考 spring5.0.0参考手册
2、spring的美好未来
3、spring3.2.5内容构成
由于本人相对古董,仍然使用3.x,后续关于spring相关文章也是基于该版本,因此,简单介绍下3.x的构成和使用场景。
1)spring3.x 构成,图片来源于spring参考手册
2)spring 使用场景,图片来源于spring参考手册
三、spring 开发环境搭建
1、前提条件
- Eclipse IDE
- Eclipse Maven 插件
2、新建maven项目,pom文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.marcus</groupId>
<artifactId>lmdgh-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
<build>
<finalName>lmdgh-spring</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf-8</encoding>
<wtpmanifest>true</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>WebContent</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
3、简单bean配置文件 helloWorldBean.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.marcus.spring.beans.HelloWorldBean">
<property name="message" value="Hello World!"/>
</bean>
</beans>
4、HelloWorldBean.java
package com.marcus.spring.beans;
public class HelloWorldBean {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("Your Message : " + message);
}
}
5、测试类BeanApp.java
本案例使用BeanFactory加载、管理Bean,并验证是否单例。
public class BeanApp {
public static void main(String[] args) {
Resource resource=new ClassPathResource("helloWorldBean.xml");
BeanFactory fa=new DefaultListableBeanFactory();
BeanDefinitionReader bdr=new XmlBeanDefinitionReader((BeanDefinitionRegistry) fa);
bdr.loadBeanDefinitions(resource);
//
HelloWorldBean helloWorld = fa.getBean("helloWorld", HelloWorldBean.class);
helloWorld.getMessage();
System.out.println(helloWorld);
helloWorld.setMessage("tmd singleton!");
//
helloWorld = fa.getBean("helloWorld", HelloWorldBean.class);
helloWorld.getMessage();
System.out.println(helloWorld);
}
}
6、控制台输出如下
Your Message : Hello World!
com.marcus.spring.beans.HelloWorldBean@6cd8737
Your Message : tmd singleton!
com.marcus.spring.beans.HelloWorldBean@6cd8737