我听说了很多关于Spring的事情,人们在网上说Spring是一个很好的web开发框架。简而言之,Spring框架到底是用来干什么的?为什么我要用它而不是纯Java。
当前回答
其优点是依赖注入(DI)。这意味着将对象创建任务外包出去。让我用一个例子来解释。
public interface Lunch
{
public void eat();
}
public class Buffet implements Lunch
{
public void eat()
{
// Eat as much as you can
}
}
public class Plated implements Lunch
{
public void eat()
{
// Eat a limited portion
}
}
现在在我的代码中,我有一个类LunchDecide,如下所示:
public class LunchDecide {
private Lunch todaysLunch;
public LunchDecide(){
this.todaysLunch = new Buffet(); // choose Buffet -> eat as much as you want
//this.todaysLunch = new Plated(); // choose Plated -> eat a limited portion
}
}
在上面的类中,根据心情选择Buffet()或Plated()。但是这个系统是紧密耦合的。每次需要不同类型的对象时,我们都需要更改代码。在本例中,注释掉一行!假设有50个不同的人使用50个不同的类。那会是一团乱麻。在这种情况下,我们需要解耦系统。让我们重写LunchDecide类。
public class LunchDecide {
private Lunch todaysLunch;
public LunchDecide(Lunch todaysLunch){
this.todaysLunch = todaysLunch
}
}
Notice that instead of creating an object using new keyword we passed the reference to an object of Lunch Type as a parameter to our constructor. Here, object creation is outsourced. This code can be wired either using Xml config file (legacy) or Java Annotations (modern). Either way, the decision on which Type of object would be created would be done there during runtime. An object would be injected by Xml into our code - Our Code is dependent on Xml for that job. Hence, Dependency Injection (DI). DI not only helps in making our system loosely coupled, it simplifies writing of Unit tests since it allows dependencies to be mocked. Last but not the least, DI streamlines Aspect Oriented Programming (AOP) which leads to further decoupling and increase of modularity. Also note that above DI is Constructor Injection. DI can be done by Setter Injection as well - same plain old setter method from encapsulation.
其他回答
过去,Spring只是一个依赖注入框架(Guice, PicoContainer,…),但现在它是构建企业应用程序的完整解决方案。
spring依赖注入,当然,spring的核心仍然在那里(你可以在这里查看其他好的答案),但是还有更多来自spring的答案……
Spring现在有很多项目,每个项目都有一些子项目(http://spring.io/projects)。当有人谈到spring时,你一定要弄清楚他说的是什么spring项目,是仅仅是spring核心,也就是spring框架,还是另一个spring项目。
值得一提的春季项目有:
Spring Security - http://projects.spring.io/spring-security/ Spring Webservices - http://projects.spring.io/spring-ws/ Spring集成- http://projects.spring.io/spring-integration/
如果你的应用程序需要一些更指定的功能,你也可以在那里找到:
Spring批处理框架设计,使批处理能够开发 批处理应用程序 Spring HATEOAS基于HATEOAS原理轻松创建REST API Spring Mobile和Spring Andriod用于移动应用程序开发 Spring Shell构建一个功能齐全的Shell(又名命令行)应用程序 Spring Cloud和Spring Cloud Data Flow用于云应用程序
还有一些小项目,比如spring-social-facebook (http://projects.spring.io/spring-social-facebook/)
你可以使用spring进行web开发,因为它有spring MVC模块,这是spring框架项目的一部分。或者你可以将spring与另一个web框架一起使用,比如struts2。
过去,我从纯技术的角度考虑Spring框架。
根据一些团队工作和开发企业web应用程序的经验,我会说Spring通过解耦各个元素(bean)来实现更快的应用程序(web应用程序)开发。更快的发展使它如此受欢迎。Spring允许将构建(连接)应用程序的责任转移到Spring框架上。Spring框架的依赖注入负责将单个bean连接/连接到工作应用程序中。
这样,一旦定义了bean之间的接口,开发人员就可以更专注于单个组件(bean)的开发。
测试这样的应用程序很容易——主要的关注点是单个bean。它们可以很容易地解耦和模拟,因此单元测试是快速和有效的。
Spring框架定义了多个专门的bean,如@Controller (@Restcontroller)、@Repository、@Component来服务于web目的。Spring和Maven一起提供了一个对开发人员来说很直观的结构。 团队工作简单快捷,因为单独的元素是分开的,可以重复使用。
Spring非常适合将类实例粘合在一起。你知道你的Hibernate类总是需要一个数据源,Spring将它们连接在一起(也有一个数据源的实现)。
数据访问对象总是需要Hibernate访问,Spring将Hibernate类连接到dao中。
此外,Spring基本上为您提供了一系列库的可靠配置,并指导您应该使用哪些库。
Spring真的是一个很棒的工具。(我说的不是Spring MVC,只是基本框架)。
其优点是依赖注入(DI)。这意味着将对象创建任务外包出去。让我用一个例子来解释。
public interface Lunch
{
public void eat();
}
public class Buffet implements Lunch
{
public void eat()
{
// Eat as much as you can
}
}
public class Plated implements Lunch
{
public void eat()
{
// Eat a limited portion
}
}
现在在我的代码中,我有一个类LunchDecide,如下所示:
public class LunchDecide {
private Lunch todaysLunch;
public LunchDecide(){
this.todaysLunch = new Buffet(); // choose Buffet -> eat as much as you want
//this.todaysLunch = new Plated(); // choose Plated -> eat a limited portion
}
}
在上面的类中,根据心情选择Buffet()或Plated()。但是这个系统是紧密耦合的。每次需要不同类型的对象时,我们都需要更改代码。在本例中,注释掉一行!假设有50个不同的人使用50个不同的类。那会是一团乱麻。在这种情况下,我们需要解耦系统。让我们重写LunchDecide类。
public class LunchDecide {
private Lunch todaysLunch;
public LunchDecide(Lunch todaysLunch){
this.todaysLunch = todaysLunch
}
}
Notice that instead of creating an object using new keyword we passed the reference to an object of Lunch Type as a parameter to our constructor. Here, object creation is outsourced. This code can be wired either using Xml config file (legacy) or Java Annotations (modern). Either way, the decision on which Type of object would be created would be done there during runtime. An object would be injected by Xml into our code - Our Code is dependent on Xml for that job. Hence, Dependency Injection (DI). DI not only helps in making our system loosely coupled, it simplifies writing of Unit tests since it allows dependencies to be mocked. Last but not the least, DI streamlines Aspect Oriented Programming (AOP) which leads to further decoupling and increase of modularity. Also note that above DI is Constructor Injection. DI can be done by Setter Injection as well - same plain old setter method from encapsulation.
Spring框架绝对适合web开发,尤其适合restful api服务。
这是因为它的依赖注入和与其他模块的集成,如spring security、spring aop、mvc框架、微服务
在任何应用程序中,安全性都很可能是一个需求。 如果您的目标是构建一个需要长期维护的产品,那么您将需要利用Aop的概念。 如果你的应用程序有太多的流量,从而增加了负载,你需要使用微服务的概念。
Spring在一个平台中提供了所有这些特性。支持多个模块。 最重要的是,spring是开源的和可扩展的框架,在生命周期中到处都有钩子来集成自定义代码。
Spring Data是一个提供与您的项目集成的项目。
所以弹簧几乎可以满足所有的需求。
推荐文章
- 如何使一个Java通用方法静态?
- for-each循环和迭代器,哪个更有效?
- 泛型类中的静态方法?
- 如何在JPA中持久化类型列表<字符串>的属性?
- 考虑在配置中定义一个'package'类型的bean [Spring-Boot]
- Java注释中的/**和/*
- java8 LocalDate Jackson格式
- Android Studio谷歌JAR文件导致GC开销限制超过错误
- 如何在Intellij生成串行版本UID
- “比较法违反其总合同!”
- 从Java项目生成UML类图
- 正确地从一个<Integer>的列表中移除一个整数
- Java开关语句:需要常量表达式,但它是常量
- Java的assertEquals方法可靠吗?
- 如何在Java中获得系统变量值?