没有 id 或 name 的 Spring bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/322208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring bean with no id or name
提问by bmw0128
I'm reviewing some Spring code, and I see a few bean defs that do not have an id or a name. The person who did it is not around to ask. The application is working fine. I am not familiar what this necessarily means. Anybody know if this means anything in particular?
我正在一些 Spring 代码,我看到一些没有 id 或名称的 bean defs。做这件事的人不在身边问。该应用程序运行良好。我不熟悉这必然意味着什么。有人知道这是否意味着什么特别的事情吗?
采纳答案by Spencer Kormos
Some beans are not required to be accessed by other beans in the context file or programmatically. So as mentioned by JacobM, they don't require an id or name as they're not referenced.
某些 bean 不需要被上下文文件中的其他 bean 或以编程方式访问。因此,正如 JacobM 所提到的,它们不需要 id 或名称,因为它们没有被引用。
Such an example would be a PropertyPlaceholderConfigurer, which reads a property file, then allows for runtime property replacement in the context definition.
此类示例是PropertyPlaceholderConfigurer,它读取属性文件,然后允许在上下文定义中替换运行时属性。
The example definition would be
示例定义将是
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="myapp.properties" />
</bean>
The JavaDoc provides further documentation on this object, but further on in the file you can reference properties from your file by just using the standard template replace placeholder ${...}.
JavaDoc 提供了关于此对象的更多文档,但在文件中,您可以通过仅使用标准模板替换占位符 ${...} 来引用文件中的属性。
回答by Jacob Mattison
One possibility is that you can define a bean in place, and so you don't need an id since you don't need to refer to it from anywhere else. Say I have a Foo object that takes a Bar property:
一种可能性是您可以就地定义一个 bean,因此您不需要 id,因为您不需要从其他任何地方引用它。假设我有一个带有 Bar 属性的 Foo 对象:
<bean id="foo" class="Foo">
<property name="bar">
<bean class="Bar">
</property>
</bean>
The Bar bean doesn't need a name because it's only used to set that one property.
Bar bean 不需要名称,因为它仅用于设置该属性。
回答by kgiannakakis
Check the possibility of auto-wiring. An other bean could reference the unnamed bean by having the autowire property set to byType.
检查自动接线的可能性。另一个 bean 可以通过将 autowire 属性设置为 byType 来引用未命名的 bean。
This is just a guess. Without a concrete example, I can't say any more.
这只是一个猜测。没有具体的例子,我不能多说。
回答by digitalsanctum
The id and name attributes are optional and are used to reference the bean definition from other definitions. Look at the official Spring documentationfor more detail.
id 和 name 属性是可选的,用于从其他定义中引用 bean 定义。有关更多详细信息,请查看官方 Spring 文档。
回答by swaroop
take a look at https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#beans-beannameit says,
You are not required to supply a name or id for a bean. If no name or id is supplied explicitly, the container generates a unique name for that bean. However, if you want to refer to that bean by name, through the use of the ref element or Service Locator style lookup, you must provide a name. Motivations for not supplying a name are related to using inner beans and autowiring collaborators.
您不需要为 bean 提供名称或 ID。如果没有明确提供名称或 id,容器将为该 bean 生成一个唯一的名称。但是,如果要通过名称引用该 bean,通过使用 ref 元素或服务定位器样式查找,则必须提供名称。不提供名称的动机与使用内部 bean 和自动装配协作者有关。
Also, Beans such as BeanPostProcessor, BeanFactoryPostProcessor and PropertyPlaceholderConfigurer are automatically detected by application context and typically won't have name
此外,诸如 BeanPostProcessor、BeanFactoryPostProcessor 和 PropertyPlaceholderConfigurer 之类的 Bean 会被应用程序上下文自动检测到,并且通常没有名称
If you consider any spring bean, Spring mandates it to have an identifier. In case, if you have not provided any identifier (via id or name attribute) to a bean in your configuration, you won't run into exceptions. Spring will manage such situation by assigning a default identifier. it has BeanNameGenerator to assign default name. <bean class="com.package.name.TestBean">will be named as "com.package.name.TestBean" @Beankind of beans will have its method name as bean name
如果您考虑任何 spring bean,Spring 会要求它有一个标识符。如果您没有在配置中为 bean 提供任何标识符(通过 id 或 name 属性),则不会遇到异常。Spring 将通过分配默认标识符来管理这种情况。它有 BeanNameGenerator 来分配默认名称。<bean class="com.package.name.TestBean">将被命名为“com.package.name.TestBean”@Bean类的bean将其方法名称作为bean名称
thus, in your code, for some reason, you can skip naming few beans and the code still works if you are accessing those beans with their default name
因此,在您的代码中,出于某种原因,您可以跳过命名几个 bean,如果您使用默认名称访问这些 bean,代码仍然有效
credits : https://www.javacodegeeks.com/2013/02/spring-bean-names.html
学分:https: //www.javacodegeeks.com/2013/02/spring-bean-names.html
回答by Chochos
Beans without id or name can still be referenced by the class name. Spring names those beans automatically using the class name and if there is more than one bean of the same class it appends a number to them. Anonymous beans are usually defined inside a property tag, but if they're just there maybe there's autowiring configured in some other beans. Anyway, I think adding a name or id to those beans won't break your application.
没有 id 或 name 的 bean 仍然可以通过类名引用。Spring 使用类名自动为这些 bean 命名,如果同一个类有多个 bean,它会向它们附加一个数字。匿名 bean 通常定义在属性标签内,但如果它们只是存在,则可能在其他一些 bean 中配置了自动装配。无论如何,我认为向这些 bean 添加名称或 ID 不会破坏您的应用程序。
回答by Chochos
As a couple of people mentioned above, not all bean-grabbing is based on name/ID; some of it is based on type. For example, there is a method
正如上面提到的几个人,并非所有的抢豆都是基于名称/ID;其中一些是基于类型的。例如,有一个方法
BeanFactoryUtils.beansOfTypeIncludingAncestors(...)
BeanFactoryUtils.beansOfTypeInducingAncestors(...)
that grabs all the beans of some given type. This is used for example by the Spring Web MVC DispatcherServlet (among many other places) to discover beans by type, such as HandlerMappings, HandlerAdapters, HandlerExceptionResolvers and so forth. Contrast this with cases where the bean must have a specific well-known name/ID to be found, such as the LocaleResolver (ID must be "localeResolver" or it won't be found) and ThemeResolver (ID must be "themeResolver" or it won't be found).
抓取某种给定类型的所有 bean。例如,Spring Web MVC DispatcherServlet(以及许多其他地方)使用它来按类型发现 bean,例如 HandlerMappings、HandlerAdapters、HandlerExceptionResolvers 等。将此与 bean 必须具有特定众所周知的名称/ID 才能找到的情况进行对比,例如 LocaleResolver(ID 必须是“localeResolver”,否则将找不到)和 ThemeResolver(ID 必须是“themeResolver”或不会被发现)。
回答by Jarek Krochmalski
Beans defined without name and ID can be accessed with a generated ID (full package name and class name), for example:
没有名称和 ID 定义的 Bean 可以使用生成的 ID(完整的包名和类名)访问,例如:
bean defined as
豆定义为
<bean class="pl.finsys.initOrder.TestBeanImpl">
can be accessed by
可以通过
TestBean bean = (TestBean) ctx.getBean("pl.finsys.initOrder.TestBeanImpl");
回答by Krishna
//Bean Cfg File without Bean id
//没有Bean id的Bean Cfg文件
<bean class="com.ds.DemoBean">
<property name="msg" value="Hello"/>
</bean>
<bean class="com.ds.DemoBean">
<property name="msg" value="Hello"/>
</bean>
//We can Access
//我们可以访问
Object obj=factory.getBean("com.ds.DemoBe
Object obj=factory.getBean("com.ds.DemoBe
回答by Prasanna Kumar
its not a mandatory to provide java Bean Id..If we are not providing Bean Id,our Container Provides the Default Been Id.Default Bean Id look like as "(Package Name).(Bean Class Name)#N"where N=0,1,2,......etc.
提供 java Bean Id 不是强制性的。 0,1,2,......等

