我听说了很多关于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.
其他回答
Spring框架绝对适合web开发,尤其适合restful api服务。
这是因为它的依赖注入和与其他模块的集成,如spring security、spring aop、mvc框架、微服务
在任何应用程序中,安全性都很可能是一个需求。 如果您的目标是构建一个需要长期维护的产品,那么您将需要利用Aop的概念。 如果你的应用程序有太多的流量,从而增加了负载,你需要使用微服务的概念。
Spring在一个平台中提供了所有这些特性。支持多个模块。 最重要的是,spring是开源的和可扩展的框架,在生命周期中到处都有钩子来集成自定义代码。
Spring Data是一个提供与您的项目集成的项目。
所以弹簧几乎可以满足所有的需求。
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
简而言之,我认为Spring是应用程序中的“粘合剂”。它用于集成不同的框架和您自己的代码。
公认的答案不涉及注释的使用,因为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非常适合将类实例粘合在一起。你知道你的Hibernate类总是需要一个数据源,Spring将它们连接在一起(也有一个数据源的实现)。
数据访问对象总是需要Hibernate访问,Spring将Hibernate类连接到dao中。
此外,Spring基本上为您提供了一系列库的可靠配置,并指导您应该使用哪些库。
Spring真的是一个很棒的工具。(我说的不是Spring MVC,只是基本框架)。
推荐文章
- “比较法违反其总合同!”
- 从Java项目生成UML类图
- 正确地从一个<Integer>的列表中移除一个整数
- Java开关语句:需要常量表达式,但它是常量
- Java的assertEquals方法可靠吗?
- 如何在Java中获得系统变量值?
- 比较Java中2个XML文档的最佳方法
- Java SE 8有对或元组吗?
- Java 8流的.min()和.max():为什么编译?
- 生成1和10之间的随机数Java
- 我如何从资产文件夹解析一个本地JSON文件到一个ListView?
- 将枚举转换为集合/列表
- 在Java中创建对象数组
- Java中字符串的字节数
- IntelliJ IDEA with Junit 4.7”!!JUnit 3.8或更高版本: