博客
关于我
Java基础--Annotation
阅读量:505 次
发布时间:2019-03-07

本文共 4779 字,大约阅读时间需要 15 分钟。

其他网址

注解设值/取值

简介

属性可以是单个对象,也可以是数组

可以设值属性为枚举类型;

实例 

package org.example.a;import java.lang.annotation.*;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.List;@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD})@Documented@interface Parent {    Color[] value() default {};}@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD})@Documented@interface Bottom {    String name() default "";}enum Color {    RED,    @Bottom(name = "浅红色")    @Parent({RED})    LIGHT_RED,    @Bottom(name = "深红色")    @Parent({RED, LIGHT_RED})    DARK_RED;    List
childrens = new ArrayList<>(); static List
bottoms = new ArrayList<>(); static { for (Color value : Color.values()) { try { Field field = Color.class.getField(value.name()); if (field.isAnnotationPresent(Bottom.class)) { bottoms.add(value); } Parent parent = field.getAnnotation(Parent.class); if (parent != null) { for (Color color : parent.value()) { color.childrens.add(value); } } } catch (NoSuchFieldException e) { e.printStackTrace(); } } }}public class Demo { public static void main(String[] args) { System.out.println("Color.RED.childrens: " + Color.RED.childrens); System.out.println("Color.LIGHT_RED.childrens: " + Color.LIGHT_RED.childrens); System.out.println("Color.bottoms: " + Color.bottoms); try { Field field = Color.class.getField(Color.LIGHT_RED.name()); if (field.isAnnotationPresent(Bottom.class)) { System.out.println("Color.LIGHT_RED的注解属性名: " + field.getAnnotation(Bottom.class).name()); } } catch (NoSuchFieldException e) { e.printStackTrace(); } }}

执行结果

Color.RED.childrens:         [LIGHT_RED, DARK_RED]Color.LIGHT_RED.childrens:   [DARK_RED]Color.bottoms:               [LIGHT_RED, DARK_RED]Color.LIGHT_RED的注解属性名: 浅红色

Annotation+反射 实现工厂模式

其他网址

简介

需求:

代码 

package org.example.a;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;//业务接口interface IMessage{    public void send(String msg);}//业务接口实现子类class IMessageImpl1 implements IMessage{    @Override    public void send(String msg) {        System.out.println("send(IMessageImpl1): " + msg);    }}//业务接口实现子类class IMessageImpl2 implements IMessage{    @Override    public void send(String msg) {        System.out.println("send(IMessageImpl2): " + msg);    }}class Factory{    private Factory() {}    public static 
T getInstance(Class
clazz) { try { //利用反射获取实例化对象 return (T) new MessageProxy().bind(clazz.getDeclaredConstructor().newInstance()); } catch (Exception e) { return null; } }}class MessageProxy implements InvocationHandler { private Object target; public Object bind(Object target) { this.target = target; return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); } public boolean connect() { System.out.println("connect(proxy)"); return true; } public void close() { System.out.println("close(proxy)"); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if(this.connect()) { return method.invoke(this.target, args);//代理调用 }else { throw new Exception("cannont send"); } } finally { this.close(); } }}@Target({ElementType.TYPE, ElementType.METHOD}) //只能用在类和方法上@Retention(RetentionPolicy.RUNTIME)@interface UseMessage{ //定义要使用的类型 public Class
clazz();}//Annotation定义使用类。红色部分可以修改为其他实现类,实现调用不同类输出。@UseMessage(clazz = IMessageImpl1.class)class MessageService{ private IMessage message; //定义业务处理 public MessageService() { UseMessage use = MessageService.class.getAnnotation(UseMessage.class); this.message = (IMessage) Factory.getInstance(use.clazz()); //通过Annotation获取 } public void send(String msg) { this.message.send(msg); }}public class Demo { public static void main(String[] args) { MessageService messageService = new MessageService(); messageService.send("www.sina.com.cn"); }}

输出结果

connect(proxy)

send(IMessageImpl1): www.sina.com.cn
close(proxy)

转载地址:http://zytjz.baihongyu.com/

你可能感兴趣的文章
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>