我听说了很多关于Spring的事情,人们在网上说Spring是一个很好的web开发框架。简而言之,Spring框架到底是用来干什么的?为什么我要用它而不是纯Java。
当前回答
基本上Spring是一个依赖注入框架,这种模式允许构建非常解耦的系统。
这个问题
例如,假设你需要列出系统的用户,并声明一个名为UserLister的接口:
public interface UserLister {
List<User> getUsers();
}
也许一个实现访问一个数据库来获取所有的用户:
public class UserListerDB implements UserLister {
public List<User> getUsers() {
// DB access code here
}
}
在你的视图中,你需要访问一个实例(只是一个例子,记住):
public class SomeView {
private UserLister userLister;
public void render() {
List<User> users = userLister.getUsers();
view.render(users);
}
}
注意,上面的代码没有初始化变量userLister。我们该怎么办?如果我像这样显式地实例化对象:
UserLister userLister = new UserListerDB();
...我将视图与访问DB的类的实现结合在一起。如果我想从DB实现切换到另一个从逗号分隔的文件(记住,这是一个例子)获取用户列表的实现呢?在这种情况下,我将再次转到我的代码,并将上面的行更改为:
UserLister userLister = new UserListerCommaSeparatedFile();
这样的小程序没有问题,但是……在具有数百个视图和类似数量的业务类的程序中会发生什么?维护变成了噩梦!
Spring(依赖注入)方法
Spring所做的是使用XML文件或注释将类连接起来,这样所有的对象都由Spring实例化和初始化,并注入到正确的位置(servlet、Web框架、业务类、dao,等等,等等……)
回到Spring中的例子,我们只需要为userLister字段设置一个setter,并有一个像这样的XML文件:
<bean id="userLister" class="UserListerDB" />
<bean class="SomeView">
<property name="userLister" ref="userLister" />
</bean>
或者更简单地用@Inject注释视图类中的字段:
@Inject
private UserLister userLister;
这样,当视图创建时,它将神奇地有一个UserLister准备工作。
List<User> users = userLister.getUsers(); // This will actually work
// without adding any line of code
太棒了!不是吗?
如果您想使用UserLister接口的另一种实现呢?只需更改XML。 如果没有UserLister实现呢?编写UserLister的临时模拟实现,简化视图的开发。 如果我不想再使用Spring了怎么办?千万别用!您的应用程序没有与它耦合。控制反转:“应用程序控制框架,而不是框架控制应用程序”。
还有一些其他的依赖注入选项,在我看来,除了简单、优雅和稳定之外,Spring之所以如此有名,是因为SpringSource的人编写了许多pojo,这些pojo有助于将Spring与许多其他通用框架集成在一起,而不会对应用程序造成干扰。此外,Spring还有几个不错的子项目,比如Spring MVC、Spring WebFlow、Spring Security,还有一长串等等。
无论如何,我鼓励你阅读Martin Fowler关于依赖注入和控制反转的文章,因为他在这方面做得比我好。在了解了基本知识之后,看看Spring Documentation,在我看来,它是有史以来最好的Spring书籍。
其他回答
春天有三样东西。
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.
Spring是Enterprise JavaBeans (EJB)技术的一个很好的替代方案。它还具有web框架和web服务框架组件。
你可能想在一个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最初是一个相当简单的依赖注入系统。现在它很大,里面什么都有(除了众所周知的厨房水槽)。
但不用担心,它是模块化的,所以你可以只使用你想要的部件。
去看看一切从哪里开始尝试:
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,但绝对值得一看。
Spring包含(正如Skaffman正确指出的那样)MVC框架。简而言之,这里是我的输入。 Spring支持服务层、web层和业务层的分离,但它最擅长的是对象的“注入”。用一个例子解释一下,请看下面的例子:
public interface FourWheel
{
public void drive();
}
public class Sedan implements FourWheel
{
public void drive()
{
//drive gracefully
}
}
public class SUV implements FourWheel
{
public void drive()
{
//Rule the rough terrain
}
}
现在,在代码中有一个名为RoadTrip的类,如下所示
public class RoadTrip
{
private FourWheel myCarForTrip;
}
现在,无论何时你想要一个Trip实例;有时你可能需要一个SUV来初始化FourWheel,有时你可能需要一个Sedan。这真的取决于你想要什么基于具体的情况。
为了解决这个问题,你需要一个工厂模式作为创建模式。其中工厂返回正确的实例。所以最终你会用大量的粘合代码来正确地实例化对象。在没有粘合代码的情况下,Spring可以最好地完成粘合代码的工作。您用XML声明映射,它会自动初始化对象。它还为实例使用了大量的单例架构,这有助于优化内存使用。
这也被称为控制反转。其他可以做到这一点的框架有谷歌guice, Pico container等。
除此之外,Spring还有验证框架,广泛支持与JDBC、iBatis和Hibernate(以及更多)合作的DAO层。对数据库事务提供出色的事务控制。
还有很多关于Spring的东西可以在像“Pro Spring”这样的好书中读到。
跟踪url可能也有帮助。 http://static.springframework.org/docs/Spring-MVC-step-by-step/ http://en.wikipedia.org/wiki/Spring_Framework http://www.theserverside.com/tt/articles/article.tss?l=SpringFramework
推荐文章
- 如何使一个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中获得系统变量值?