Since AndroidAnnotations 2.4
@EBean
:
@EBean
进行注解:
@EBean public class MyClass { }
An@EBean
annotated class must have only one constructor. This constructor must either have no parameters, or, as ofAndroidAnnotations 2.7
, it may have one parameter of typeContext
.一个用@EBean
注解的类必须只有一个构造函数。这个构造函数必须是无参的,或者,在AndroidAnnotations 2.7
以后,可以带一个类型为Context
的参数。
@Bean
:
@Bean
注解就可以使用这个优化类。
@EBean public class MyOtherClass { @Bean MyClass myClass; }
@EActivity public class MyActivity extends Activity { @Bean MyOtherClass myOtherClass; }
You always get a new instance when you use@Bean
on a field, unless that bean is a Singleton (see Scopes below).当你使用@Bean
在一个字段上时, 你一直获得一个新的实例。除非这个bean是一个单例(参看下面的作用域)。
myOtherClass
injected :
myOtherClass
注入进来:
@EActivity public class MyChildActivity extends MyActivity { }
Since AndroidAnnotations 2.5
@Bean
annotation.
@Bean
注入的参数值。
@EActivity public class MyActivity extends Activity { /* A MyImplementation instance will be injected. * MyImplementation must be annotated with @EBean and implement MyInterface. */ @Bean(MyImplementation.class) MyInterface myInterface; }
Does that provide decoupling advantages ? Well, the class still knows about its dependencies implementation, but at least the code using those dependencies has to rely on the API, which is an interesting goal.那样是不是提供了松耦合的优势呢?其实,这个类还是知道它自己的依赖实现,但是至少代码使用这些依赖必须依靠API,这其实是一个很有意思的目的。
@EBean
class:
@EBean
类中使用大部分的AA注解:
@EBean public class MyClass { @SystemService NotificationManager notificationManager; @UiThread void updateUI() { } }
@EBean
class:
@EBean
类中
使用和视图相关
( @View
, @Click
...)
的注解:
@EBean public class MyClass { @ViewById TextView myTextView; @Click(R.id.myButton) void handleButtonClick() { } }
Notice that this will only work if the root Android component that depends onMyClass
is an activity with a layout that contains the needed views. Otherwise,myTextView
will be null, andhandleButtonClick()
will never be called.请注意,只有当依赖于MyClass
的Android根组件是一个activity,并且它的布局文件包含了所需的视图,这些注解才会生效。否则,myTextView
会为null,handleButtonClick()
也永远不会被调用。
@EBean
class, using the
@RootContext
annotation. Please notice that it only gets injected if the context has the right type.
@RootContext
注解注入依赖你
@EBean
类的Android根组件。请注意,只有当context的类型正确时,注入才会成功。
@EBean public class MyClass { @RootContext Context context; // Only injected if the root context is an activity @RootContext Activity activity; // Only injected if the root context is a service @RootContext Service service; // Only injected if the root context is an instance of MyActivity @RootContext MyActivity myActivity; }
MyClass
instance referenced by the following activity, the
service
field of
MyClass
(see above) will be
null
.
MyClass
实例,它的字段
service
将为空。
@EActivity public class MyActivity extends Activity { @Bean MyClass myClass; }
@EBean
annotated class is called, it's fields have not been injected yet. If you need to execute code at build time, after dependency injection, you should use the
@AfterInject
annotation on some methods.
@EBean
注解类的构造器被调用后,它的字段还没有注入进来。假如你需要在构建项目的时候执行代码,那么在依赖注入后,你需要添加使用
@AfterInject
注解的方法。
@EBean public class MyClass { @SystemService NotificationManager notificationManager; @Bean MyOtherClass dependency; public MyClass() { // notificationManager and dependency are null } @AfterInject public void doSomethingAfterInjection() { // notificationManager and dependency are set } }
@AfterViews
annotated methods with the same name, the generated code will be buggy. See issue #591 for more details.
@AfterViews
注解方法使用相同的名字,生成的代码会有bug。详情参见
issue #591
。
Since AndroidAnnotations 2.5
@EBean(scope = Scope.Singleton) public class MySingleton { }
Caution警告
同样,我们也在使用
- If your bean has a
Singleton
scope, it will only keep a reference to the application context (usingcontext.getApplicationContext()
. That's because it lives longer than any activity / service, so it shouldn't keep a reference to any such Android component, to avoid memory leaks.- 假如你的bean有了
Singleton
作用域,它将保持application context的引用(使用context.getApplicationContext()
。那是因为它是生命周期比任何activity或者service都长,所以它应该在任何Android组件中保留引用,避免引起内存泄漏。- We also disable view injection and view event binding in
@EBean
components withSingleton
scope, for the exact same reason (views have a reference to the activity, which may therefore create a leak).Singleton
作用域的@EBean
组件中禁用了视图注入和视图事件绑定(视图拥有activity的引用,因此有可能产生内存泄漏)。
本文档的简单示例下载