在ActFramework 依赖注入 II - 注入对象类型中我们提到了定义绑定的一种方式:
1. 使用Module
// Define bindings public class MyModule extends org.osgl.inject.Module { protected void configure() { bind(MyService.class).to(OneService.class); bind(MyService.class).named("two").to(TwoService.class); } }
这篇文章继续介绍ActFramework的其他绑定方式
2. 自定义工厂
工厂和上面的Module是相当的,把上面的Module用工厂的方式来写会是这样:
public class MyFactory { @org.osgl.inject.annotation.Provides public MyService getOneService(OneService oneService) { return oneService; } @org.osgl.inject.annotation.Provides @Named("two") public MyService getTwoService(TwoService twoService) { return twoService; } }
3. 自动绑定
自动绑定不需要定义Module和工厂,但是需要在Interface(被绑定类)上使用@act.inject.AutoBind
注解:
// The interface @act.inject.AutoBind public interface MyService { void service(); }
定义缺省实现
// The implemention one public class OneService implements MyService { public void service() {Act.LOGGER.info("ONE is servicing");} }
使用@javax.inject.Named
注解定义Qualified的实现
// The implemention two @javax.inject.Named("two") public class TwoService implements MyService { public void service() {Act.LOGGER.info("TWO is servicing");} }
使用依赖注入
// Inject the service public class Serviced { // this one will get bind to the default implementation: OneService @javax.inject.Inject private MyService one; // this one will get bind to TwoService @javax.inject.Inject @javax.inject.Named("two") private MyService two; }
链接
- ActFramework依赖注入 I - 简介
- ActFramework依赖注入 II - 注入对象类型
- ActFramework官网
- [ActFramework@开源中国](https://www.oschina.net/p/actframework)
- [ActFramework@码云](https://git.oschina.net/actframework/actframework)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。