반응형
스프링 빈즈 - 컨스트럭터 arg로 널 배선하는 방법?
나는 다음과 같은 콩을 정의합니다.
<bean id="myBean" class="com.me.myapp.Widget">
<constructor-arg name="fizz" value="null"/>
<constructor-arg name="buzz" ref="someOtherBean" />
</bean>
앱을 실행하면 Spring에서 bean config 예외가 발생합니다.
[java] Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myBean' defined in class path resource [spring-config.xml]:
Unsatisfied dependency expressed through constructor argument with index 0
of type [com.me.myapp.Widget]: Could not convert constructor argument value of
type [java.lang.String] to required type [com.me.myapp.Widget]: Failed to
convert value of type 'java.lang.String' to required type
'com.me.myapp.Widget'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [java.lang.String] to required
type [com.me.myapp.Widget]: no matching editors or conversion strategy found
저는 단지 당신이 원하는 것을Widget다음과 같은 Java를 작성할 수 있습니다.
Widget w = new Widget(null, someOtherBean);
이거 어떻게 해요?미리 감사드립니다!
스프링스를 사용하시면 됩니다.<null>태그:
<bean id="myBean" class="com.me.myapp.Widget">
<constructor-arg name="fizz">
<null />
</constructor-arg>
<constructor-arg name="buzz" ref="someOtherBean" />
</bean>
스프링이 사용자가 언급한 순서대로의 값을 생성자에 주입하지 못하는 경우가 있습니다.명시적인 주문으로 시도해 보는 것이 더 좋습니다.
<constructor-arg index="0" name="fizz"><null/></constructor-arg>
<constructor-arg index="1" name="buzz"><ref bean="someOtherBean"/></constructor-arg>
사실은 저를 위해 일하진 않았지만,
그래서 제가 한 일은, 대상을 봄에 통과시키는 것이 아니라, "대상이 null인지" 묻고, 새로운 대상을 만들어 그것에 할당하는 것입니다.
(봄과 유사하게) 한 번 행해지기도 하는데, 차이점은, 그것이 최선의 연습이 아니라는 것과, 객체의 생성점이 수업의 흐름 속에 있고, 초기 단계에 있지 않다는 것입니다.
언급URL : https://stackoverflow.com/questions/12941955/spring-beans-how-to-wire-null-as-a-constructor-arg
반응형
'programing' 카테고리의 다른 글
| 공간 조인이 있는 mysql 뷰가 작동하지 않습니다. (0) | 2023.09.12 |
|---|---|
| 모든 부모가 있는 트리에 있는 노드의 mysql에서 부모/자녀 관계의 전체 트리 가져오기 (0) | 2023.09.12 |
| 가장 적은 양의 명령에서 가장 빠른 정수 제곱근 (0) | 2023.09.12 |
| Mapstruct - Generated Mapper 클래스에서 스프링 종속성을 주입하는 방법 (0) | 2023.09.12 |
| ajax 시간 초과 콜백 함수 (0) | 2023.09.12 |