programing

스프링 구성 파일에서 bean ID와 이름을 사용하는 차이점

easyjava 2023. 3. 10. 22:53
반응형

스프링 구성 파일에서 bean ID와 이름을 사용하는 차이점

이 명령어를 사용하는 것과 다른 점이 있습니까?id와 "time"을 해 주세요.name<bean>

Spring 레퍼런스 3.2.3.1 Naming Beans:

모든 콩에는 하나 이상의 ID(식별자 또는 이름이라고도 함)가 있습니다.이러한 용어는 같은 것을 가리킵니다.이러한 ID는 콩이 호스트되는 컨테이너 내에서 고유해야 합니다.콩은 거의 항상 하나의 아이디만 가지지만, 만약 콩이 두 개 이상의 아이디를 가지고 있다면, 나머지 아이디는 기본적으로 가명으로 간주될 수 있습니다.

XML 기반 구성 메타데이터를 사용하는 경우 'id' 또는 'name' 속성을 사용하여 빈 식별자를 지정합니다.id 속성을 사용하면 ID를 하나만 지정할 수 있습니다.또한 실제 XML 요소 ID 속성이기 때문에 XML 파서는 다른 요소가 ID를 참조할 때 추가 검증을 수행할 수 있습니다.따라서 빈 ID를 지정하는 것이 좋습니다.다만, XML 사양에서는, XML ID 로 유효한 문자를 제한하고 있습니다.이것은 보통 제약사항이 아닙니다만, 이러한 특수 XML 문자 중 하나를 사용해야 하거나 다른 에일리어스를 bean에 삽입해야 하는 경우 'name' 속성에 쉼표(,), 세미콜론(;) 또는 공백으로 구분하여 하나 이상의 bean ID를 지정할 수도 있습니다.

으로는 '아예'는id하고 있으며, Atribute는 XML ID Atribute의 Atribute에 준거하고 있습니다.name조금 더 유연합니다.으로 말하면, 저는 반적로,, 는는 generally를 사용합니다.name같은일 뿐이야.'만'은 '만'입니다.

3.1 이후 Spring 3.1 ★id입니다.xsd:string에서는, 에 and and, and and and of of, the and and and the the the the the the the the the the the and and와 할 수 있습니다.name여하하다

「」의 id a. a. a.name는요?name할 수 단, 에일리어스는 콤마, 세미콜론 또는 공백으로 되어 있습니다.id단일 값이어야 합니다.

Spring 3.2 매뉴얼에서 다음 내용을 참조하십시오.

XML 기반 구성 메타데이터에서는 id 및/또는 이름 속성을 사용하여 빈 식별자를 지정합니다.id 속성을 사용하면 ID를 하나만 지정할 수 있습니다.일반적으로 이러한 이름은 영숫자('myBean', 'fooService' 등)이지만 특수 문자도 사용할 수 있습니다.빈에 다른 에일리어스를 삽입할 경우 쉼표(,), 세미콜론(;) 또는 공백으로 구분하여 이름 속성으로 지정할 수도 있습니다.Spring 3.1 이전 버전에서는 id 속성이 xsd로 입력되었습니다.ID: 가능한 문자를 제한합니다. 3.1부터는 xsd:string이 됩니다. 빈 ID의 일의성은 XML 파서에 의해 더 이상 적용되지 않지만 컨테이너에 의해 강제됩니다.

어느 쪽이든 좋습니다.니즈에 따라 달라집니다.
에 「」 「」 「」 등되어 있는 ./viewSummary.html는, 「콩으로서 허가되지 않습니다.id「XML ID」입니다.할 수 .idname★★★★★★ 。
name는 attribute의 이 됩니다.alias콩의 경우 특정 콩에 대해 여러 식별자를 지정할 수 있습니다.

아래 질문에 대답하겠습니다.

id 애트리뷰트를 사용하는 것과 <bean> 태그의 이름 애트리뷰트를 사용하는 것에 차이가 있습니까?

< 태그를 볼 수 .<bean> 아이디로 사용해도 마찬가지입니다.

어떻게?

name 모두 합니다(는 의미하지 않습니다). id name name ( ( id 、 ( ( 는 ( ( 는 는 는 는 는 는 는 는 ( id id id id id id는 ) 。, '어느 경우든'에 전화를 .applicationContext.getBean("bean-identifier");

<bean> 태그에 대응하는 자바 @Bean을 사용하면 id 속성을 찾을 수 없습니다.ID 값은 이름 속성을 통해서만 @Bean에 지정할 수 있습니다.

하겠습니다.
을 spring1

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
  <bean id="foo" class="com.intertech.Foo"></bean>
  <bean id="bar" class="com.intertech.Bar"></bean>
</beans>

Foo는 Foo 오브젝트를 반환합니다.Foo f = (Foo) context.getBean("foo");를 바꿉니다체하하id="foo"name="foo" spring1.xml을 사용하다

다음과 같이 xml 구성을 정의합니다.

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
  <bean id="fooIdentifier" class="com.intertech.Foo"></bean>
  <bean name="fooIdentifier" class="com.intertech.Foo"></bean>
</beans>

Bean Definition Parsing Exception 。'fooIdentifier'는 'fooIdentifier'입니다.이것은 에 설정, , 설정, 설정 등이 있는 와 같은 입니다.
<bean name="fooIdentifier" class="com.> </bean> </bean>
<bean name="fooIdentifier" class="com.> </bean> </bean>


식별자로도 얻을 수 . config as config as as as as as config as as

<?xml version="1.0" encoding="UTF-8"?><br>
<beans ...>
  <bean id="fooById" name="fooByName" class="com.intertech.Foo"></bean>
  <bean id="bar" class="com.intertech.Bar"></bean>
</beans>

다음 코드가 true로 출력됩니다.

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(...);
Foo fooById = (Foo) context.getBean("fooById")// returns Foo object;
Foo fooByName = (Foo) context.getBean("fooByName")// returns Foo object;
System.out.println(fooById == fooByName) //true

Application Context xml에서 ID와 이름을 정의할 때 차이가 있습니까?아니오 3.1(spring)의 경우 ID는 xsd:string 유형으로도 정의됩니다.즉, 이름을 정의할 때 사용할 수 있는 모든 문자는 Id에도 사용할 수 있습니다.이것은 Spring 3.1 이전에는 불가능했습니다.

ID와 동일한 이름을 사용하는 이유 응용 프로그램의 각 구성 요소가 해당 구성 요소 자체 고유의 빈 이름을 사용하여 공통 종속성을 참조할 수 있도록 하는 등 일부 상황에서 유용합니다.

For example, the configuration metadata for subsystem A may refer to a DataSource via the name subsystemA-dataSource. The configuration metadata for subsystem B may refer to a DataSource via the name subsystemB-dataSource. When composing the main application that uses both these subsystems the main application refers to the DataSource via the name myApp-dataSource. To have all three names refer to the same object you add to the MyApp configuration metadata the following 

<bean id="myApp-dataSource" name="subsystemA-dataSource,subsystemB-dataSource" ..../>

Alternatively, You can have separate xml configuration files for each sub-system and then you can make use of
alias to define your own names.

<alias name="subsystemA-dataSource" alias="subsystemB-dataSource"/>
<alias name="subsystemA-dataSource" alias="myApp-dataSource" />

둘다요.id그리고.name는 Spring IOC 컨테이너/Application Contecxt 내의 빈 식별자입니다.idAttribute를 사용하면 ID를 하나만 지정할 수 있지만name이치노

스프링 닥터는 여기서 확인하실 수 있습니다.

언급URL : https://stackoverflow.com/questions/874505/difference-between-using-bean-id-and-name-in-spring-configuration-file

반응형