The back of the leaflet.
Suppose you have defined the following class. This is a test of a method that adds two arguments ʻint` and returns.
InnerTest.java
@RunWith(SpringRunner.class)
public class InnerTest {
@Inject
Inner inner;
@Test
public void test() {
int a = 1;
int b = 2;
//Assertion
assertThat(inner.add(a, b), equalTo(a + b));
}
/**
*Inner class for testing
*
*/
public class Inner {
//Just add the values of the arguments and return
public int add(int a, int b) {
return a + b;
}
}
}
So, I think that the bean definition is as follows.
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>
When you run ...
Error log
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
why! Bean defined!
When I googled, there was a person asking the same question.
How to create a Spring Bean of a Inner class?
--Make the inner class static.
Or
--Connect with $ instead of.
If you do either of
I don't know if there is demand, but I needed it for the system I'm developing in business, so as a note: stuck_out_tongue :.
For example, in a test, if you define the test Controller
as a static inner class, the package will be only the test class and it will be easier to see: v :.
Recommended Posts