千锋学习的55天

千锋学习的55天


Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface JDBCInfo {

String driverClass() default "com.mysql.jdbc.Driver";
String jdbcUrl() default "jdbc:mysql://localhost:3306/day53";
String user() default "root";
String password() default "root123";

}


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

@Target(value = {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Myannotation {
String value() default “hello ananan”;
}


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

@Target(value = {ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest {
String value() default “”;
}


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

@Retention(RetentionPolicy.RUNTIME)
public @interface Systemlog {
String className();
String methodName();
}


package Dao;

import Annotation.Systemlog;

public class UserDaoImpl implements UserDao {
@Override
public void userAdd() {
System.out.println(“userAdd()”);
}

@Override
public void userDelete() {
    System.out.println("userDelete");
}

@Override
public void userUpdate() {
    System.out.println("UserUpdate");
}

@Override
public void userSelect() {
    System.out.println("UserSelect");

}

}



package Dao;

import Annotation.Systemlog;
import sun.text.resources.cldr.es.FormatData_es_419;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UserDaoWrapper implements UserDao {
private UserDao userDao;
public UserDaoWrapper(UserDao userDao) {
this.userDao = userDao;
}

@Override
public void userAdd() throws NoSuchMethodException {
    printlng("userAdd");
    userDao.userAdd();
}

@Override
public void userDelete() throws NoSuchMethodException {
    userDao.userDelete();
    printlng("userDelete");
}

@Override
public void userUpdate() throws NoSuchMethodException {
printlng("userUpdate");
}

@Override
public void userSelect() throws NoSuchMethodException {
printlng("userSelect");
}
private void printlng(String methodName) throws NoSuchMethodException {
    System.out.println("sssssssss");
    Class<? extends UserDao> aClass = userDao.getClass();
    Class<?>[] interfaces = aClass.getInterfaces();;
    Class<?> anInterface = interfaces[0];
    Method methods = anInterface.getMethod(methodName);
    if(null!=methods){
        boolean annotationPresent = methods.isAnnotationPresent(Systemlog.class);
        if(annotationPresent){
            SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
            Date s=new Date();
            String format1 = format.format(s);
            Systemlog annotation = methods.getAnnotation(Systemlog.class);
            String s1 = annotation.className();
            String s2 = annotation.methodName();
            System.out.println(format1+"-----------"+s1+"类"+s2+"()方法---运行了");
        }
    }


}

}


package demo01;

import Annotation.Myannotation;

@Myannotation
public class demo01 {
public static void main(String[] args){
int i=1;
}
}

package demo01;

import Annotation.MyTest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Demo02 {
    public static void main(String[] args) {
        Class<Tset01> tset01Class = Tset01.class;
        Method[] methods = tset01Class.getMethods();
        Arrays.stream(methods).forEach(method -> {
            boolean annotationPresent = method.isAnnotationPresent(MyTest.class);
            if (annotationPresent){
                try {
                    method.invoke(tset01Class.newInstance());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
            }else {

            }
        });
    }
}

package demo01;

import Annotation.JDBCInfo;

import java.sql.Connection;

public class demo03 {
    public static void main(String[] args) {
        Connection connection = JdbcUtil.get();
        System.out.println(connection);
    }
}


在这里插入代码片import Annotation.Systemlog;
import Dao.UserDao;
import Dao.UserDaoImpl;
import javafx.beans.property.SimpleListProperty;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.text.SimpleDateFormat;
import java.util.Date;

public class demo05 {
public static void main(String[] args) {
UserDao userDao=new UserDaoImpl();
UserDao userDao1 =(UserDao) Proxy.newProxyInstance(userDao.getClass().getClassLoader(), userDao.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object revl = null;
if (null != method) {
boolean annotationPresent = method.isAnnotationPresent(Systemlog.class);
if (annotationPresent) {
revl = method.invoke(userDao, args);
String currentTime = new SimpleDateFormat(“yyyy年MM月dd日 hh:mm:ss”).format(new Date());
Systemlog annotation = method.getAnnotation(Systemlog.class);
String s = annotation.className();
String s1 = annotation.methodName();
System.out.println(currentTime + " — " + s + “类中 —” + s1 + “()方法 — 运行了”);
} else {
revl = method.invoke(userDao, args);
}

            }
            return revl;
        }
    });
    try {
        userDao1.userDelete();
        userDao1.userAdd();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值