我听说了很多关于Spring的事情,人们在网上说Spring是一个很好的web开发框架。简而言之,Spring框架到底是用来干什么的?为什么我要用它而不是纯Java。
当前回答
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是应用程序中的“粘合剂”。它用于集成不同的框架和您自己的代码。
春天有三样东西。
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为配置引入了对各种注释的支持。
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只是一个依赖注入框架(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一开始是依赖注入,然后为几乎所有的东西添加了包装之王(JPA实现的包装等)。
说来话长……Spring的大多数部分更倾向于XML解决方案(XML脚本引擎…brrrr),所以对于DI,我使用Guice
很好的库,但是随着依赖性的增长,例如Spring JDBC(可能是一个具有实名参数的Java JDBC解决方案)将从maven 4-5继承。
使用Spring MVC(“big Spring”的一部分)进行web开发…它是“基于请求的”框架,有“请求vs组件”的圣战…由你决定
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap