Java元注解,简单案例
- 【注解】
程序中有 注释 和注解
* 注释:给开发人员.
* 注解:给计算机看的.
注解使用:学习框架支持注解开发.
- 【JDK提供的注解】
@Override :描述方法的重写.
@SuppressWarnings :压制警告.
@Deprecated :标记过时.
- 自定义注解:
定义一个类:class
定义一个借口:interface
定义一个枚举:enum
定义一个注解:@interface
用法:
@interface MyAnno1{
}
带有属性的注解:
@interface MyAnno2{
int a() default 1;
String b();
// 注解属性的类型:基本数据类型,字符串类型String,Class,注解类型,枚举类型,以及以上类型的一维数组.
// Date d();
Class clazz();
MyAnno3 m3(); // 注解
Color c(); // 枚举
String[] arrs();
}
@MyAnno4("aaa") // 如果属性名称为value 那么使用的时候 value可以省略(只出现这一个value的属性情况下).
public class AnnotationDemo3 {
}
@interface MyAnno4{
String value();
int a() default 1;
}
【自定义注解案例】
注解类
package com.xujingyang.annotation; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; //保留到运行时
@Retention(RetentionPolicy.RUNTIME)
//只能作用在方法上
@Target(ElementType.METHOD)
public @interface MyTest { }
测试注解类
package com.xujingyang.annotation;
public class Test {
@MyTest
public void f1(){
System.out.println("f1方法执行了~~~~");
}
public void f2(){
System.out.println("f2方法执行了~~~~");
}
@MyTest
public void f3(){
System.out.println("f3方法执行了~~~~");
}
}
主测试类
package com.xujingyang.annotation;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws Exception{
Class clazz=Test.class;
Method[] methods = clazz.getMethods();
for (Method method : methods) {
// method.invoke(clazz.newInstance());
// System.out.println(method.getName());
boolean b = method.isAnnotationPresent(MyTest.class);//判断是否有添加此注解
if(b){
method.invoke(clazz.newInstance());
}
}
}
}
打印结果

案例二,使用注解方式加载获取JDBC连接的参数
注解类
package com.xujingyang.jdbc; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//保留到运行时
@Retention(RetentionPolicy.RUNTIME)
//只能在方法上添加此注解
@Target(ElementType.METHOD)
public @interface JDBCInfo {
//定义加载数据库的几种属性,可以用default关键字赋默认值
String DriverClass() default "com.mysql.jdbc.Driver";
String Url();
String User() default "root";
String Pwd() default "root";
}
测试类
package com.xujingyang.jdbc; import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager; public class Conn {
public static void main(String[] args) throws Exception {
System.out.println(getConnection());//打印连接地址,说明成功
} @JDBCInfo(Url="jdbc:mysql://localhost:3306/day16")
public static Connection getConnection() throws Exception{
//加载类的字节码对象
Class<Conn> clazz=Conn.class; //获取此方法
Method method = clazz.getMethod("getConnection"); //获取注解对象
JDBCInfo info = method.getAnnotation(JDBCInfo.class); //获取各个已赋值的属性
String driverClass = info.DriverClass();
String url = info.Url();
String user = info.User();
String pwd = info.Pwd(); //注册驱动
Class.forName(driverClass); //获得连接
return DriverManager.getConnection(url, user, pwd);
}
}
打印结果

注解的简单用法就记这么多了,更深入的研究待以后来搞
Java元注解,简单案例的更多相关文章
- 使用Java元注解和反射实现简单MVC框架
Springmvc的核心是DispatcherServlet来进行各种请求的拦截,进而进行后续的各种转发处理.流程图如下: 说明:客户端发出一个http请求给web服务器,web服务器对http请求进 ...
- Spring注解与Java元注解小结
注解 Annotation 基于注解的开发,使得代码简洁,可读性高,简化的配置的同时也提高了开发的效率,尤其是SpringBoot的兴起,随着起步依赖和自动配置的完善,更是将基于注解的开发推到了新的高 ...
- Java元注解—— @Retention @Target @Document @Inherited
java中元注解有四个: @Retention @Target @Document @Inherited: @Retention:注解的保留位置 @Retention(RetentionPolicy. ...
- Java元注解@Retention规则
@Retention是java当中的一个元注解,该元注解通常都是用于对软件的测试 1.适用方式: @Retention(RetentionPolicy.RUNTIME) @interf ...
- Java 元注解
元注解@Target,@Retention,@Documented,@Inherited * * @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: * Elemen ...
- Java元注解
元注解是指注解的注解,包括@Retention @Target @Document @Inherited四种. 1.@Retention: 定义注解的保留策略@Retention(RetentionP ...
- java元注解(注解在注解上的注解)
//ElementType.TYPE 给类.接口.枚举上使用 @Target(ElementType.TYPE)//给注解进行注解,表示该注解可以用在什么地方 //@Retention(Retenti ...
- java 导出excel(简单案例)
public class Student { private int id; private String name; private int age; private Date birth; pub ...
- java元注解 @Retention注解使用
@Retention定义了该Annotation被保留的时间长短: 1.某些Annotation仅出现在源代码中,而被编译器丢弃: 2.另一些却被编译在class文件中,注解保留在class文件中,在 ...
随机推荐
- android横屏布局文件设置
一.AndroidManifest.xml配置 1.在AndroidManifest.xml的activity(需要禁止转向的activity)配置中加入 android:screenOrient ...
- linux部署python和加入mysqldb、easy_install
一.安装easy_install 参考文章: http://www.cnblogs.com/huangjacky/archive/2012/03/28/2421866.html 安装 wget htt ...
- android中shape的属性
<shape> <!– 实心 –> <solid android:color=”#ff9d77″/> <!– 渐变 –> <gradient an ...
- 发布本地jar到Nexus仓库
mvn deploy:deploy-file -Durl=http://192.168.0.4:8081/nexus/content/repositories/thirdparty -Dreposit ...
- PHP封装返回Ajax字符串和JSON数组
<?php class DBDA { public $host="localhost"; public $uid = "root"; public $pw ...
- STL迭代器辅助函数——advance
Advance(i, n) increments the iterator i by the distance n. If n > it it , the call has no effect. ...
- BZOJ5118:Fib数列2(O1快速模)
题意:输入N,输出fib(2^N)%1125899839733759.(P=1125899839733759是素数) 思路:欧拉降幂,因为可以表示为矩阵乘法,2^N在幂的位置,矩阵乘法也可以降幂,所以 ...
- COGS1516. 棋盘上的车
[题目描述] 在n*n(n≤20)的方格棋盘上放置n 个车,求使它们不能互相攻击的方案总数. [输入格式] 一行一个正整数n. [输出格式] 一行一个正整数,即方案总数. [样例输入] 3 [样例输出 ...
- php7 新特性整理
PHP7 已经出来1年了,PHP7.1也即将和大家见面,这么多好的特性,好的方法,为什么不使用呢,也希望PHP越来越好. 在这里整理 PHP 5.1 ,PHP5.2,PHP5.3,PHP5.4,PHP ...
- ecshop彻底去版权把信息修改成自己的全教程
前台部分: 一.去掉头部title部分的ECSHOP演示站-Powered by ecshop 1.问题:“ECSHOP演示站”方法:在后台商店设置 – 商店标题修改2.问题:“ Powered by ...