/*** Created by liuml on 2022/4/14 10:47*/ public class HttpObject {String TAG = "HttpObject";public void post() {Log.d(TAG, "HttpObject post: post请求数据 ");} } /*** Created by liuml on 2022/4/14 10:47*/ public class DataObject { }
@Module public class HttpModule {@Providespublic HttpObject providerHttpObject(){return new HttpObject();} }
2. Component 创建
/*** Created by liuml on 2022/4/14 10:50* 组件 用来放module的 需要那些module*/ @Component(modules = {HttpModule.class, DataModule.class}) public interface MyComponent {//这里的参数不能用多态void inject(MainActivity activity);}
3. 在activity里面注入并调用
public class MainActivity extends AppCompatActivity {String TAG = "MainActivity";//注入后直接用@InjectHttpObject httpObject;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);DaggerMyComponent.create().inject(this);httpObject.post();Log.d(TAG, "onCreate: httpObject "+httpObject.hashCode());} }
/*** Created by liuml on 2022/4/14 10:48** 这个类用来提供对象*/ @Module public class HttpModule {@Singleton@Providespublic HttpObject providerHttpObject(){return new HttpObject();} }
Component组件
/*** Created by liuml on 2022/4/14 10:50* 组件 用来放module的 需要那些module*/ @Singleton @Component(modules = {HttpModule.class, DataModule.class}) public interface MyComponent {//这里的参数不能用多态void inject(MainActivity activity);}
// 你想保持两个组件之间的独立you want to keep two components independent.// 你想强调一个组件是依赖于另一个组件you want to explicitly show what dependencies from one component is used by the other.
Subcomponents - Use this when:
// 你想让一个组件保持高内聚(不想直接暴露子组件的实例化过程) you want to keep two component cohesive. // 你并不在意明确展示出两个组件之间的依赖关系(哪个组件依赖哪个组件) you may not care to explicitly show what dependencies from one component is used by the other.
This document outlines the recommended naming conventions for HTML attributes in Fast Components, focusing on readability and consistency with existing standards. ...
[详细]