# tins **Repository Path**: hovvsoon/tins ## Basic Information - **Project Name**: tins - **Description**: tins - 轻量的依赖注入容器 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-08-21 - **Last Updated**: 2023-10-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 实例罐头 > 实例罐头 - 轻量的依赖注入容器 ## 功能特性 假设存在循环依赖关系。 ```mermaid graph TD; A-->B; A-->C; C-->A; ``` ### 依赖注入容器 ```java @Named("a") public class A { @Inject public B b; @Inject public C c; // getter and setter // constructor } @Named("b") public class B { @Value("hello world!") public String name; // getter and setter // constructor } @Named("c") public class C implements Runnable { @Inject public A a; // getter and setter // constructor @Override public void run() { Logger.getGlobal().info(String.format("name:%s", a.getB().getName())); } } ``` 按照循环依赖关系构建的代码。 ```java public class ContextTest { public static void main(String[] args) throws Exception { var ioc = Context.run(ContextTest.class); new Thread((Runnable) ioc.get("c")).run(); } } ``` 构建实例罐头依赖注入容器,并从容器中拿到`Runnable`实现接口的`c`,之后进行运行。 输出: ``` 8月 8, 2023 8:08:08 下午 cc.aolob.tins.example.C run 信息: name:hello world! ``` ### 切面编程支持 不变动`c`的`run`方法,尝试改变`run`的行为。 ```java @Named("c") public class C implements Runnable { @Inject public A a; // getter and setter // constructor // TYR TO CHANGE RUN @Override // DONT EDIT ME public void run() { java.util.logging.Logger.getGlobal().info(String.format("name:%s", a.getB().getName())); } } ``` 声明`caspect`对`c`进行切面编程。 ```java @Aspect public class Caspect { @Pointcut("cc.aolob.tins.example.C#run") public void log() { java.util.logging.Logger.getGlobal().info(String.format("ts:%d", System.currentTimeMillis())); } } ``` 输出: ``` 8月 8, 2023 08:08:08 上午 cc.aolob.tins.example.Caspect log 信息: ts:1692671753032 8月 8, 2023 08:08:08 上午 cc.aolob.tins.example.C run 信息: name:hello world! ```