AutowiredAnnotationBeanPostProcessor
当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有@Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。
interface Member
java.lang.reflect.Member
Field,Method,Constructor都要继承的接口,它表示一个类中的元素
/** * Field,Method,Constructor都要继承的接口,它表示一个类中的元素 */ public interface Member { //表示类定义中全部(包括继承)的某类public元素 public static final int PUBLIC = 0; //表示当前类定义中(不包括继承)的全部某类元素 public static final int DECLARED = 1; //取得定义此元素的类 public Class getDeclaringClass(); //取得元素名 public String getName(); //取得元素修饰符 public int getModifiers(); }
interface AnnotatedElement
方法摘要
<T extends Annotation> T getAnnotation(Class<T> annotationClass)
如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。
Annotation[]getAnnotations()
返回此元素上存在的所有注释。
Annotation[]getDeclaredAnnotations()
返回直接存在于此元素上的所有注释。
booleanisAnnotationPresent(Class<? extends Annotation> annotationClass)
如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。
class AccessibleObject
implements java.lang.reflect.AnnotatedElement
java.lang.reflect.AccessibleObject
AccessibleObject是 Field、Method 和 Constructor 对象的基类。
它提供了将反射的对象标记为在使用时取消默认Java 语言访问控制检查的能力。
对于公共成员、默认(打包)访问成员、受保护成员和私有成员,在分别使用 Field、Method 或 Constructor 对象来设置或获得字段、调用方法,或者创建和初始化类的新实例的时候,会执行访问检查。
interface Annotation
java.lang.annotation.Annotation
所有 annotation 类型都要扩展的公共接口
java.lang.Class annotationType();
返回此 annotation 的注释类型
java.beans.PropertyDescriptor
PropertyDescriptor类表示JavaBean类通过存储器导出一个属性。主要方法:
1、getPropertyType(),获得属性的Class对象。
2、getReadMethod(),获得用于读取属性值的方法;
getWriteMethod(),获得用于写入属性值的方法。
3 、setReadMethod(Method readMethod),设置用于读取属性值的方法;
setWriteMethod(MethodwriteMethod),设置用于写入属性值的方法;
//通过属性名获取对应的值,利用反射方法,如下: TestBean bean = new TestBean(); String propertyName = "field"; //给一个属性,获取值 PropertyDescriptor pd = new PropertyDescriptor(propertyName,bean.getClass()); Method methodGetX = pd.getReadMethod();//Read对应get()方法 Object reValue = methodGetX.invoke(bean); //给某个属性设置值,如下: String propertyName2 = "field";//给一个属性,设置值 PropertyDescriptor pd2 = new PropertyDescriptor(propertyName2,bean.getClass()); Method methodSetY = pd2.getWriteMethod();//Write对应set()方法 methodSetY.invoke(bean,3);
例子:
import java.beans.PropertyDescriptor; import java.lang.reflect.Method; public class Test { private String field; /** * @param field the field to set */ public void setField(String field) { this.field = field; System.out.println("set"); } /** * @return the field */ public String getField() { return ""; } public static void main(String[] args) throws Exception { Method method = new PropertyDescriptor("field", Test.class).getWriteMethod(); String clazz = new PropertyDescriptor("field", Test.class).getPropertyType().getName(); System.out.println("======="+clazz); Test a = new Test(); method.invoke(a, "aaa"); System.out.println(a.field); } }
结果:
=======java.lang.String
set
aaa
org.springframework.util.ReflectionUtils
反射操作类
ReflectionUtils.makeAccessible(method);
public static void makeAccessible(Method method) { if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) { method.setAccessible(true); } }
Arrays.copyOf(elements, elements.length);
System.arraycopy(elements, head, a, 0, size());
相关推荐
- AutowiredAnnotationBeanPostProcessor:处理自动注入 - AnnotationAwareAspectJAutoProxyCreator:来做AOP功能; - xxx.... - 增强的功能注解: AsyncAnnotationBeanPostProcessor - .... 2. 事件驱动模型...
[[03 08:58:22,539 INFO ] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.(AutowiredAnnotationBeanPostProcessor.java:153) - JSR-330 'javax.inject.Inject' annotation ...
这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。...
2.AutowiredAnnotationBeanPostProcessor 3.ApplicationListenerDetector 接下来看看里面都干了啥 CommonAnnotationBeanPostProcessor 这一步, 主要是扫描作用. 1. 扫描 @PostConstruct 和 @PreDestroy (这个很直观...
org.springframework.context.annotation.ConfigurationClassPostProcessor@705202d1 org.springframework.context.annotation.internalAutowiredAnnotation.internaly .AutowiredAnnotationBeanPostProcessor @ ...