Springでの注釈方式のデフォルトのbeanName生成ルール
Springでの注釈方式のデフォルトのbeanName生成ルール
Springでは、beanを配置するとき、nameを指定しなくてもいいです.そうすると、SpringはデフォルトのbeanNameを生成します.ルールは簡単に類名の頭文字を小文字にするだけだと思っていましたが、今日Spring公式文書を見たら、それだけではないことが分かりました.オリジナルのクラス名をbeanNameとする.分析してみます.
原文は以下の通りです
With component scanning the claspath、Spring geneneneraates bean names for unnamed coponents、フォフォローwing the rules aboove:esentially、taing the simple clasandand turnininititial charactr to lowese.Howese thethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethetheup per case、the original casing gets preserved.These are the same rules as defined by
springは、名前のないコンポーネントにbean nameを生成する際に、簡単なクラス名を採用し、その初期文字を小文字に変換するという意味です.ただし、特殊な場合には、最初の文字と2番目の文字が大文字である場合には、元の形を保持します.スプリングは
FooBahを入力してfooBahを出力しますが、入力URLの出力は依然としてURLです.この部分のコードも理解できます.読者自身で読んでください.
Springがどのように生成されたかを深く理解していきたいです.この記事SpringのデフォルトbeanNameを参照してください.
Springにおける注釈とXMLの異なる生成ルール:
Springはコアインターフェース
たとえば:
a.注釈方式
b.xml方式
Springでは、beanを配置するとき、nameを指定しなくてもいいです.そうすると、SpringはデフォルトのbeanNameを生成します.ルールは簡単に類名の頭文字を小文字にするだけだと思っていましたが、今日Spring公式文書を見たら、それだけではないことが分かりました.オリジナルのクラス名をbeanNameとする.分析してみます.
原文は以下の通りです
With component scanning the claspath、Spring geneneneraates bean names for unnamed coponents、フォフォローwing the rules aboove:esentially、taing the simple clasandand turnininititial charactr to lowese.Howese thethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethetheup per case、the original casing gets preserved.These are the same rules as defined by
java.beans.Introspector.decapitalize
(which Spring is using here)springは、名前のないコンポーネントにbean nameを生成する際に、簡単なクラス名を採用し、その初期文字を小文字に変換するという意味です.ただし、特殊な場合には、最初の文字と2番目の文字が大文字である場合には、元の形を保持します.スプリングは
java.beans.Introspector
クラスのdecapitalize
方法で実現される.この方法を見てみましょう. /**
* Utility method to take a string and convert it to normal Java variable
* name capitalization. This normally means converting the first
* character from upper case to lower case, but in the (unusual) special
* case when there is more than one character and both the first and
* second characters are upper case, we leave it alone.
*
* Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
* as "URL".
*
* @param name The string to be decapitalized.
* @return The decapitalized version of the string.
*/
public static String decapitalize(String name) {
if (name == null || name.length() == 0) {
return name;
}
if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
Character.isUpperCase(name.charAt(0))){
return name;
}
char chars[] = name.toCharArray();
chars[0] = Character.toLowerCase(chars[0]);
return new String(chars);
}
この方法は、注釈から分かるように、stringを通常のjava変数に変換する方法です.さらに例を挙げた.FooBahを入力してfooBahを出力しますが、入力URLの出力は依然としてURLです.この部分のコードも理解できます.読者自身で読んでください.
Springがどのように生成されたかを深く理解していきたいです.この記事SpringのデフォルトbeanNameを参照してください.
Springにおける注釈とXMLの異なる生成ルール:
Springはコアインターフェース
BeanNameGenerator
を通じてbeanNameを生成しています.現在は具体的な実装クラスは2つしかありません.一つはXMLに対して注釈するためのものです.この2つの方法はルールが同じではありません.XML方式の生成ルールは、クラス名+「貓」+数字です.注釈方式によるルール生成は、呼び出しを使用する上記のdecapitalize
方法である.たとえば:
a.注釈方式
@Component
public class UserService() {
... }
IoC容器の中のbeanNameはuserServiceです.b.xml方式
package com.fishblog.service
public class UserService() {
... }
<bean class="com.fishblog.service.UserService">bean>
IoC容器の中のbeanNameはcome.fish blog.service.UserService