在写这篇文章的时候;本身其实很犹豫;由于这个Spring的教程网上的教程也有不少。可是我的但愿本身能够在底层尽可能的详细的总结一下;关于Spring的教程我的目标主要是底层的学习;可能这个是一个艰难的开始;期间可能有些错误,但愿你们一同讨论。php
Spring FrameWork目前已经发布Spring5.0了;Spring框架是一个全功能栈(full-stack)的应用程序框架。html
直接文章发布已经更新到SpringFramwork5.0;若是你想看其余版本的特性推荐《Spring历史版本变迁和现在的生态帝国》参考博文java
- 基于 Java 8 的反射加强, Spring Framework 5.0 中的方法参数能够更加高效的进行访问。
- 核心的 Spring 接口如今提供基于Java 8 的默认方法构建的选择性声明。 用
- @Nullable 和 @NotNull 注解来显示代表可为空的参数和以及返回值。这样就够在编译的时候处理空值而不是在运行时抛出 NullPointerExceptions
基于日志记录方面:web
- Spring Framework 5.0 带来了 Commons Logging 桥接模块的封装, 它被叫作 spring-jcl 而不是标准的 Commons Logging。固然,无需任何额外的桥接,新版本也会对 Log4j 2.x, SLF4J, JUL ( java.util.logging) 进行自动检测。
Spring Framework 5.0 如今支持候选组件索引做为类路径扫描的替代方案。该功能已经在类路径扫描器中添加,以简化添加候选组件标识的步骤。
能够在 Spring 的 Jira上了解更多关于组件索引的相关信息spring
引入了对 JetBrains Kotlin 语言的支持。Kotlin 是一种支持函数式编程编程风格的面向对象语言。Kotlin 运行在 JVM 之上,但运行环境并不限于 JVM。数据库
笔者对于Kotlin语言不是很熟悉;不作过多的评价。编程
Spring 发行版本的一个激动人心的特性就是新的响应式堆栈 WEB 框架。这个堆栈彻底的响应式且非阻塞,适合于事件循环风格的处理,能够进行少许线程的扩展。服务器
后续针对这个作个专题的探讨;Spring WebFluxapp
Spring Framework 5.0 彻底支持 JUnit 5 Jupiter,因此可使用 JUnit 5 来编写测试以及扩展。此外还提供了一个编程以及扩展模型,Jupiter 子项目提供了一个测试引擎来在 Spring 上运行基于 Jupiter 的测试。 另外,Spring Framework 5 还提供了在 Spring TestContext Framework 中进行并行测试的扩展。
针对响应式编程模型, spring-test 如今还引入了支持 Spring WebFlux 的 WebTestClient 集成测试的支持,相似于 MockMvc,并不须要一个运行着的服务端。使用一个模拟的请求或者响应, WebTestClient 就能够直接绑定到 WebFlux 服务端设施框架
Spring Framework 5.0目前支持如下升级库的版本 :
- Spring Framework 5.0目前支持如下升级库的版本 :
- Jackson 2.6+
- EhCache 2.10+ / 3.0 GA
- Hibernate 5.0+
- JDBC 4.0+
- XmlUnit 2.x+
- OkHttp 3.x+
- Netty 4.1+
在 API 层面,Spring Framework 5.0 再也不支持如下包:
- beans.factory.access
- jdbc.support.nativejdbc
- spring-aspects 模块的 mock.staticmock
- web.view.tiles2M.(最低要求 Tiles 3)
- orm.hibernate3 和 orm.hibernate4.
目前 Hibernate 5 是支持的框架。
- Portlet.
- Velocity.
- JasperReports.
- XMLBeans.
- JDO.
- Guava.
若是你正在使用任何上面的包,建议你将 Spring Framework 版本维持在 4.3.x。
Spring官方其实推荐使用SpringBoot构建Spring框架程序,可是这里重点关注Spring的框架的内容;因此这里使用Spring框架搭建;后期有机会搭建SpringBoot的教程和资料。
构建Maven的工程;工程目录以下:
使用注解方式的Helloworld:
pom.xml文件:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
复制代码
hello/MessageService.java
package hello.annotation;
public interface MessageService {
String getMessage();
}
复制代码
hello/MessagePrinter.java
package hello.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessagePrinter {
final private MessageService service;
@Autowired
public MessagePrinter(MessageService service) {
this.service = service;
}
public void printMessage() {
System.out.println(this.service.getMessage());
}
}
复制代码
hello/Application.java
package hello.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
@Configuration
@ComponentScan
public class Application {
@Bean
MessageService mockMessageService() {
return new MessageService() {
public String getMessage() {
return "Hello World!";
}
};
}
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(Application.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();
}
}
复制代码
<?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="MessageService" class="hello.xml.MessageService">
<property name="message" value="Hello World!"/>
</bean>
</beans>
复制代码
MessageService.java
package hello.xml;
public class MessageService {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
复制代码
Application.java
package hello.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
MessageService messageService = application.getBean(MessageService.class);
System.out.println(messageService.getMessage());
}
}
复制代码
4.运行结果:
参考资料: