我对Dagger2提出了一个小问题。 但首先让我展示示例代码:
@Singleton
@Component(module={ApplicationModule.class})
public Interface ApplicationComponent {
}
@Module
public class ApplicationModule {
@Provides
public Context provideContext() {
return context;
}
}
我知道组件中的对象现在是" Singletons" .. 我的问题......这对模块有什么影响吗?模块也是Singleton吗?
答案 0 :(得分:1)
不,除非您为enum NumberOrNothing {
Number(i32),
Nothing,
}
use self::NumberOrNothing::{Number,Nothing};
impl NumberOrNothing {
fn print(self) {
match self {
Nothing => println!("The number is: <nothing>"),
Number(n) => println!("The number is: {}", n),
};
}
}
fn vec_min(v: &Vec<i32>) -> NumberOrNothing {
fn min_i32(a: i32, b: i32) -> i32 {
if a < b {a} else {b}
}
let mut min = Nothing;
for e in v.iter() {
//Alternatively implicitly and with *e replaced by e:
//for e in v {
min = Number(match min {
Nothing => *e,
Number(n) => min_i32(n, *e),
});
}
min
}
pub fn main() {
let vec = vec![18,5,7,2,9,27];
let foo = Nothing;
let min = vec_min(&vec);
let min = vec_min(&vec);
min.print();
}
带注释的提供者方法指定范围,否则模块不会是单例。
@Provides