alexey-petrusevich / alexey-petrusevich/IMDB
Create ServiceContainer
- 主要语言
- Java
- 星标
- 0
- 派生
- 0
- PR 合并指标
- 30 天内没有已合并 PR
描述
You should create the ServiceContainer. ServiceContainer - it will be special helper class (singleton) which will provide possibility to create special objects (let's name them "Services") and get access to them which can be used in several classes.
Main plus in this class will be that you could create Services using annotations (```@Service``` - for example).
```
interface SystemService {
void init(Map initParams);
}
```
```
@Service
public class DatabaseService implements SystemService {
...
}
```
At start of your application you should read all project's directories and read all classes that have ```@Service``` annotation. After that you will have Class instances, check - if it implements SystemService interface or not, if not - throw exception (custom, something like - IllegalServiceImplementation). If everything is good - create instance of Service and pass parameter's map.
Main question is - what is this Services using for? Let's look at the example: You have a database and you have a lot of places where you need to use connection to DB, but it will be strange to create this connection every time, better solution will be to create one object (singleton) and store a lot of connections to DB (that you created before) and return it on every request to this singleton.
Dev hints:
About annotations:
( https://www.baeldung.com/java-custom-annotation , https://habr.com/ru/post/139736/ )
To read project dirs, check that is class implements something and create instances of classes you must use Reflection
( https://www.geeksforgeeks.org/reflection-in-java/ , https://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful )
About singletons:
( https://habr.com/ru/post/129494/ )
贡献指南
这个仓库没有索引到贡献指南
评估
这个 Issue 还没有评估数据。