【Java EE 学习第49天】【Spring学习第一天】【MVC】【注解回顾】
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【Java EE 学习第49天】【Spring学习第一天】【MVC】【注解回顾】,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2020字,纯文字阅读大概需要3分钟。
内容图文
一、MVC
1.使用Spring有一个非常大的好处,那就是能够实现完全面向接口编程,传统的使用Dao、Service并不能实现完全的面向接口编程。
2.示例:https://github.com/kdyzm/day45_spring_mvc
二、注解示例
1.定义注解方法示例:
package com.kdyzm.spring.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD}) public @interface MyAnnotation { String value() default "默认值"; }
2.使用注解方法示例
package com.kdyzm.spring.annotation; @MyAnnotation public class Person { @MyAnnotation("name属性") public String name; @Override @MyAnnotation("toString方法!!!") public String toString() { return "Person [name=" + name + "]"; } public String getName() { return name; } publicvoid setName(String name) { this.name = name; } }
3.解析注解方法示例
package com.kdyzm.spring.annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Test { public static void main(String[] args) { Class<Person> clazz=Person.class; if(clazz.isAnnotationPresent(MyAnnotation.class)){ MyAnnotation annotation=(MyAnnotation) clazz.getAnnotation(MyAnnotation.class); System.out.println(annotation.value()); } Field [] fields=clazz.getFields(); for(Field field:fields){ System.out.println(field.getName()); if(field.isAnnotationPresent(MyAnnotation.class)){ MyAnnotation annotation=field.getAnnotation(MyAnnotation.class); System.out.println(annotation.value()); } } Method[]methods=clazz.getMethods(); for(Method method:methods){ System.out.println(method.getName()); if(method.isAnnotationPresent(MyAnnotation.class)){ MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); System.out.println(annotation.value()); } } } }
4.解析结果:
默认值
name
name属性
toString
toString方法!!!
getName
setName
wait
wait
wait
equals
hashCode
getClass
notify
notifyAll
原文:http://www.cnblogs.com/kuangdaoyizhimei/p/4843978.html
内容总结
以上是互联网集市为您收集整理的【Java EE 学习第49天】【Spring学习第一天】【MVC】【注解回顾】全部内容,希望文章能够帮你解决【Java EE 学习第49天】【Spring学习第一天】【MVC】【注解回顾】所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。