发表于: 2018-01-22 18:13:22
1 630
今天完成的事情:
1. MyBatis入门
学习文档:http://wiki.jikexueyuan.com/project/mybatis-in-action/interface-programming.html
http://www.cnblogs.com/shanheyongmu/p/5842256.html
2. Junit
学习文档:http://hao.jobbole.com/junit/
Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。主要功能包括:断言, 测试运行器(Runner),通过测试集组合测试, 测试执行顺序, 异常测试, Matchers和assertThat, 忽略测试(Ignoreing Test),测试超时, 参数化测试 (这些都是 GitHub 链接,包含使用方法)
3. Annotation 注解
学习文档:http://www.importnew.com/10294.html
http://how2j.cn/k/annotation/annotation-brief/1055.html
Annotation是一种应用于类、方法、参数、变量、构造器及包声明中的特殊修饰符。它是一种由JSR-175标准选择用来描述元数据的一种工具。
用一个词就可以描述注解,那就是元数据,即一种描述数据的数据。所以,可以说注解就是源代码的元数据。比如,下面这段代码:
1 2 3 4 | @Override public String toString() { return "This is String Representation of current object." ; } |
上面的代码中,我重写了toString()
方法并使用了@Override
注解。但是,即使我不使用@Override注解标记代码,程序也能够正常执行。那么,该注解表示什么?这么写有什么好处吗?事实上,
@Override告诉编译器这个方法是一个重写方法(描述方法的元数据),如果父类中不存在该方法,编译器便会报错,提示该方法没有重写父类中的方法。如果我不小心拼写错误,例如将toString()
写成了toStrring(){double r}
,而且我也没有使用@Override
注解,那程序依然能编译运行。但运行结果会和我期望的大不相同。
假如你想为应用设置很多的常量或参数,这种情况下,XML是一个很好的选择,因为它不会同特定的代码相连。如果你想把某个方法声明为服务,那么使用Annotation会更好一些,因为这种情况下需要注解和方法紧密耦合起来,开发人员也必须认识到这点。
@Documented –注解是否将包含在JavaDoc中
@Retention –什么时候使用该注解
@Target? –注解用于什么地方
@Inherited – 是否允许子类继承该注解
@Retention– 定义该注解的生命周期。
RetentionPolicy.SOURCE – 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
RetentionPolicy.CLASS – 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式。
RetentionPolicy.RUNTIME– 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。
@Target – 表示该注解用于什么地方。如果不明确指出,该注解可以放在任何地方。以下是一些可用的参数。需要说明的是:属性的注解是兼容的,如果你想给7个属性都添加注解,仅仅排除一个属性,那么你需要在定义target包含所有的属性。
ElementType.TYPE:用于描述类、接口或enum声明
ElementType.FIELD:用于描述实例变量
ElementType.METHOD
ElementType.PARAMETER
ElementType.CONSTRUCTOR
ElementType.LOCAL_VARIABLE
ElementType.ANNOTATION_TYPE 另一个注释
ElementType.PACKAGE 用于记录java文件的package信息
那么,注解的内部到底是如何定义的呢?Annotations只支持基本类型、String及枚举类型。注释中所有的属性被定义成方法,并允许提供默认值。
1 2 3 4 5 6 7 8 9 | @Target (ElementType.METHOD) @Retention (RetentionPolicy.RUNTIME) @interface Todo { public enum Priority {LOW, MEDIUM, HIGH} public enum Status {STARTED, NOT_STARTED} String author() default "Yash" ; Priority priority() default Priority.LOW; Status status() default Status.NOT_STARTED; } |
下面的例子演示了如何使用上面的注解。
1 2 3 4 5 | @Todo (priority = Todo.Priority.MEDIUM, author = "Yashwant" , status = Todo.Status.STARTED) public void incompleteMethod1() { //Some business logic is written //But it’s not complete yet } |
如果注解中只有一个属性,可以直接命名为“value”,使用时无需再标明属性名。
1 2 3 4 5 6 | @interface Author{ String value(); } @Author ( "Yashwant" ) public void someMethod() { } |
但目前为止一切看起来都还不错。我们定义了自己的注解并将其应用在业务逻辑的方法上。现在我们需要写一个用户程序调用我们的注解。这里我们需要使用反射机制。如果你熟悉反射代码,就会知道反射可以提供类名、方法和实例变量对象。所有这些对象都有getAnnotation()这个方法用来返回注解信息。我们需要把这个对象转换为我们自定义的注释(使用 instanceOf()检查之后),同时也可以调用自定义注释里面的方法。看看以下的实例代码,使用了上面的注解:
1 2 3 4 5 6 7 8 9 10 | Class businessLogicClass = BusinessLogic. class ; for (Method method : businessLogicClass.getMethods()) { Todo todoAnnotation = (Todo)method.getAnnotation(Todo. class ); if (todoAnnotation != null ) { System.out.println( " Method Name : " + method.getName()); System.out.println( " Author : " + todoAnnotation.author()); System.out.println( " Priority : " + todoAnnotation.priority()); System.out.println( " Status : " + todoAnnotation.status()); } } |
4. 工厂模式
http://www.importnew.com/24128.html
工厂模式的意义在于对象的创建、管理可以使用工厂去管理,而不是创建者自身。最典型的工厂模式使用者就是Spring,Spring内部的容器就是一个工厂,所有的bean都由这个容器管理,包括它们的创建、销毁、注入都被这个容器管理。
工厂模式分简单工厂和抽象工厂。它们的区别在于抽象工厂抽象程度更高,把工厂也抽象成了一个接口,这样可以再每添加一个新的对象的时候而不需要修改工厂的代码。
比如有个Repository接口,用于存储数据,有DatabaseRepository,CacheRepository,FileRepository分别在数据库,缓存,文件中存储数据,定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public interface Repository { void save(Object obj); } class DatabaseRepository implements Repository { @Override public void save(Object obj) { System.out.println( "save in database" ); } } class CacheRepository implements Repository { @Override public void save(Object obj) { System.out.println( "save in cache" ); } } class FileRepository implements Repository { @Override public void save(Object obj) { System.out.println( "save in file" ); } } |
简单工厂的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class RepositoryFactory { public Repository create(String type) { Repository repository = null ; switch (type) { case "db" : repository = new DatabaseRepository(); break ; case "cache" : repository = new CacheRepository(); break ; case "file" : repository = new FileRepository(); break ; } return repository; } public static void main(String[] args) { RepositoryFactory factory = new RepositoryFactory(); factory.create( "db" ).save( new Object()); factory.create( "cache" ).save( new Object()); factory.create( "file" ).save( new Object()); } } |
简单工厂的弊端在于每添加一个新的Repository,都必须修改RepositoryFactory中的代码
明天计划的事情:Mybatis 和 Spring 的整合
遇到的问题:午饭后注意力不能集中,有厌学情绪,慢慢调整。
收获:
1. 这个网站(ImportNew)翻译了很多国外文章和教程,有些好玩的材料,比如理解 Java 的8张图片:http://www.importnew.com/11725.html
2. 这个网站整合了很多官方资源文档的连接,比如 Spring 的系统学习资料:http://hao.jobbole.com/spring-framework/
评论