Andorid之Annotation框架初使用(一)
2024-09-28 01:20:58
1. 设置Activity的布局
@EActivity(R.layout.main)
public class MyActivity extends Activity {}
注: 此时在AndroidManifest.xml 注册Activity需要: <activity android:name="MyActivity_" />
2. 设置及使用Application
设置:在AndroidManifest.xml也需要配置<application android:name="MyApplicaton_" />
@EApplication
public class MyApplication extends Application {}
使用:
@EActivity
public class MyActivity extends Activity {
@App
MyApplication application;
}
或者:
@EBean
public class MyBean {
@App
MyApplication application;
}
3. 普通类
@EBean
public class MyClass {}
注:这个普通只能有一个构造函数,这个构造函数要不是没有参数,或者只有一个Context的参数
使用: @Bean的标签每次都会创建一个实例,所以不能继承一个使用@EBean的类
@EBean
public class MyOtherClass {
@Bean
MyClass myClass;
}
声明接口的同时也实例化:
@EActivity
public class MyActivity extends Activity {
/* A MyImplementation instance will be injected.
* MyImplementation 必须使用标签 @EBean and 实现 MyInterface.
*/
@Bean(MyImplementation.class)
MyInterface myInterface;
}
在普通类中引入它所在的Activity或者Service的Context
@EBean
public class MyClass {
@RootContext
Context context;
// Only injected if the root context is an activity,else is null
@RootContext
Activity activity;
// Only injected if the root context is a service,else is null
@RootContext
Service service;
// Only injected if the root context is an instance of MyActivity,else is null
@RootContext
MyActivity myActivity;
}
使用标签@AfterViewInject, 会在所有依赖注入完成后才执行。
@EBean
public class MyClass {
@SystemService
NotificationManager notificationManager;
@Bean
MyOtherClass dependency;
public MyClass() {
// notificationManager and dependency are null
}
@AfterInject
public void doSomethingAfterInjection() {
// notificationManager and dependency 已经设置完成
}
}
注意,拥有父子关系的@AfterInject的方法名字不能相同
单例的普通类:
@EBean(scope = Scope.Singleton)
public class MySingleton {}
注意: 在单例的普通类如果需要使用context,请使用ApplicationContext。也不要在这里注入view或者是view的事件
最新文章
- CentOS7 + mono +Jexus 环境的搭建
- app——分享wap站,数据处理页面展示
- HackerRank ";New Year Chaos";
- 用JSON-server模拟REST API(一) 安装运行
- SQL Server使用WITH AS递归查找
- wxsqlite3的加密模块单独编译
- css内容生成器
- Python和C#基本算法实现对比
- 关于oracle数据库(7)查询1
- 使用express创建web服务器
- gameUnity 0.15alpha 网络游戏框架
- Win下安装MySQL 5.6
- java.sql.SQLException: Access denied for user &#39;sa&#39;@&#39;localhost&#39; (using password: YES)
- 我的前端故事----来聊聊react-native应用的健康监控
- 关于字数太多直接变成省略号的方法css
- DevExpress v17.2新版亮点—WPF篇(四)
- POJ 3233 Matrix Power Series (矩阵快速幂)
- PHP任意文件上传漏洞CVE-2015-2348浅析
- maven 介绍(二)
- iOS11.2-11.3.1进行越狱及问题
热门文章
- LeetCode765. Couples Holding Hands
- JS中firstChild,lastChild,nodeValue属性
- vue 子父组件之间的通信,及在调用组件的地方
- Hadoop(四)HDFS的高级API操作
- 错误:Could not create the Android package. See the Output (Build) window for more details
- oracle创建计划任务
- 2017年浙江中医药大学大学生程序设计竞赛(重现赛)D - CC的神奇背包
- Docker应用系列(一)| 构建Redis哨兵集群
- 利用gmpy2破解rsa
- 在Azure中创建asp.net core 应用