How do I create an instance of a static nested class as a Spring bean in an XML configuration file? For example:
package com.x.y;
public class A {
    public static class B {
    ...
    }
}
So that I have a Spring-managed bean of class B?
As you know Java inner classes are defined within the scope of other classes, similarly, inner beans are beans that are defined within the scope of another bean. Thus, a <bean/> element inside the <property/> or <constructor-arg/> elements is called inner bean and it is shown below.
Yes, A spring bean may have static methods too.
Terminology: Nested classes are divided into two categories: non-static and static. Non-static nested classes are called inner classes. Nested classes that are declared static are called static nested classes. A nested class is a member of its enclosing class.
By marking this method as static , it can be invoked without causing instantiation of its declaring @Configuration class, thus avoiding the above-mentioned lifecycle conflicts. Note however that static @Bean methods will not be enhanced for scoping and AOP semantics as mentioned above.
Using A$B syntax, which is how the classloader sees inner classes.  So assuming package com.x.y, then:
<bean id="myBean" class="com.x.y.A$B"/>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With