Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could private static class Spring bean annotated @Service be autowired?

Tags:

java

spring

If I have a private static inner class annotated with @Service, will it be autowired? Will Spring's component search find it?

like image 902
Timofey Gorshkov Avatar asked Mar 22 '23 10:03

Timofey Gorshkov


1 Answers

Yes; Spring's component scanner will also find inner classes and treat them as beans.

Remember to use the $-notation when referring to the class from an XML-file; e.g.

<bean id="myBean" class="com.mybeans.OuterClass$InnerClass"/>.

UPDATE: Yes; Spring can instantiate private inner classes.

like image 110
tbsalling Avatar answered Apr 06 '23 03:04

tbsalling