春豆是什么? [英] What in the world are Spring beans?

查看:161
本文介绍了春豆是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到我能理解的Spring bean的高级定义。我经常在Grails文档和书中看到它们,但我认为理解它们是有益的。那么Spring bean是什么?他们如何使用?它们与依赖注入有什么关系?

解决方案


构成您的骨干的对象应用程序,并且由Spring IoC *容器管理的
被称为bean。一个bean是一个
对象,它被实例化,组装并由
的Spring IoC容器管理。这些bean是使用您提供给容器的配置
元数据创建的,例如,以
XML定义的形式。


有关豆和范围的更多信息,请参见 SpringSource

lockquote

当您创建一个bean定义时,您实际创建的是一个
的配方创建由bean
定义的类的实际实例。 bean定义是一个配方的想法是很重要的,
,因为它意味着,就像一个类一样,你可能有
多个从一个配方创建的对象实例。



您不仅可以控制要插入到从
特定的bean定义创建的对象中的各种依赖项和配置
值,还可以控制对象从特定的bean定义中创建
。这种方法非常强大,
让您可以灵活地选择您通过配置创建的对象的范围,而不必在Java类中烘烤对象的作用域
水平。豆可以被定义为
部署在多个作用域中的一个


* IoC:控制反转


I am yet to find a high-level definition of Spring beans that I can understand. I see them referenced often in Grails documentation and books, but I think that understanding what they are would be beneficial. So what are Spring beans? How can they be used? Do they have something to do with Dependency Injection?

解决方案

The objects that form the backbone of your application and that are managed by the Spring IoC* container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container, for example, in the form of XML definitions.

More to learn about beans and scope from SpringSource:

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

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. 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. Beans can be defined to be deployed in one of a number of scopes

*IoC: Inversion of Control

这篇关于春豆是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆