java annotation注释

news/2024/5/19 2:26:57 标签: annotation, 注释, java, 反射

java annotation讲解 :http://danwind.iteye.com/blog/1140831/

 

java annotation实例:

 

java">package com.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.FIELD,ElementType.METHOD})
public @interface DataBaseBean
{
	public int i = 0;
	public String value() default "mysql";
}

 

利用java反射,和注释的测试用例

   class DataBase{

java">	@DataBaseBean
	private String name;
	@OracleAnnotation("oracle")
	private Object driver;
	private int length;
	@DataBaseBean
	public String getName(String j,String i, int ii,java.util.Date d)
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Object getDriver()
	{
		return driver;
	}
	public void setDriver(Object driver)
	{
		this.driver = driver;
	}
	public int getLength()
	{
		return length;
	}
	public void setLength(int length)
	{
		this.length = length;
	}
	
}
 

 

 

java">public class ReflectAnnotation
{
	public static void main(String[] args)
	{
		try
		{
			Class pojo = Class.forName("com.annotation.DataBase");
			Field[] filedList = pojo.getDeclaredFields();
			Method[] methodList = pojo.getDeclaredMethods();
			System.out.println("getClassEle----------------");
			for (int i = 0; i < filedList.length; i++)
			{
				Field file = filedList[i];
				System.out.println(Modifier.toString(file.getModifiers())+"    "+file.getType()+"    "+file.getName());
				Annotation[] annotations = file.getAnnotations();
				
				for (int j = 0; j < annotations.length; j++)
				{
					if(annotations[j].annotationType().equals(DataBaseBean.class))
					{
						DataBaseBean anno = (DataBaseBean)annotations[j];
						System.out.println("value值为"+anno.value());
					}
					else if(annotations[j].annotationType().equals(OracleAnnotation.class))
					{
						OracleAnnotation anno = (OracleAnnotation)annotations[j];
						System.out.println("value值为"+anno.value());
					}
					System.out.println("filed上的注释为"+annotations[j].annotationType());
				}
			}
			for (int i = 0; i < methodList.length; i++)
			{
				Method method = methodList[i];
				boolean MethodAnnotation = method.isAnnotationPresent(DataBaseBean.class);
			
				if(MethodAnnotation)
				{
					Class parameters[] = method.getParameterTypes();
				    System.out.print(Modifier.toString(method.getModifiers()) + " " + method.getReturnType() + " " + method.getName() + " (");
				    for(int k = 0;k < parameters.length;k++)
				    {
				     System.out.print("参数列表:"+parameters[k].toString());
				    }
				    DataBaseBean databaseBean = method.getAnnotation(DataBaseBean.class);
				    System.out.println(databaseBean.value());
				}
			}
		}
		catch (Exception e)
		{
			System.out.println("参数报错");
		}
	}
	
}
 

 

 

 

 

 

 

 

 


http://www.niftyadmin.cn/n/1695791.html

相关文章

Spring 自定义属性编辑器

装 Spring 自定义属性编辑器 Spring DI注入的时候可以把普通属性注入进来&#xff0c;但是像Date类型的就无法被识别。这时可以通过Spring的属性编辑器把配置文件中的字符串转化成相应的对象进行注入。 Spring有自带的属性编辑器&#xff0c;我们也可以写自定义的属性编辑器 …

非对称加密的不足

【这块肯定是没有彻底搞清楚&#xff0c;模模糊糊&#xff0c;搞清楚&#xff0c;搞清楚】 非对称加密是一个貌似完美了&#xff0c;并且工业现在也是也是这样用的&#xff0c;但是有个问题是如果公钥被窃取了咋办&#xff1f; 现在是由&#xff21;&#xff0d;&#xff0d;&a…

项目通过nginx强转为https访问后,代码中重定向的连接又变成了http协议,导致点击页面按钮,后台逻辑处理完后重定向报错了...

修改如下&#xff0c;需要在nginx对应的server下的location中增加配置&#xff0c;使重定向的地址协议取当前链接的协议&#xff0c;而不是nginx访问tomcat的协议&#xff0c;因为nginx访问tomcat是http的&#xff0c;并没有对tomcat修改为https访问&#xff1b; 转载于:https:…

新微预约手机端提交无返回的解决办法

说起这个新微预约啊 真心坑啊 我刚装的时候 mapi文件的url参数配置错了 害我搞了一晚上没搞定 第二天早上茅塞顿开加上 州城阿泽 的提醒 找到了解决办法&#xff0c;这个是我自己的原因不关群主的事情&#xff01;~~ 说起这个手机端提交无返回但是确实提交了的这个问题&#xf…

oracle job实例(转)

转 http://xurichusheng.iteye.com/blog/1328406 定时执行的job 1、创建表 Sql代码 -- create table CREATE TABLE g_test(c_id NUMBER(28) PRIMARY KEY,c_date DATE); 2、创建序列 Sql代码 --create sequence CREATE SEQUENCE seq_test MINVALUE 1 MAXVALUE 99999999…

Oracle数据块block

Oracle块是建筑数据库的基石。是数据库的最小i/o单位。Oracle读取数据文件时是以块的形式读取的。 Block&#xff08;块&#xff09;最基本的存储单元 Extent&#xff08;范围&#xff09;一次分配的连续的块Segment&#xff08;段&#xff09;属于同一对象的范围组成一个段Ta…

cisco 2950 3550 3750 系列交换机密码破解

cisco 2950 3550 3750 系列交换机密码破解#本文中仅以2950 交换机为例进行说明 破解密码原则&#xff1a;只删除密码 &#xff0c;不破坏配置#本文中的#号表示注释的意思 #第一步. 连接交换机的console口到终端 #第二步. 按住交换机面板上的mode键的同时 插入电源&#xff0c;松…

oracle大批数据加载

1.使用字表查询插入数据 insert into employee (name,num,sal) select name,num,sql from emp where num between 0 end 20; 当需要加载大量数据的时候&#xff0c;使用 insert /*APPEND */ into employee (name,num,sal) select name,num,sql from emp where num between 0…