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


当前回答

春天有三样东西。

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.

其他回答

与J2EE相比,Spring是一个轻量级且灵活的框架。 弹簧容器充当了控制的反转。 Spring使用AOP,即代理和单例、工厂和模板方法设计模式。 分层架构:分离关注点和可重用层,易于维护。

Spring最初是一个相当简单的依赖注入系统。现在它很大,里面什么都有(除了众所周知的厨房水槽)。

但不用担心,它是模块化的,所以你可以只使用你想要的部件。

去看看一切从哪里开始尝试:

http://www.amazon.com/Expert-One-Design-Development-Programmer/dp/0764543857/ref=sr_1_1?ie=UTF8&s=books&qid=1246374863&sr=1-1

它可能很旧,但却是一本好书。

关于另一本关于春天的好书,请看:

http://www.amazon.com/Professional-Java-Development-Spring-Framework/dp/0764574833/ref=sr_1_2?ie=UTF8&s=books&qid=1246374863&sr=1-2

它还引用了较老版本的Spring,但绝对值得一看。

你可能想在一个web应用程序中使用Spring -

Spring MVC, which with 2.5+ allows you to use POJOs as Controller classes, meaning you don't have to extend from any particular framework (as in Struts or Spring pre-2.5). Controller classes are also dead simple to test thanks in part to dependency injection Spring integration with Hibernate, which does a good job of simplifying work with that ORM solution (for most cases) Using Spring for a web app enables you to use your Domain Objects at all levels of the application - the same classes that are mapped using Hibernate are the classes you use as "form beans." By nature, this will lead to a more robust domain model, in part because it's going to cut down on the number of classes. Spring form tags make it easier to create forms without much hassle.

此外,Spring是巨大的——所以在web应用程序中使用很多其他的东西你可能会感兴趣,比如Spring AOP或Spring Security。但上面列出的四件事描述了Spring在web应用程序中使用的常见组件。

公认的答案不涉及注释的使用,因为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是Enterprise JavaBeans (EJB)技术的一个很好的替代方案。它还具有web框架和web服务框架组件。