ProvidesMethods
@Provides方法 @Provides Methods
当你需要创建一个对象时,可以使用@Provides方法。这个方法在modele里定义,并且必须有一个带@Provides注解。这个方法的返回类型将是绑定的类型。每当注入需要的这个类型的实例时,将会调用这个方法。
When you need code to create an object, use an @Provides method. The method must be defined within a module, and it must have an @Provides annotation. The method’s return type is the bound type. Whenever the injector needs an instance of that type, it will invoke the method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class BillingModule extends AbstractModule { @Override protected void configure() { ... } @Provides TransactionLog provideTransactionLog() { DatabaseTransactionLog transactionLog = new DatabaseTransactionLog(); transactionLog.setJdbcUrl("jdbc:mysql://localhost/pizza"); transactionLog.setThreadPoolSize(30); return transactionLog; } } |
如果@Provides方法里有一个类似@PayPal或者@Named(“Checkout”)的绑定注解,Guice将会绑定这个被标注的类型。依赖关系可以作为参数传递给方法。注入器会在调用这个方法前一次执行这些绑定。
If the @Provides method has a binding annotation like @PayPal or @Named(“Checkout”), Guice binds the annotated type. Dependencies can be passed in as parameters to the method. The injector will exercise the bindings for each of these before invoking the method.
1 2 3 4 5 6 7 |
@Provides @PayPal CreditCardProcessor providePayPalCreditCardProcessor( @Named("PayPal API key") String apiKey) { PayPalCreditCardProcessor processor = new PayPalCreditCardProcessor(); processor.setApiKey(apiKey); return processor; } |
抛异常 Throwing Exceptions
Guice不允许Providers抛异常。从@Provides方法中抛出的异常将会被包含在ProvisionException异常中。允许从@Provides方法中抛出任意类型的异常不是最好的做法——执行期,或者检查期。如果你有些情况下需要抛出异常,你可以用ThrowingProviders extension @CheckedProvides方法
Guice does not allow exceptions to be thrown from Providers. Exceptions thrown by @Provides methods will be wrapped in a ProvisionException. It is bad practice to allow any kind of exception to be thrown — runtime or checked — from an @Provides method. If you need to throw an exception for some reason, you may want to use the ThrowingProviders extension @CheckedProvides methods.
实例
@Provides Methods(@Provides注解方法):用@Provides来注解方法产生需要的对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import com.google.inject.Inject; import com.google.inject.name.Named; public class Configure { String jdbcUrl; int loginTimeout; @Inject public Configure(@Named("JDBC URL") String jdbcUrl, int loginTimeout) { this.jdbcUrl = jdbcUrl; this.loginTimeout = loginTimeout; } @Override public String toString() { return "jdbcUrl==>" + this.jdbcUrl + "\tloginTimeout==>" + this.loginTimeout + " seconds"; } } import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.name.Names; public class ConfigureModule extends AbstractModule { @Override protected void configure() { bind(String.class) .annotatedWith(Names.named("JDBC URL")) .toInstance("jdbc:mysql://localhost/pizza"); } @Provides int getLoginTimeout() { System.out.println("run @Provides"); return 20; } } import com.google.inject.Guice; import com.google.inject.Injector; public class Test { public static void main(String[] args) { Injector injector = Guice.createInjector(new ConfigureModule()); Configure configure = injector.getInstance(Configure.class); System.out.println(configure); } } |
执行结果:
run @Provides
jdbcUrl==>jdbc:mysql://localhost/pizza loginTimeout==>20 seconds
可以看到,在module里没有绑定int类型的值,却在依赖时注入了。
下一节:Provider Bindings
说明:
鉴于网上guice中文资料较少,出于个人爱好,对该项目下的用户API文档进行翻译。如有翻译不恰当之处,还望指正。
google Guice 项目地址:https://github.com/google/guice
Guice 英文API地址:https://github.com/google/guice/wiki/LinkedBindings
发表评论
难的是和自己保持联系~