四季豆到底是什么?

本文翻译自:What in the world are Spring beans?

I am yet to find a high-level definition of Spring beans that I can understand. 我还没有找到我能理解的高级Spring bean定义。 I see them referenced often in Grails documentation and books, but I think that understanding what they are would be beneficial. 我看到Grails文档和书籍中经常引用它们,但是我认为了解它们的好处将是有益的。 So what are Spring beans? 那么什么是春豆呢? How can they be used? 如何使用它们? Do they have something to do with Dependency Injection? 他们和依赖注入有关系吗?


#1楼

参考:https://stackoom.com/question/1A8mL/四季豆到底是什么


#2楼

The objects that form the backbone of your application and that are managed by the Spring IoC* container are called beans. 构成应用程序主干并由Spring IoC *容器管理的对象称为bean。 A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Bean是由Spring IoC容器实例化,组装和以其他方式管理的对象。 These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML <bean/> definitions. 这些bean是使用您提供给容器的配置元数据创建的,例如,以XML <bean/>定义的形式。

More to learn about beans and scope from SpringSource : SpringSource了解更多关于bean和作用域的信息:

When you create a bean definition what you are actually creating is a recipe for creating actual instances of the class defined by that bean definition. 当您创建bean定义时,您实际创建的是创建该bean定义所定义的类的实际实例的方法。 The idea that a bean definition is a recipe is important, because it means that, just like a class, you can potentially have many object instances created from a single recipe. bean定义是配方的想法很重要,因为它意味着像一个类一样,您可能从一个配方中创建了许多对象实例。

You can control not only the various dependencies and configuration values that are to be plugged into an object that is created from a particular bean definition, but also the scope of the objects created from a particular bean definition. 您不仅可以控制要插入到从特定bean定义创建的对象中的各种依赖项和配置值,还可以控制从特定bean定义创建的对象的范围。 This approach is very powerful and gives you the flexibility to choose the scope of the objects you create through configuration instead of having to 'bake in' the scope of an object at the Java class level. 这种方法非常强大,可让您灵活地选择通过配置创建的对象的范围,而不必在Java类级别上“接受”对象的范围。 Beans can be defined to be deployed in one of a number of scopes 可以将Bean定义为部署在多个范围之一中

*IoC: Inversion of Control * IoC:控制反转


#3楼

Spring beans are just object instances that are managed by the Spring container, namely, they are created and wired by the framework and put into a "bag of objects" (the container) from where you can get them later. Spring bean只是由Spring容器管理的对象实例,即,它们是由框架创建和连接的,并放入“对象袋”(容器)中,以后可以从那里获取它们。

The "wiring" part there is what dependency injection is all about, what it means is that you can just say "I will need this thing" and the framework will follow some rules to get you the proper instance. “接线”部分就是依赖注入的全部含义,这意味着您可以说“我将需要这个东西”,并且框架将遵循一些规则来为您提供适当的实例。

For someone who isn't used to Spring, I think Wikipedia Spring's article has a nice description : 对于不习惯使用Spring的人,我认为Wikipedia Spring的文章有一个很好的描述

Central to the Spring Framework is its inversion of control container, which provides a consistent means of configuring and managing Java objects using reflection. Spring框架的核心是控制容器的倒置,它提供了使用反射配置和管理Java对象的一致方法。 The container is responsible for managing object lifecycles of specific objects: creating these objects, calling their initialization methods, and configuring these objects by wiring them together. 容器负责管理特定对象的对象生命周期:创建这些对象,调用其初始化方法以及通过将它们连接在一起来配置这些对象。

Objects created by the container are also called managed objects or beans . 容器创建的对象也称为托管对象bean The container can be configured by loading XML files or detecting specific Java annotations on configuration classes. 可以通过加载XML文件或检测配置类上的特定Java注释来配置容器。 These data sources contain the bean definitions which provide the information required to create the beans. 这些数据源包含Bean定义,这些定义提供了创建Bean所需的信息。

Objects can be obtained by means of either dependency lookup or dependency injection . 可以通过依赖关系查找依赖关系注入来获取对象。 Dependency lookup is a pattern where a caller asks the container object for an object with a specific name or of a specific type. 依赖关系查找是一种模式,在此模式下,调用者向容器对象询问具有特定名称或特定类型的对象。 Dependency injection is a pattern where the container passes objects by name to other objects, via either constructors, properties, or factory methods. 依赖注入是一种模式,其中容器通过构造函数,属性或工厂方法将名称按名称传递给其他对象。


#4楼

Well you understood it partially. 好吧,您对此有所了解。 You have to tailor the beans according to your need and inform Spring container to manage it when required, by using a methodology populalrly known as IoC ( Inversion of Control ) coined by Martin Fowler , also known as Dependency Injection (DI). 您必须根据需要定制bean,并通过使用Martin Fowler提出的俗称为IoC( 控制反转 )的方法(也称为依赖注入 (DI))来通知Spring容器以在需要时进行管理。

You wire the beans in a way, so that you do not have to take care of the instantiating or evaluate any dependency on the bean. 您以某种方式连接Bean,这样您就不必担心实例化或评估对Bean的任何依赖关系。 This is popularly known as Hollywood Principle . 这就是俗称的好莱坞原则

Google is the best tool to explore more on this in addition to the links you would get flooded with here in this question. 除了您将在本问题中充斥的链接之外,Google是探索更多有关此内容的最佳工具。 :) :)


#5楼

In Spring, those objects that form the backbone of your application and that are managed by the Spring IoC container are referred to as beans. 在Spring中,那些构成应用程序主干并由Spring IoC容器管理的对象称为Bean。 A bean is simply an object that is instantiated, assembled and otherwise managed by a Spring IoC container; Bean只是一个对象,可以通过Spring IoC容器实例化,组装和管理。


#6楼

Spring have the IoC container which carry the Bag of Bean ; 春天有装豆袋的IoC容器; creation maintain and deletion are the responsibilities of Spring Container. 创建,维护和删除是Spring Container的职责。 We can put the bean in to Spring by Wiring and Auto Wiring. 我们可以通过接线和自动接线将bean放入Spring。 Wiring mean we manually configure it into the XML file and "Auto Wiring" mean we put the annotations in the Java file then Spring automatically scan the root-context where java configuration file, make it and put into the bag of Spring. 接线是指我们将其手动配置到XML文件中,而“自动接线”是指将批注放入Java文件中,然后Spring会自动扫描java配置文件所在的根上下文,并将其放入Spring包中。

Here is the detail URI where you got more information about Beans 这是详细的URI,在这里您可以获得有关Beans的更多信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值