package 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.METHOD)
public @interface AttachAnnotation {
String paramValue() default "河北省"; // 参数名为"paramValue" 默认值为"河北省"
}
package annotation;
/**
* @author 路人宅
*/
public class AttachEmlement {
// 普通
public void AttachDefault(String name){
System.out.println("归属:" + name);
}
// 使用注解并传入参数
@AttachAnnotation(paramValue="河北省")
public void AttachAnnotation(String name){
System.out.println("归属:" + name);
}
// 使用注解并使用默认参数
@AttachAnnotation
public void AttachAnnotationDefault(String name){
System.out.println("归属:" + name);
}
}
package annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class AnnotionOperator {
public static void main(String[] args) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
ClassNotFoundException {
AttachEmlement element = new AttachEmlement(); // 初始化一个实例,用于方法调用
Method[] methods = AttachEmlement.class.getDeclaredMethods(); // 获得所有方法
for (Method method : methods) {
AttachAnnotation annotationTmp = null;
if ((annotationTmp = method.getAnnotation(AttachAnnotation.class)) != null)
method.invoke(element, annotationTmp.paramValue());
else
method.invoke(element, "河南省");
}
}
}
/**
* This constructor performs injection on this instance,
* based on the current web application context.
* Intended for use as a base class.
* @see #processInjectionBasedOnCurrentContext
*/
public SpringBeanAutowiringSupport() {
processInjectionBasedOnCurrentContext(this);
}
/**
* Either this or {@link #getResourceToInject} needs to be overridden.
*/
protected void inject(Object target, String requestingBeanName, PropertyValues pvs) throws Throwable {
if (this.isField) {
Field field = (Field) this.member;
ReflectionUtils.makeAccessible(field);
field.set(target, getResourceToInject(target, requestingBeanName));
}
else {
if (checkPropertySkipping(pvs)) {
return;
}
try {
Method method = (Method) this.member;
ReflectionUtils.makeAccessible(method);
method.invoke(target, getResourceToInject(target, requestingBeanName));
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有