Spring Interface Bean Definition
Library gives an ability to make beans without stereotype annotations (@Service @Component etc.) but through interface inheritance (Like spring data repositories do)
Getting started
Maven:
<dependency>
<groupId>org.decembrist.spring</groupId>
<artifactId>spring-interface-bean</artifactId>
<version>1.1.0</version>
</dependency>
Gradle:
implementation "org.decembrist.spring:spring-interface-bean:1.1.0"
Tested spring-boot version: 2.4.5
Example:
//1. Extend class with IBean interface to make it singleton bean
class SomeClass implements IBean {}
class AnotherClass {
//2. SomeClass above will be injected (Singleton scope)
@Autowired private SomeClass someClass;
}
//3. SubInterface works the same way
interface InterfaceBeanSubInterface extends IBean {
}
class SomeClass2 implements InterfaceBeanSubInterface {}
class AnotherClass2 {
//4. SomeClass2 above will be injected (Singleton scope)
@Autowired private SomeClass2 someClass;
}
To use without springboot autoconfiguration:
//import postprocessor
@Import(InterfaceBeanPostProcessor.class)
Manual interface bean functionality:
If you don't want to use another dependency just copy this bean to your codebase InterfaceBeanPostProcessor
//And replace IBean.class whatever interface you want to be bean definer
scanner.addIncludeFilter(new AssignableTypeFilter(IBean.class));
Exceptions:
- If your bean class has one of stereotype annotations - everything works as usual, interface bean postprocessor ignores that classes
- Only singleton scope supported. Now you can't change interface beans scope, you should use stereotype annotations in this case
Properties:
#disable interface bean definition
spring.interface-bean=false