我听说了很多关于Spring的事情,人们在网上说Spring是一个很好的web开发框架。简而言之,Spring框架到底是用来干什么的?为什么我要用它而不是纯Java。
当前回答
Spring是Enterprise JavaBeans (EJB)技术的一个很好的替代方案。它还具有web框架和web服务框架组件。
其他回答
其优点是依赖注入(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最初是一个相当简单的依赖注入系统。现在它很大,里面什么都有(除了众所周知的厨房水槽)。
但不用担心,它是模块化的,所以你可以只使用你想要的部件。
去看看一切从哪里开始尝试:
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
过去,我从纯技术的角度考虑Spring框架。
根据一些团队工作和开发企业web应用程序的经验,我会说Spring通过解耦各个元素(bean)来实现更快的应用程序(web应用程序)开发。更快的发展使它如此受欢迎。Spring允许将构建(连接)应用程序的责任转移到Spring框架上。Spring框架的依赖注入负责将单个bean连接/连接到工作应用程序中。
这样,一旦定义了bean之间的接口,开发人员就可以更专注于单个组件(bean)的开发。
测试这样的应用程序很容易——主要的关注点是单个bean。它们可以很容易地解耦和模拟,因此单元测试是快速和有效的。
Spring框架定义了多个专门的bean,如@Controller (@Restcontroller)、@Repository、@Component来服务于web目的。Spring和Maven一起提供了一个对开发人员来说很直观的结构。 团队工作简单快捷,因为单独的元素是分开的,可以重复使用。
公认的答案不涉及注释的使用,因为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注释。
推荐文章
- 如果我同步了同一个类上的两个方法,它们能同时运行吗?
- 如何减去X天从一个日期对象在Java?
- Java标识符中的“连接字符”是什么?
- 一对多、多对一、多对多的区别?
- 从枚举中选择一个随机值?
- 如何从URL获取参数与JSP
- 如何在Eclipse中生成Javadoc注释?
- 找到java类从哪里加载
- 从集合中随机选取一个元素
- 为什么x == (x = y)和(x = y) == x不一样?
- 什么Java 8流。收集等价物可在标准Kotlin库?
- 等待未来的名单
- 如何检查JSON键是否存在?
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 即使从未抛出异常,使用try-catch块的代价是否昂贵?