Spring
简介Spring
是一个轻量级Java
开发框架,最先由Rod Johnson
建立,目的是为了解决企业级应用开发的业务逻辑层和其余各层的耦合问题,是一个分层的Java SE/EE full-stack
轻量级开源框架,为开发Java
应用程序提供全面的基础架构支持。java
Spring
体系结构目前Spring
已经集成了20多个模块,分布在如下模块中:git
Core Container
:核心容器Data Access/Integration
:数据访问/集成Web
:Web
模块AOP
:Aspect Oriented Programming
,面向切面编程Instrumentation
:植入模块Messaging
:消息传输Test
:测试模块如图所示:github
由Spring-core
、Spring-beans
、Spring-context
、Spring-context-support
、Spring-expression
等模块组成:web
Spring-core
:提供了框架的基本组成部分,包括Ioc
(Inversion of Control
,控制反转)以及DI
(Dependency Injection
,依赖注入)功能Spring-beans
:提供了BeanFactory
,是工厂模式的一个典型实现,Spring
将管理的对象称为Bean
Spring-context
:创建在Core
和Beans
模块的基础上,提供了一个框架式的对象访问方式,是访问定义和配置的任何对象的媒介,ApplicationContext
接口是Context
模块的焦点Spring-context-support
:支持整合第三方库到Spring
应用程序上下文,特别是用于高速缓存(EhCache
、JCache
)和任务调度(CommonJ
、Quartz
)的支持Spring-expression
:提供了强大的表达式语言去支持运行时查询和操做对象图,是JSP 2.1
规定的统一表达式语言的扩展AOP
和Instrumentation
Spring-aop
:提供了一个符合AOP
要求的面向切面的编程实现,容许定义方法拦截器和切入点Spring-aspects
:提供了与AspectJ
的集成功能,AspectJ
是一个功能强大且成熟的AOP
框架Spring-instrumentation
:提供了类植入支持和类加载器的实现Spring 4.0
后增长了消息模块,提供了对消息传递体系结构和协议的支持。spring
数据访问/集成层由JDBC
、ORM
、OXM
、JMS
和事务模块组成。数据库
Spring-JDBC
:提供了一个JDBC
抽象层,消除了繁琐的JDBC
编码和数据库厂商特有的错误代码解析Spring-ORM
:为流行的ORM
(Object-Relational Mapping
,对象关系映射)框架提供了支持,包括JPA
和Hibernate
,使用Spring-ORM
框架能够将这些O/R
映射框架与Spring
提供的全部其余功能结合使用Spring-OXM
:提供了一个支持对象/XML
映射的抽象层实现,例如JAXB
、Castor
、JiBX
、XStream
Spring-JMS
:JMS
(Java Messaging Service
,Java
消息传递服务),包含用于生产和使用消息的功能Spring-TX
:事务管理模块,支持用于实现特殊接口和全部POJO
类的编程和声明式事务管理Web
Web
有Spring-Web
、Spring-WebMVC
、Spring-WebSocket
和Portlet
模块组成。express
Spring-Web
:提供了基本的Web
开发集成功能,例如多文件上传等Spring-WebMVC
:也叫Web-Servlet
模块,包含用于Web
应用程序的Spring MVC
和REST Web Services
的实现。Spring-WebSocket
:提供了WebSocket
和SockJS
的实现Porlet
:相似于Servlet
模块的功能,提供了Porlet
环境下MVC
的实现Spring-test
模块支持使用JUnit
或TestNG
对Spring
组件进行单元测试和集成测试。编程
OpenJDK 11.0.8
IDEA 2020.2
Maven
/Gradle
Demo
(Java
版)Maven
工程pom.xml
文件加入:api
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.8.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> </dependencies>
能够戳这里查看最新的版本。缓存
新建以下5个空文件:
applicationContext.xml
该文件是Spring
的配置文件,习惯命名为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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="test" class="TestImpl"/> </beans>
这里声明了一个id
为test
,类为TestImpl
的Bean
。
TestInterface
public interface TestInterface { void hello(); }
TestImpl
public class TestImpl implements TestInterface { @Override public void hello() { System.out.println("Hello Spring."); } }
Main
public class Main { public static void main(String[] args) { System.out.println("Hello"); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ((TestInterface)context.getBean("test")).hello(); } }
Tests
public class Tests { @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); TestInterface testInterface = (TestInterface)context.getBean("test"); testInterface.hello(); } }
直接点击测试类旁边的按钮便可:
若出现以下错误:
是JDK
版本设置错误的问题,先打开Project Structure
,修改Modules
下的JDK
版本:
下一步是打开设置,修改Comiler
下的JDK
版本:
输出:
Main
默认不能直接运行Main
函数,须要添加运行配置:
选择Application
添加配置,而且指定Name
以及Main class
:
这样就能够运行了:
Kotlin
版Demo
使用Gradle
+Kotlin
的入门Demo
。
Gradle
工程build.gradle
完整文件以下:
plugins { id 'java' id 'org.jetbrains.kotlin.jvm' version '1.4.0' } group 'org.example' version '1.0-SNAPSHOT' repositories { mavenCentral() } dependencies { compile group: 'org.springframework', name: 'spring-context', version: '5.2.8.RELEASE' compile group: 'org.springframework', name: 'spring-core', version: '5.2.8.RELEASE' compile group: 'org.springframework', name: 'spring-beans', version: '5.2.8.RELEASE' testCompile group: 'junit', name: 'junit', version: '4.12' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" } compileKotlin { kotlinOptions { jvmTarget = "11" } }
除了添加依赖之外还添加了一些其余参数。
TestInterface
interface TestInterface { fun hello() }
TestImpl
class TestImpl:TestInterface { override fun hello() { println("Hello Spring") } }
Main
fun main() { println("Hello") val context: ApplicationContext = ClassPathXmlApplicationContext("applicationContext.xml") val test: TestInterface = context.getBean("test") as TestInterface test.hello() }
applicationContext.xml
同上。
Tests
class Tests { @Test fun test() { val context:ApplicationContext = ClassPathXmlApplicationContext("applicationContext.xml") val test:TestInterface = context.getBean("test") as TestInterface test.hello() } }
一样直接点击旁边的按钮便可运行:
Main
一样点击按钮便可: