내부 클래스를 빈 정의하는 방법
개발 환경
소개
전단지의 뒷면입니다.
보통 이너 클래스를 빈 정의 해 본다
다음과 같은 클래스를 정의했다고 가정합니다. 인수의 2 개의
int를 더해 돌려주는 메소드의 테스트입니다.InnerTest.java
@RunWith(SpringRunner.class)
public class InnerTest {
@Inject
Inner inner;
@Test
public void test() {
int a = 1;
int b = 2;
// アサーション
assertThat(inner.add(a, b), equalTo(a + b));
}
/**
* テスト用のインナークラス
*
*/
public class Inner {
// 引数の値を足すして返すだけ
public int add(int a, int b) {
return a + b;
}
}
}
그래서, 빈 정의하면 다음과 같이 된다고 생각합니다.
InnerTest-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
">
<bean class="example.InnerTest.Inner" />
</beans>
실행하면 ......
오류 로그
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'example.InnerTest' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 43 common frames omitted
어째서! 콩 정의하자!
라고 생각하고 구그하면 같은 질문을 하고 있는 사람이 있었습니다.
How to create a Spring Bean of a Inner class?
또는
중 하나를하면 해결할 수 있습니다.
이 기사를 게시하기에 이른 경위
수요가 있을지 모르겠지만, 업무로 개발하고 있는 시스템에서 필요하게 되었으므로 메모로서
예를 들어 테스트에서 테스트용
Controller를 static인 이너 클래스로 정의해 두면 패키지가 테스트 클래스만이 되어 보기 쉬워지는 것은 아닐까요 Reference
이 문제에 관하여(내부 클래스를 빈 정의하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/neriudon/items/9d09ae1c5412299dfc47텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)