java中的注解

注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,没加,则等于没有某种标记,以后,javac编译器,开发工具和其他程序可以用反射来了解你的类及各种元素上有无何种标记,看你有什么标记,就去干相应的事。标记可以加在包,类,字段,方法,方法的参数以及局部变量上。

常见的注解有:@Deprecated,@Override,@SuppressWarnings


注解其实就是一个特殊的类,他可以有自己的属性;


1.定义一个注解类:

/**
* 自定义一个注解类
*/

@Target(value={ElementType.METHOD,ElementType.TYPE})//表明该注解只能存在类,或者方法上面
@Retention(RetentionPolicy.RUNTIME)//表明该注解在虚拟机运行时仍然纯在;
public @interface MyAnnotation {

     //定义了一个color属性,该属性的返回值是String类型;这样使用:@MyAnnotation(color="red");指定了默认值为blue;
     String color() default "blue";
    
     //定义了一个value属性,该属性的返回值只能是基本类型加String类型或数组;
     //如果没有其他的属性,在定义注解时,可以:@MyAnnotation("haha")
     String value();
    
     //该属性的返回值是数组类型;
     int[]  numbers() default {22,11,32};
}

/**
public enum RetentionPolicy {
    SOURCE,//源代码
    CLASS,//字节码
    RUNTIME//虚拟机运行时;
}
*
*/

2。在需要的地方创建注解类的对象,即@XX;

<span style="font-size:18px;">/**
*定义了一个注解,该注解加在类上,这是一个注解类的对象,可以为其指定属性值;
*/
     @MyAnnotation (color= "red",value= "haha",numbers={12,19,15})
     public class AnnotationTest {
          String str= "";
        
          @MyAnnotation(value ="haha" ,numbers={12,14,15}) //在方法上加注解
          public static void main(String[] args) {
               //利用反射获取类上的注解对象:该注解是类上的注解!!!
               MyAnnotation annotation=AnnotationTest.class .getAnnotation( MyAnnotation.class);
               if(annotation!=null){//该类上确实加有注解
                    System.out.println(annotation.value());//获取属性的值
                    System.out.println(annotation.numbers()[1]);//获取返回值为数组的属性的值;
               }
             
          }
     }</span>


3.定义在方法上的注解:以获取数据库连接为例,使用注解替换掉置文

driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///day11
username=root
password=root


①定义注解:DbInfo.java 

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 定义注解,用来演示替代获取数据库连接的配置文件
 * @author oterman
 */
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DbInfo {

     String driver();

     String url();

     String user();

     String password();

}
②使用注解

public class JdbcUtil {
     @DbInfo(driver= "com.mysql.jdbc.Driver",url="jdbc:mysql:///myestore" ,user="root" ,password="root")
     public static Connection getConn() throws Exception, NoSuchMethodException{
           //在运行的时候,获取注解里各个属性的值;
           Class clazz=JdbcUtil.class;
           //clazz.getAnnotation(annotationClass);获取类上的注解
           //反射获取方法
          Method method= clazz.getMethod("getConn", null) ;
           //clazz.getField("").getAnnotation(annotationClass);获取字段上的属性
          
           DbInfo annotation = method.getAnnotation(DbInfo .class);//获取方法上的注解
          System. out.println(annotation.driver());
          System. out.println(annotation.url());
          System. out.println(annotation.user());
          System. out.println(annotation.password());
          
           return null ;
     }

     public static void main(String[] args) throws Exception {
           getConn();
     }

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值