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.
 * [email protected]
 * 建立调用者与建立者之间的桥梁
 */
@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 有点难理解,看代码说明
    //待续
发布了14 篇原创文章 · 获赞 1 · 访问量 8044

猜你喜欢

转载自blog.csdn.net/hua199237/article/details/55664660