Spring Framework究竟是什么? [关闭]

本文翻译自:What exactly is Spring Framework for? [closed]

I hear a lot about Spring , people are saying all over the web that Spring is a good framework for web development. 我听到很多关于Spring的消息 ,人们在网上都说Spring是一个很好的Web开发框架。 What exactly is Spring Framework for? Spring Framework究竟是什么?


#1楼

参考:https://stackoom.com/question/4SCT/Spring-Framework究竟是什么-关闭


#2楼

Spring is great for gluing instances of classes together. Spring非常适合将类的实例粘合在一起。 You know that your Hibernate classes are always going to need a datasource, Spring wires them together (and has an implementation of the datasource too). 您知道您的Hibernate类总是需要一个数据源,Spring将它们连接在一起(并且还具有数据源的实现)。

Your data access objects will always need Hibernate access, Spring wires the Hibernate classes into your DAOs for you. 您的数据访问对象将始终需要Hibernate访问,Spring将Hibernate类连接到您的DAO中。

Additionally, Spring basically gives you solid configurations of a bunch of libraries, and in that, gives you guidance in what libs you should use. 另外,Spring基本上为您提供了一堆库的可靠配置,并为此提供了应该使用哪些库的指导。

Spring is really a great tool. Spring真的是一个很棒的工具。 (I wasn't talking about Spring MVC, just the base framework). (我不是在谈论Spring MVC,只是基础框架)。


#3楼

Spring contains ( as Skaffman rightly pointed out ) a MVC framework. Spring 包含正如Skaffman正确指出的 )一个MVC框架。 To explain in short here are my inputs. 这里简单解释一下我的意见。 Spring supports segregation of service layer, web layer and business layer, but what it really does best is "injection" of objects. Spring支持服务层,Web层和业务层的隔离,但它最擅长的是对象的“注入”。 So to explain that with an example consider the example below: 所以用一个例子解释一下,考虑下面的例子:

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
   }
}

Now in your code you have a class called RoadTrip as follows 现在在您的代码中,您有一个名为RoadTrip的类,如下所示

public class RoadTrip
{
    private FourWheel myCarForTrip;
}

Now whenever you want a instance of Trip; 现在每当你想要一个Trip的实例; sometimes you may want a SUV to initialize FourWheel or sometimes you may want Sedan. 有时你可能想要一辆SUV来初始化FourWheel,或者有时你可能想要Sedan。 It really depends what you want based on specific situation. 这取决于你想要的具体情况。

To solve this problem you'd want to have a Factory Pattern as creational pattern. 要解决此问题,您需要将工厂模式作为创建模式。 Where a factory returns the right instance. 工厂返回正确实例的地方。 So eventually you'll end up with lots of glue code just to instantiate objects correctly. 所以最终你会得到很多胶水代码,只是为了正确地实例化对象。 Spring does the job of glue code best without that glue code. 没有胶水代码,Spring可以最好地完成胶水代码的工作。 You declare mappings in XML and it initialized the objects automatically. 您在XML中声明映射并自动初始化对象。 It also does lot using singleton architecture for instances and that helps in optimized memory usage. 对于实例,它也使用单例体系结构,并且有助于优化内存使用。

This is also called Inversion Of Control. 这也称为控制反转。 Other frameworks to do this are Google guice, Pico container etc. 其他框架是谷歌guice,Pico容器等。

Apart from this, Spring has validation framework, extensive support for DAO layer in collaboration with JDBC, iBatis and Hibernate (and many more). 除此之外,Spring还有验证框架,与JDBC,iBatis和Hibernate(以及更多)合作,对DAO层提供了广泛的支持。 Provides excellent Transactional control over database transactions. 为数据库事务提供出色的事务控制。

There is lot more to Spring that can be read up in good books like "Pro Spring". 春天还有很多东西可以在像“Pro Spring”这样的好书中读到。

Following URLs may be of help too. 以下网址也可能有所帮助。
http://static.springframework.org/docs/Spring-MVC-step-by-step/ http://static.springframework.org/docs/Spring-MVC-step-by-step/
http://en.wikipedia.org/wiki/Spring_Framework http://en.wikipedia.org/wiki/Spring_Framework
http://www.theserverside.com/tt/articles/article.tss?l=SpringFramework http://www.theserverside.com/tt/articles/article.tss?l=SpringFramework


#4楼

Spring started off as a fairly simple dependency injection system. Spring开始时是一个相当简单的依赖注入系统。 Now it is huge and has everything in it (except for the proverbial kitchen sink). 现在它很大,里面装着一切(除了众所周知的厨房水槽)。

But fear not, it is quite modular so you can use just the pieces you want. 但不要害怕,它非常模块化,所以你可以只使用你想要的碎片。

To see where it all began try: 要了解它的全部内容,请尝试:

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/Expert-One-Design-Development-Programmer/dp/0764543857/ref=sr_1_1?ie=UTF8&s=books&qid=1246374863&sr=1-1

It might be old but it is an excellent book. 它可能很旧,但它是一本很好的书。

For another good book this time exclusively devoted to Spring see: 这次专门讨论Spring的另一本好书:

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

It also references older versions of Spring but is definitely worth looking at. 它也引用了旧版本的Spring,但绝对值得一看。


#5楼

Basically Spring is a framework for which is a pattern that allows to build very decoupled systems. 基本上Spring是的框架,它是一种允许构建非常分离的系统的模式。

The problem 问题

For example, suppose you need to list the users of the system and thus declare an interface called UserLister : 例如,假设您需要列出系统的用户,从而声明一个名为UserLister的接口:

public interface UserLister {
    List<User> getUsers();
}

And maybe an implementation accessing a database to get all the users: 也许是访问数据库以获取所有用户的实现:

public class UserListerDB implements UserLister {
    public List<User> getUsers() {
        // DB access code here
    }
}

In your view you'll need to access an instance (just an example, remember): 在您的视图中,您将需要访问一个实例(只是一个示例,请记住):

public class SomeView {
    private UserLister userLister;

    public void render() {
        List<User> users = userLister.getUsers();
        view.render(users);
    }
}

Note that the code above doesn't have initialized the variable userLister . 请注意,上面的代码没有初始化变量userLister What should we do? 我们应该做什么? If I explicitly instantiate the object like this: 如果我显式实例化这样的对象:

UserLister userLister = new UserListerDB();

...I'd couple the view with my implementation of the class that access the DB. ...我将视图与访问数据库的类的实现结合起来。 What if I want to switch from the DB implementation to another that gets the user list from a comma-separated file (remember, it's an example)? 如果我想从数据库实现切换到另一个从逗号分隔文件中获取用户列表的内容(请记住,这是一个示例),该怎么办? In that case I would go to my code again and change the last line by: 在那种情况下,我会再次访问我的代码,并通过以下方式更改最后一行:

UserLister userLister = new UserListerCommaSeparatedFile();

This has no problem with a small program like this but... What happens in a program that has hundreds of views and a similar number of business classes. 这对于像这样的小程序没有问题,但是...在具有数百个视图和相似数量的业务类的程序中会发生什么。 The maintenance becomes a nightmare! 维护变成了一场噩梦!

Spring (Dependency Injection) approach Spring(依赖注入)方法

What Spring does is to wire the classes up by using an XML file or annotations, this way all the objects are instantiated and initialized by Spring and injected in the right places (Servlets, Web Frameworks, Business classes, DAOs, etc, etc, etc...). 什么春天确实是使用XML文件或注解,这样所有的对象实例化和春天初始化,并在适当的地方(Servlet的Web框架,业务类,DAO的,等,等,等注入接线班达...)。

Going back to the example in Spring we just need to have a setter for the userLister field and have either an XML file like this: 回到Spring的例子,我们只需要为userLister字段设置一个setter,并且有一个像这样的XML文件:

<bean id="userLister" class="UserListerDB" />

<bean class="SomeView">
    <property name="userLister" ref="userLister" />
</bean>

or more simply annotate the filed in our view class with @Inject : 或者更简单地使用@Inject在我们的视图类中注释该@Inject

@Inject
private UserLister userLister;

This way when the view is created it magically will have a UserLister ready to work. 这样,当创建视图时,它会神奇地使UserLister准备好工作。

List<User> users = userLister.getUsers();  // This will actually work
                                           // without adding any line of code

It is great! 太棒了! Isn't it? 不是吗?

  • What if you want to use another implementation of your UserLister interface? 如果要使用UserLister接口的其他实现,该怎么办? Just change the XML 只需更改XML即可
  • What if don't have a UserLister implementation ready? 如果没有准备好UserLister实现怎么办? Program a temporal mock implementation of UserLister and ease the development of the view UserLister的临时模拟实现并简化视图的开发
  • What if I don't want to use Spring anymore? 如果我不想再使用Spring怎么办? Just don't use it! 只是不要使用它! Your application isn't coupled to it. 您的应用程序未与其耦合。 Inversion of Control states: "The application controls the framework, not the framework controls the application". 控制反转状态:“应用程序控制框架,而不是框架控制应用程序”。

There are some other options for Dependency Injection around there, what in my opinion has made Spring so famous besides its simplicity, elegance and stability is that the guys of SpringSource have programmed many many POJOs that help to integrate Spring with many other common frameworks without being intrusive in your application. 在那里有一些其他的依赖注入选项,我认为Spring除了简单,优雅和稳定之外如此着名,SpringSource的人已经编写了许多POJO,它们有助于将Spring与许多其他常见框架集成,而不是侵入你的应用程序。 Also Spring has several good subprojects like Spring MVC, Spring WebFlow, Spring Security and again a loooong list of etceteras. Spring还有几个很好的子项目,比如Spring MVC,Spring WebFlow,Spring Security,还有一些loooong的etceteras列表。

Hope this helps. 希望这可以帮助。 Anyway, I encourage you to read Martin Fowler's article about Dependency Injection and Inversion of Control because he does it better than me. 无论如何,我鼓励你阅读Martin Fowler关于依赖注入和控制反转的文章 ,因为他做得比我好。 After understanding the basics take a look to Spring Documentation , in my opinion it is used to be the best Spring book ever. 了解基础知识去看一下后 Spring文档 ,在我看来 曾经是最好的春季书永远。


#6楼

What you'd probably want in a web application with Spring - 在Spring的Web应用程序中你可能想要的东西 -

  • 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). Spring MVC,2.5+允许您使用POJO作为Controller类,这意味着您不必从任何特定框架扩展(如Struts或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) Spring与Hibernate集成,可以很好地简化ORM解决方案的工作(大多数情况下)
  • 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." 将Spring用于Web应用程序使您可以在应用程序的所有级别使用域对象 - 使用Hibernate映射的相同类是您用作“表单bean”的类。 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表单标签可以更轻松地创建表单而不会有太多麻烦。

In addition, Spring is HUGE - so there are a lot of other things you might be interested in using in a web app such as Spring AOP or Spring Security. 另外,Spring很大 - 所以你可能有兴趣在Spring AOP或Spring Security等Web应用程序中使用很多其他东西。 But the four things listed above describe the common components of Spring that are used in a web app. 但是上面列出的四件事描述了在Web应用程序中使用的Spring的常见组件。

发布了0 篇原创文章 · 获赞 52 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/CHCH998/article/details/105618097