是春天getbean案件是否传染? [英] Is spring getbean case sentitive or not?

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

问题描述

当我使用 getBean(test)

我有一个像

@Component
public class TEST {
}

可以加载这个bean吗?

can this bean be loaded?

推荐答案

getBean() 区分大小写,但是Spring对 @Component @Bean 类。请参见 4.10.5命名自动检测组件

getBean() is case sensitive, however Spring uses custom bean naming strategy for @Component and @Bean classes. See 4.10.5 Naming autodetected components:


在扫描过程中自动检测组件时,它的bean名称由 BeanNameGenerator 策略[...]生成。默认情况下,任何Spring构造型注释( @Component @Repository @Service @Controller )包含 name 值,从而将该名称提供给相应的bean定义。

When a component is autodetected as part of the scanning process, its bean name is generated by the BeanNameGenerator strategy [...]. By default, any Spring stereotype annotation (@Component, @Repository, @Service, and @Controller) that contains a name value will thereby provide that name to the corresponding bean definition.

如果此类注释不包含 name 值或任何其他检测到的组件(例如由自定义过滤器),默认bean名称生成器返回非大写的非限定类名称

If such an annotation contains no name value or for any other detected component (such as those discovered by custom filters), the default bean name generator returns the uncapitalized non-qualified class name.

对于 @Configuration / @Bean 参见 4.12.4.5自定义bean命名

And for @Configuration/@Bean see 4.12.4.5 Customizing bean naming:


默认情况下,配置类使用 @Bean 方法的名称作为结果bean的名称。但是,可以使用 name 属性覆盖此功能。

By default, configuration classes use a @Bean method's name as the name of the resulting bean. This functionality can be overridden, however, with the name attribute.

返回你的问题。因为你的类没有遵循Java命名约定(驼峰式名称),所以Spring使用不寻常的名称,这将起作用:

Back to your question. Because your class is not following Java naming conventions (camel-case names) Spring uses unusual name for the bean, this will work:

getBean("TEST")

但是如果使用预期的命名( @组件类Test {} ),必须使用小写标识符:

However if you use expected naming (@Component class Test { }), you must use lower-case identifiers:

getBean("test")

此外,如果您的名字更复杂,则应用非大写的驼峰式语法(继续引用 Spring文档):

Moreover if your name is more complex, uncapitalized camel-case syntax applies (continuing to quote the Spring documentation):


[...]例如,如果检测到以下两个组件,则名称将为 myMovieLister movieFinderImpl



@Service("myMovieLister")
public class SimpleMovieLister {
  // ...
}

@Repository
public class MovieFinderImpl implements MovieFinder {
  // ...
}

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

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