jAVA 注解 及使用(Dagger2&ButterKnief)

  1. 基础知识

    元注解

    • @target
    • @retention
    • @@Documented
    • @Inherited
      jdk文档的注释

Retention

  1. 注解用法
//注解实例
@MyAnotation(grade = "我的中学", schoolName = "吉安")
public class AnotationTest {
	public static void main(String[] args) {
		if (AnotationTest.class.isAnnotationPresent(MyAnotation.class)) {
			MyAnotation anotation = AnotationTest.class.getAnnotation(MyAnotation.class);
			System.out.println(anotation.grade() + anotation.schoolName());
		}
	}
}

构建注解器

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
public @interface MyAnotation {
	String schoolName();

	String grade() default "北源中学";

}

打印结果
我的中学吉安中学

  1. Dagger2 的使用
    先看他人的详解

  2. 最简单的使用

	//创建一个需要构建的类
	public class Person {
	    private static final String TAG = "Person";
	
	    public Person() {
	        Log.i(TAG, "dagger person create");
	    }
	}

	//构建一个module 
	@Module
public class MainModule {
    @Provides 
    @Singleton
    Person providerPerson(){
        return new Person();
    }
}

/**
 * Created by pengjf on 2017/2/18.
 * 1042293434@qq.com
 * 建立调用者与建立者之间的桥梁
 */
@Component(modules = MainModule.class)
@Singleton
public interface MainComponent {
    void inject(TestDaggerActivity activity);
}

/**
 * 特别注意
 * DaggerMainComponent 这个类必须先用as编译一遍才被创立
 * 点击进入可以看到这么一个方法
 *
 *public MainComponent build() {
 if (mainModule == null) {
 this.mainModule = new MainModule();
 }
 return new DaggerMainComponent(this);
 }
 */

public class TestDaggerActivity extends AppCompatActivity {
    @Inject
    Person person1;
    @Inject
    Person person2 ;
    @Bind(R.id.tv_text)
    TextView tvText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dagger);
        ButterKnife.bind(this);
        MainComponent component = DaggerMainComponent.builder().mainModule(new MainModule()).build();
        component.inject(this);

        initView();
    }

    private void initView() {
        tvText.setText(person1.toString() +"\n"+ person2.toString());
    }
}
public class TestDaggerActivity extends AppCompatActivity {
    @Inject
    Person person1;
    @Inject
    Person person2 ;
    @Bind(R.id.tv_text)
    TextView tvText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dagger);
        ButterKnife.bind(this);
        MainComponent component = DaggerMainComponent.builder().mainModule(new MainModule()).build();
        component.inject(this);

        initView();
    }

    private void initView() {
        tvText.setText(person1.toString() +"\n"+ person2.toString());
    }
}
几点说明
  • @provides 是dagger的一个方法的提供在module中并加载需注入的类
  • 在MainModule 类中我们有一个@singleton 这么一个注解 那么必须在MainComponent 中也必须加上@singleton 否则编译不通过
  • 运行可以发现textView显示的person1 与person2的toString方法都是一样说明person1 与person2构建的是同一个类,但是如果我们创建另一个Activity,并另创一个MainComponent 得到的person类是不同的。
  • 那如果要保证person是唯一单例类则需要用到Dagger2的另一个关键字@scope
  • @Scope 有点难理解,看代码说明
    //待续
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值