Automatic dependency injection that works on Desktop, Web and Android.
Spring support library: here
Maven:
<groupId>pl.tahona</groupId>
<artifactId>di</artifactId>
<version>1.0.0</version>
Gradle:
compile 'pl.tahona:di:1.0.0'
//Create Injector:
final Injector injector = new Injector();
//Register beans
injector.register(AnnotationScopeLocalBean.class);
injector.register(UserService.class);
injector.register(ClientService.class);
injector.register(ConnectionService.class);
final BeanContainer beanContainer = new BeanContainer(injector);
beanContainer.initialize();class UserService {
@Wire private ConnectionService connectionService;
//...
}@Service
class UserService {
private ConnectionService connectionService;
public UserService (ConnectionService connectionService) {
this.connectionService = connectionService;
}
}Method executed after bean injection.
class UserService {
@Wire private ConnectionService connectionService;
@Init
private init () {
connectionService.prepareForConnection();
}
}@Component
class UserService {
@Wire private ConnectionService connectionService;
@Init
private init () {
connectionService.prepareForConnection();
}
}
...
Map<String,Class> classes = new BeanScanner("com.test").scan();
Injector in = new Injector()
in.registerAll(classes);
BeanContainer b = new BeanContainer(in);
b.initialize()Spring annotations (@Component, @Service, @Repository) are in DI-Spring