我听说了很多关于Spring的事情,人们在网上说Spring是一个很好的web开发框架。简而言之,Spring框架到底是用来干什么的?为什么我要用它而不是纯Java。


当前回答

过去,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为配置引入了对各种注释的支持。

Spring注释方法(依赖注入)

还有另一种使用XML文件连接类的方法:注释。让我们使用接受的答案中的示例,并使用@Component、@Service、@Repository或@Configuration注释之一直接在类上注册bean:

@Component
public class UserListerDB implements UserLister {
    public List<User> getUsers() {
        // DB access code here
    }
}

这样,当视图创建时,它将神奇地有一个UserLister准备工作。

上面的语句是有效的,它不需要使用任何XML文件,并使用另一个注释@Autowired来查找相关的实现并将其注入其中。

@Autowired
private UserLister userLister;

在用于获取要注入的bean实现的方法上使用@Bean注释。

春天有三样东西。

Spring handles Dependency Injection and I recommend you read Martin Fowler's excellent introduction on dependency injection. The second thing Spring does is wrap excellent Java libraries in a very elegant way to use in your applications. For a good example see how Spring wraps Task Executors and Quartz Scheduler. Thirdly Spring provides a bunch of implementations of web stuff like REST, an MVC web framework and more. They figure since you are using Spring for the first two, maybe you can just use it for everything your web app needs.

The problem is that Spring DI is really well thought out, the wrappers around other things are really well thought out in that the other things thought everything out and Spring just nicely wraps it. The Spring implementations of MVC and REST and all the other stuff is not as well done (YMMV, IMHO) but there are exceptions (Spring Security is da bomb). So I tend to use Spring for DI, and its cool wrappers but prefer other stuff for Web (I like Tapestry a lot), REST (Jersey is really robust), etc.

春天是用来干什么的?我会简短地回答这个问题,但首先,让我们再看看维克多·雨果的例子。这不是一个很好的例子,因为它没有证明需要一个新的框架。

public class BaseView {
  protected UserLister userLister;

  public BaseView() {
    userLister = new UserListerDB(); // only line of code that needs changing
  }
}

public class SomeView extends BaseView {
  public SomeView() {
    super();
  }

  public void render() {
    List<User> users = userLister.getUsers();
    view.render(users);
  }
}

完成了!因此,现在即使您有数百或数千个视图,您仍然只需要更改一行代码,就像Spring XML方法中那样。 但是更改一行代码仍然需要重新编译,而不是编辑XML。好吧,我的挑剔的朋友,使用Ant和脚本!

那么春天是用来干什么的呢?这是为:

盲目的开发者随大流 雇主不想雇佣毕业的程序员,因为他们在大学里没有教过这样的框架 从一个糟糕的设计开始,需要修补的项目(如维克多·雨果的例子所示)

进一步阅读:http://discuss.joelonsoftware.com/?joel.3.219431.12

其优点是依赖注入(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一开始是依赖注入,然后为几乎所有的东西添加了包装之王(JPA实现的包装等)。

说来话长……Spring的大多数部分更倾向于XML解决方案(XML脚本引擎…brrrr),所以对于DI,我使用Guice

很好的库,但是随着依赖性的增长,例如Spring JDBC(可能是一个具有实名参数的Java JDBC解决方案)将从maven 4-5继承。

使用Spring MVC(“big Spring”的一部分)进行web开发…它是“基于请求的”框架,有“请求vs组件”的圣战…由你决定