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


当前回答

Spring核心技术参考文档描述了bean是什么。

根据Spring IoC容器和bean的介绍部分(其中“IoC”表示“控制反转”):

在Spring中,构成应用程序主干并由Spring IoC容器管理的对象称为bean。bean是由Spring IoC容器实例化、组装和管理的对象。否则,bean只是应用程序中的众多对象之一。bean以及它们之间的依赖关系反映在容器使用的配置元数据中。

Bean和作用域在Bean作用域部分中描述:

When you create a bean definition, you create 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, as with a class, you can create many object instances 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 control the scope of the objects created from a particular bean definition. This approach is powerful and flexible, because you can 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.

其他回答

在Spring中,那些构成应用程序主干并由Spring IoC管理的对象 容器被称为bean。bean只是一个实例化、组装等的对象 由Spring IoC容器管理;

Spring beans are classes. Instead of instantiating a class (using new), you get an instance as a bean cast to your class type from the application context, where the bean is what you configured in the application context configuration. This way, the whole application maintains singleton-scope instance throughout the application. All beans are initialized following their configuration order right after the application context is instantiated. Even if you don't get any beans in your application, all beans instances are already created the moment after you created the application context.

你只理解了一部分。您必须根据需要定制bean,并通知Spring容器在需要时管理它,方法是使用Martin Fowler创造的通常称为IoC(控制反转)的方法,也称为依赖注入(DI)。

您以某种方式连接bean,这样您就不必关心实例化或计算bean上的任何依赖项。这就是众所周知的好莱坞原则。

谷歌是最好的工具,除了在这个问题中你会被淹没的链接之外,还可以探索更多关于这个问题的内容。:)

Spring beans are just object instances that are managed by the Spring IOC container. Spring IOC container carry the Bag of Bean.Bean creation,maintain and deletion are the responsibilities of Spring Container. We can put the bean in to Spring by Wiring and Auto Wiring. Wiring mean we manually configure it into the XML file. 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.

For Spring, all objects are beans! The fundamental step in the Spring Framework is to define your objects as beans. Beans are nothing but object instances that would be created by the spring framework by looking at their class definitions. These definitions basically form the configuration metadata. The framework then creates a plan for which objects need to be instantiated, which dependencies need to be set and injected, the scope of the newly created instance, etc., based on this configuration metadata. The metadata can be supplied in a simple XML file, just like in the first chapter. Alternatively, one could provide the metadata as Annotation or Java Configuration.

书:Just Spring