Spring的概念 (Spring Definition)
Spring是一个全面的开源框架,用于构建Java企业应用程序。
1. 核心特点
它的核心特点包括控制反转(IoC)、依赖注入(DI)、面向切面编程(AOP)、事务管理、数据访问、集成和安全性等。Spring的目标是简化企业应用程序的开发,提高代码的可测试性、可维护性和可扩展性。
Spring in a Nutshell
- Very popular framework for building Java applications
- Provides a large number of helper classes and annotations
2. Spring Framework
Spring Framework包括多个模块,每个模块解决特定领域的问题。
- Spring Core模块提供了IoC和DI支持,
- Spring Data模块简化了数据访问,
- Spring Boot模块用于构建自包含的可执行JAR文件,
- Spring Security模块提供了安全性支持,等等。通过选择适当的模块,开发者可以轻松地构建符合项目需求的Java应用程序。
3. Goals of Using Spring
- Lightweight development with Java POJOs (Plain-Old-Java-Objects)
- Dependency injection to promote loose coupling
- Declarative programming with Aspect-Oriented-Programming (AOP)
- Minimize boilerplate Java code
4. Spring 和 Spring Boot
PROBLEM: Building a traditional Spring application is really HARD!!!
- Q: Which JAR dependencies do I need?
- Q: How do I set up configuration (xml or Java)?
- Q: How do I install the server? (Tomcat, JBoss etc…)
SOLUTION: Spring Boot
- Make it easier to get started with Spring development
- Minimize the amount of manual configuration
- Perform auto-configuration based on props files and JAR classpath
- Help to resolve dependency conflicts (Maven or Gradle)
- Provide an embedded HTTP server so you can get started quickly
- Tomcat, Jetty, Undertow, …
- Spring Boot uses Spring behind the scenes
- Spring Boot simply makes it easier to use Spring
5. Spring 和 Maven
Maven是和Spring相辅相成的,是在我们做Spring Project的时候的一个辅助工具(Think it as a helper)。我们只需要在pom.xml里加入我们需要的dependency,那么maven就会帮我们下载对应的JAR files available during compile/run
- Tell Maven the projects you are working with (dependencies)
- Spring, Hibernate etc ....
- Maven will go out and download the JAR files for those projects for you
- And Maven will make those JAR files available during compile/run
- Think of Maven as your friendly helper / personal shopper :-)
Spring Initializr - 开始Spring Project的第一步
- Quickly create a starter Spring Boot project
- Select your dependencies
- Creates a Maven/Gradle project
- Import the project into your IDE - Eclipse, IntelliJ, NetBeans etc
具体操作-process
- Configure our project at Spring Initializr website
- Download the zip file
- Unzip the file
- Import the project into our IDE
常见问题
Q: Does Spring Boot replace Spring MVC, Spring REST etc …
A: No. Instead, Spring Boot actually uses those technologies
Q: Does Spring Boot run code faster than regular Spring code?
A:No. Behind the scenes, Spring Boot uses same code of Spring Framework. Remember, Spring Boot is about making it easier to get started => Minimizing configuration etc …