際際滷

際際滷Share a Scribd company logo
Dagger 2
Dependency Injection - 仂 舒弍仍仂仆
舒亰舒弍仂从亳 仗仂亞舒仄仄仆仂亞仂 仂弍亠从仆仂亞仂
仗仂亞舒仄仄亳仂于舒仆亳, 从仂仂亶 亠舒仍亳亰亠
亳仆于亠亳 仗舒于仍亠仆亳 (Inversion of Control)
亟仍 仗亠亟仂舒于仍亠仆亳 亰舒于亳亳仄仂亠亶
从仂仄仗仂仆亠仆舒仄
DI 亠亶仄于仂从亳
 Google Guice
 Spring DI
 Java 6 EE CDI
 Dagger
仂弍仍亠仄 DI 亠亶仄于仂从仂于
 仂仍仆仂 亳仍亳 舒亳仆仂 亳仗仂仍亰ム Re鍖ection
API
 仂仆亳亞亳仂于舒仆亳亠 亰舒于亳亳仄仂亠亶 于 XML
 舒仍亳亟舒亳 亞舒舒 亰舒于亳亳仄仂亠亶 于仂 于亠仄
于仗仂仍仆亠仆亳 从仂亟舒
 亠仆亠舒亳 从仂亟舒, 亟仆仂亞仂 亟仍 亠仆亳 亠仍仂于亠从仂仄
 仂亟, 从仂仂亶 亟仆仂 仂仍舒亢亳于舒
仂亠仄 Dagger 2
 亠于亶 DI 亠亶仄于仂从, 从仂仂亶 仗仂仍仆仂
亳仗仂仍亰亠 仂仍从仂 亞亠仆亠亳仂于舒仆仆亶 从仂亟
 JSR-330
 亠亞从亳亶 亳 亳舒亠仄亶 从仂亟, 从仂仂亶 仆亠 仂仍亳舒亠 仂
仆舒仗亳舒仆仆仂亞仂 亠仍仂于亠从仂仄
 仂仍仆舒 于舒仍亳亟舒亳 于仂 于亠仄 从仂仄仗亳仍亳亳
 仂亟, 从仂仂亶 仍亠亞从仂 仂仍舒亢亳于舒
 仂从舒 仗仂亳亰于仂亟亳亠仍仆仂
仆仂于仆亠 从仂仄仗仂仆亠仆
 Inject
Target 亟仍 仗亠亠亟舒亳 亰舒于亳亳仄仂亠亶
 Module
亠亟仂舒于仍磳 亰舒于亳亳仄仂亳
 Component
弌于磶于舒亠 Targets 亳 Modules, 舒 舒从亢亠 舒仆舒于仍亳于舒ム
仂仆仂亠仆亳 仄亠亢亟 Component 亳 亳 仂亠于仍ム 亳 仆舒仂亶从
亳仄亠
OkHttpClient	client	=	new	OkHttpClient();	
	//	仆亳亳舒仍亳亰亳亠仄	从	亟仍	OkHttp	
	Cache	cache	=	new	Cache(getApplication().getCacheDir(),	10	*	1024	*	1024);	
	client.setCache(cache);	
	//	丱舒仆亳仍亳亠	亟仍	仂从亠仆舒	亠亳亳	仗仂仍亰仂于舒亠仍	
	SharedPreferences	sharedPrefeences	=	
	PreferenceManager.getDefaultSharedPreferences(this);	
	//	弌仂亰亟舒亠仄	从亰亠仄仗仍	Gson	
	Gson	gson	=	new	GsonBuilder().create();	
	GsonConverterFactory	converterFactory	=	GsonConverterFactory.create(Gson);	
	//	弌仂亰亟舒亠仄	从亰亠仄仗仍	Retrofit	
	Retrofit	retrofit	=	new	Retrofit.Builder()	
																													.baseUrl(https://onboarding2016.com")	
																													.addConverterFactory(converterFactory)	
																													.client(client)		//	亰舒亟舒亠仄	从仍亳亠仆	
																													.build();
@Module	
public	class	AppModule	{	
				Application	mApplication;	
				public	AppModule(Application	application)	{	
								mApplication	=	application;	
				}	
				@Provides	@Singleton	
				Application	providesApplication()	{	
								return	mApplication;	
				}	
}
@Module	
public	class	NetModule	{	
			String	mBaseUrl;	
			public	NetModule(String	baseUrl)	{	mBaseUrl	=	baseUrl;	}	
			@Provides	@Singleton	
			SharedPreferences	providesSharedPreferences(Application	application)	{	
							return	PreferenceManager.getDefaultSharedPreferences(application);	
			}	
			@Provides	@Singleton	
			Cache	provideOkHttpCache(Application	application)	{		
							return	new	Cache(application.getCacheDir(),	10	*	1024	*	1024);	
			}	
			@Provides	@Singleton	
			Gson	provideGson()	{			
							return	new	GsonBuilder().create();	
			}	
			@Provides	@Singleton	
			OkHttpClient	provideOkHttpClient(Cache	cache)	{	
							OkHttpClient	client	=	new	OkHttpClient();	
							client.setCache(cache);	
							return	client;	
			}	
			@Provides	@Singleton	
			Retrofit	provideRetrofit(Gson	gson,	OkHttpClient	okHttpClient)	{	
									return	new	Retrofit.Builder()	
																.addConverterFactory(GsonConverterFactory.create(gson))	
																.baseUrl(mBaseUrl)	
																.client(okHttpClient)	
																.build();	
				}	
}
public	class	Dagger2SampleActivity	extends	Activity	{	
			@Inject	MyTwitterApiClient	mTwitterApiClient;	
			@Inject	SharedPreferences	sharedPreferences;	
		public	void	onCreate(Bundle	savedInstance)	{	
							//	仆亳亳舒仍亳亰亳亠仄	仗仂仍,	仗仂仄亠亠仆仆亠	@Inject	
							InjectorClass.inject(this);	
			}		
}
@Singleton	
@Component(modules	=	{AppModule.class,	NetModule.class})	
public	interface	NetComponent	{	
			void	inject(Dagger2SampleActivity	activity);	
			//	void	inject(MyFragment	fragment);	
			//	void	inject(MyService	service);	
}
public	class	Dagger2SampleApp	extends	Application	{	
				private	NetComponent	mNetComponent;	
				@Override	
				public	void	onCreate()	{	
								super.onCreate();	
								//	弌亞亠仆亠亳仂于舒仆仆亶	从仂仄仗仂仆亠仆	Dagger%COMPONENT_NAME%	
								mNetComponent	=	DaggerNetComponent.builder()	
																.appModule(new	AppModule(this))	
																.netModule(new	NetModule(https://onboarding2016.com"))	
																.build();	
				}	
				public	NetComponent	getNetComponent()	{	
							return	mNetComponent;	
				}	
}
@Provides	@Singleton	
OkHttpClient	provideOkHttpClient(Cache	cache)	{	
				OkHttpClient	client	=	new	OkHttpClient();	
				client.setCache(cache);	
				return	client;	
}	
@Provides	@Singleton	
OkHttpClient	provideOkHttpClient()	{	
				OkHttpClient	client	=	new	OkHttpClient();	
				return	client;	
}
@Provides	@Named(cached")	@Singleton	
OkHttpClient	provideOkHttpClient(Cache	cache)	{	
				OkHttpClient	client	=	new	OkHttpClient();	
				client.setCache(cache);	
				return	client;	
}	
@Provides	@Named("non_cached")	@Singleton	
OkHttpClient	provideOkHttpClient()	{	
				OkHttpClient	client	=	new	OkHttpClient();	
				return	client;	
}
@Inject	@Named("cached")	OkHttpClient	clientWithCache;	
@Inject	@Named("non_cached")	OkHttpClient	clientWithoutCache;
@Qualifier	
@Retention(CLASS)	
public	@interface	DefaultPreferences	{	
}
Dagger 2
舒于亳亳仄仂亳
从仂仄仗仂仆亠仆仂于
亞舒仆亳亠仆亳 亰舒于亳亳仄
Component-仂于
 2 亰舒于亳亳仄 Component 仆亠 仄仂亞 亳仗仂仍亰仂于舒
仂亟亳仆 亳 仂 亢亠 Scope
 弍磶舒仆仆仂 仗仂 仂亰亟舒仆亳 亳 亟舒仍亠仆亳 仍仂从,
从仂仂亠 仂仂于亠于ム 仂亢亳亟舒亠仄仂仄 仗仂于亠亟亠仆亳,
仗仂仍仆仂 仍亠亢亳 仆舒 于舒
 亳 仂仗亳舒仆亳亠 亰舒于亳亳仄仂亠亶 仄亠亢亟 Component,
仂亟亳亠仍从亳亶 Component 亟仂仍亢亠仆 磦仆仂 仂仗亳舒
仂弍亠从, 从仂仂亠 仄仂亞 弍 仗亠亠亟舒仆 于
亰舒于亳亳仄亠 仂 仆亠亞仂 Component
@UserScope	//	仗仂仍亰亠仄	舒仆亠亠	仂弍磦仍亠仆亶	Scope	
@Component(dependencies	=	NetComponent.class,	
											modules	=	GitHubModule.class)	
public	interface	GitHubComponent	{	
				void	inject(MainActivity	activity);	
}
@Module	
public	class	GitHubModule	{	
				public	interface	GitHubApiInterface	{	
						@GET("/org/{orgName}/repos")	
						Call<ArrayList<Repository>>	getRepository(	
																						@Path("orgName")	String	orgName);	
				}	
				@Provides	
				@UserScope	//	仂仍亢亠仆	仂仂于亠于仂于舒	Scope	Component-舒	
				public	GitHubApiInterface	providesGitHubInterface(Retrofit	retrofit)	{	
								return	retrofit.create(GitHubApiInterface.class);	
				}	
}
@Singleton	
@Component(modules	=	{AppModule.class,	NetModule.class})	
public	interface	NetComponent	{	
				Retrofit	retrofit();	
				OkHttpClient	okHttpClient();	
				SharedPreferences	sharedPreferences();	
}
NetComponent	mNetComponent	=	DaggerNetComponent.builder()	
																.appModule(new	AppModule(this))	
																.netModule(new	NetModule("https://onboarding2016.com"))	
																.build();	
GitHubComponent	gitHubComponent	=	DaggerGitHubComponent.builder()	
																.netComponent(mNetComponent)	
																.gitHubModule(new	GitHubModule())	
																.build();
@Module	
public	class	MyActivityModule	{	
				private	final	MyActivity	activity;	
				public	MyActivityModule(MyActivity	activity)	{this.activity	=	activity;}	
				@Provides	@MyActivityScope	@Named("sample_list")	
				public	ArrayAdapter	providesMyListAdapter()	{	
								return	new	ArrayAdapter<String>(	...	);	
				}	
}	
@MyActivityScope	
@Subcomponent(modules={	MyActivityModule.class	})	
public	interface	MyActivitySubComponent	{	
				@Named("sample_list")	ArrayAdapter	myListAdapter();	
}	
@Singleton	
@Component(modules={	...	})	
public	interface	MyApplicationComponent	{	
				MyActivitySubComponent	newMyActivitySubcomponent(	
																																		MyActivityModule	activityModule);	
}
public	class	MyActivity	extends	Activity	{	
		@Inject	ArrayAdapter	arrayAdapter;	
		public	void	onCreate(Bundle	savedInstance)	{	
								((Dagger2SampleApp)	getApplication()).getApplicationComponent())	
												.newMyActivitySubcomponent(new	MyActivityModule(this))	
												.inject(this);	
				}		
}
弌仍从亳
 Documentation
 Codepath Guide
 GitHub page

More Related Content

Dagger 2