How to define an inner class bean

Development environment

Introduction

The back of the leaflet.

Try to define the inner class as a bean normally

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

How did you come to post this article?

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

How to define an inner class bean
How to create an application
How to use java class
How to handle an instance
How to disassemble Java class files
How to use the wrapper class
How to insert an external library
How to decompile java class files
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Java] How to use Math class
How to crop an image with libGDX
[Java] How to use the File class
How to blur an image (super easy)
[Java] How to use the HashMap class
How to publish an application on Heroku
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Java] How to use the Calendar class
How to write an RSpec controller test
How to get keycloak credentials in interceptor class
[Java] How to use FileReader class and BufferedReader class
How to write an if statement to improve readability-java
How to check if an instance variable is defined in a Ruby class
How to use an array for HashMap keys
How to create a class that inherits class information
How to get Class from Element in Java
How to deploy
How to conditionally add html.erb class in Rails
Rails6.0 ~ How to create an eco-friendly development environment
How to solve an Expression Problem in Java
[Rails] How to build an environment with Docker
How to define multiple orm.xml in Spring4, JPA2.1
How to build an executable jar in Maven
(Article about to be written) Inner class summary
How to make an crazy Android music player
How to create an oleore certificate (SSL certificate, self-signed certificate)
[Java] How to use Calendar class and Date class
How to make an image partially transparent in Processing
How to install Ruby on an EC2 instance on AWS
How to push an app developed with Rails to Github
How to make an oleore generator using swagger codegen
How to make an almost static page with rails
How to write to apply gem Pagy (pagination) to an array
How to create and execute method, Proc, Method class objects
How to standardize operations on sets -First class collection-
How to write an external reference key in FactoryBot
How to use and apply Java's JFrame / Canvas class
[Rails] How to display an image in the view
How to develop OpenSPIFe
How to call AmazonSQSAsync
How to use Map
Class to take count
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
Java inner class review
Inner class usage example