ElemenetType.CONSTRUCTOR 构造器声明 ElemenetType.FIELD 域声明(包括 enum 实例) ElemenetType.LOCAL_VARIABLE 局部变量声明 ElemenetType.METHOD 方法声明 ElemenetType.PACKAGE 包声明 ElemenetType.PARAMETER 参数声明 ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
RetentionPolicy.SOURCE 注解将被编译器丢弃 RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃 RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息
package com.test.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class MyAnnotation {
/**
* 注解类
* @author T4980D
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyClassAnnotation {
String uri();
String desc();
}
/**
* 构造方法注解
* @author T4980D
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.CONSTRUCTOR)
public @interface MyConstructorAnnotation {
String uri();
String desc();
}
/**
* 我的方法注解
* @author Owner
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyMethodAnnotation {
String uri();
String desc();
}
/**
* 字段注解定义
* @author Owner
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyFieldAnnotation {
String uri();
String desc();
}
/**
*
* 可以同时应用到类上和方法上
* @author T4980D
*
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Yts {
// 定义枚举
public enum YtsType {
util, entity, service, model
}
// 设置默认值
public YtsType classType() default YtsType.util;
// 数组
int[] arr() default {3, 7, 5};
String color() default "blue";
}
}
package com.test.annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.test.annotation.MyAnnotation.MyClassAnnotation;
import com.test.annotation.MyAnnotation.MyConstructorAnnotation;
import com.test.annotation.MyAnnotation.MyFieldAnnotation;
import com.test.annotation.MyAnnotation.MyMethodAnnotation;
import com.test.annotation.MyAnnotation.Yts;
import com.test.annotation.MyAnnotation.Yts.YtsType;
@MyClassAnnotation(desc = "The class", uri = "com.test.annotation.Test")
@Yts(classType =YtsType.util)
public class TestAnnotation {
@MyFieldAnnotation(desc = "The class field", uri = "com.test.annotation.Test#id")
private String id;
@MyConstructorAnnotation(desc = "The class constructor", uri = "com.test.annotation.Test#MySample")
public TestAnnotation() {
}
public String getId() {
return id;
}
@MyMethodAnnotation(desc = "The class method", uri = "com.test.annotation.Test#setId")
public void setId(String id) {
System.out.println(" method info: "+id);
this.id = id;
}
@MyMethodAnnotation(desc = "The class method sayHello", uri = "com.test.annotation.Test#sayHello")
@Yts
public void sayHello(String name){
if(name == null || name.equals("")){
System.out.println("hello world!");
}else{
System.out.println(name + "\t:say hello world!");
}
}
public static void main(String[] args) throws Exception {
Class<TestAnnotation> clazz = TestAnnotation.class;
// 得到类注解
MyClassAnnotation myClassAnnotation = clazz.getAnnotation(MyClassAnnotation.class);
System.out.println(myClassAnnotation.desc() + " "+ myClassAnnotation.uri());
// 得到构造方法注解
Constructor<TestAnnotation> cons = clazz.getConstructor(new Class[]{});
MyConstructorAnnotation myConstructorAnnotation = cons.getAnnotation(MyConstructorAnnotation.class);
System.out.println(myConstructorAnnotation.desc() + " "+ myConstructorAnnotation.uri());
// 获取方法注解
Method method = clazz.getMethod("setId", new Class[]{int.class});
MyMethodAnnotation myMethodAnnotation = method.getAnnotation(MyMethodAnnotation.class);
System.out.println(myMethodAnnotation.desc() + " "+ myMethodAnnotation.uri());
// 获取字段注解
Field field = clazz.getDeclaredField("id");
MyFieldAnnotation myFieldAnnotation = field.getAnnotation(MyFieldAnnotation.class);
System.out.println(myFieldAnnotation.desc() + " "+ myFieldAnnotation.uri());
}
}
package com.test.annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import com.test.annotation.MyAnnotation.MyClassAnnotation;
import com.test.annotation.MyAnnotation.MyMethodAnnotation;
import com.test.annotation.MyAnnotation.Yts;
import com.test.annotation.MyAnnotation.Yts.YtsType;
public class ParseAnnotation {
/**
* 解析方法注解
* @param <T>
* @param clazz
*/
public static <T> void parseMethod(Class<T> clazz) {
try {
T obj = clazz.newInstance();
for (Method method : clazz.getDeclaredMethods()) {
MyMethodAnnotation methodAnnotation = method.getAnnotation(MyMethodAnnotation.class);
if (methodAnnotation!=null) {
//通过反射调用带有此注解的方法
method.invoke(obj, methodAnnotation.uri());
}
Yts yts = (Yts) method.getAnnotation(Yts.class);
if (yts != null) {
if (YtsType.util.equals(yts.classType())) {
System.out.println("this is a util method");
} else {
System.out.println("this is a other method");
}
System.out.println(Arrays.toString(yts.arr())); //打印数组
System.out.println(yts.color()); //输出颜色
}
System.out.println("\t\t-----------------------");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析类注解
* @param <T>
* @param clazz
*/
public static <T> void parseType(Class<T> clazz) {
try {
Yts yts = (Yts) clazz.getAnnotation(Yts.class);
if (yts != null) {
if (YtsType.util.equals(yts.classType())) {
System.out.println("this is a util class");
} else {
System.out.println("this is a other class");
}
}
MyClassAnnotation classAnnotation = (MyClassAnnotation) clazz.getAnnotation(MyClassAnnotation.class);
if (classAnnotation != null) {
System.err.println(" class info: "+classAnnotation.uri());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
parseMethod(TestAnnotation.class);
parseType(TestAnnotation.class);
}
}
@Retention(RetentionPolicy.RUNTIME)//代表Permission注解保留在的阶段
@Target(ElementType.METHOD)//标注在方法上面
public @interface Permission {
/** 模块 */
String module();
/** 权限值 */
String privilege();
}
@Permission(module="department",privilege="view")
public String departmentlistUI(){
}
String actionName=invocation.getProxy().getActionName();
String methodName=invocation.getProxy().getMethod();
System.out.println("拦截到:action的名字:"+actionName+"方法名:"+methodName);
private boolean validate(ActionInvocation invocation) throws SecurityException, NoSuchMethodException {
String methodName=invocation.getProxy().getMethod();
Method currentMethod = invocation.getAction().getClass().getMethod(methodName);
if(currentMethod != null && currentMethod.isAnnotationPresent(Permission.class)){
//得到方法上的注解
Permission permission = currentMethod.getAnnotation(Permission.class);
//该方法上的所需要的权限
SystemPrivilege methodPrivilege = new SystemPrivilege(new SystemPrivilegePK(permission.module(), permission.privilege()));
//得到当前登录的用户
Employee e = (Employee) ActionContext.getContext().getSession().get("loginUser");
//遍历当前用户下的所有的权限组
for(PrivilegeGroup group : e.getGroups()){
//如果该权限组下包含,要访问该方法所需要的权限,就放行
if(group.getPrivileges().contains(methodPrivilege)){
return true;
}
}
//说明遍历的该用户所有的权限组,没有发现该权限,说明没有该权限
return false;
}
//没有标注注解,表示谁都可以调用该方法
return true;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有