Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/0xcaffebabe/di
一个简单依赖注入项目
https://github.com/0xcaffebabe/di
Last synced: about 1 month ago
JSON representation
一个简单依赖注入项目
- Host: GitHub
- URL: https://github.com/0xcaffebabe/di
- Owner: 0xcaffebabe
- Created: 2019-01-26T11:37:12.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-01-06T11:03:49.000Z (almost 3 years ago)
- Last Synced: 2024-10-16T00:44:03.265Z (3 months ago)
- Language: Java
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# di
#
2019-1-26根据个人对依赖注入原理理解的一个简单实现
#
测试用例:DAO:
```java
@Component
public class DAO {
{
System.out.println("DAO被新建");
}
}
```
Service:
```java
public interface Service{
}
```
ServiceImpl:
```java
@Component
public class ServiceImpl implements Service {private DAO dao;
{
System.out.println("service 被创建");
}public ServiceImpl(DAO dao){
this.dao = dao;
}@Override
public void get() {
System.out.println("get");
}
}
```
Controller:
```java
@Component
public class Controller {
{
System.out.println("controller 被新建");
}
private Service service;public Controller(Service service) {
this.service = service;
}
}
```
Main:
```java
public class Main {public static void main(String[] args) {
Context.scanAllClasses();
System.out.println(Context.get(Controller.class));
System.out.println(Context.get(Service.class));
}
}
```
最终输出:
```text
DAO被新建
service 被创建
controller 被新建
wang.ismy.di.Controller@668bc3d5
wang.ismy.di.ServiceImpl@3cda1055
```
#
AOP支持:
```java
public class Main {public static void main(String[] args) {
Context.scanAllClasses();
Context.aop(aspect -> {
System.out.println(aspect.getMethod()+"被运行");
return aspect.process();
});
Service service = Context.get(Service.class);
service.get();}
}
```
输出:
```text
DAO被新建
service 被创建
controller 被新建
public void wang.ismy.di.ServiceImpl.get()被运行
get
```