首先看一个简单的例子,直接看代码.
package com.ilucky.spring.reflect;
/**
* @author IluckySi
* @date 20140424
*/
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void getUserInfo() {
System.out.println(username + ":" + password);
}
}
package com.ilucky.spring.reflect;
/**
* @author IluckySi
* @date 20140424
*/
public class RegularMainTest {
public static void main(String[] args) {
User user = new User();
user.setUsername("Ilucky");
user.setPassword("123456");
user.getUserInfo();
}
}
/**
输出结果:
Ilucky:123456
*/
如上的主方法是采用传统方式直接调用目标类的方法,下面我们通过java反射机制间接的操作目标类.
直接看代码.
package com.ilucky.spring.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/**
* @author IluckySi
* @date 20140424
*/
public class ReflectMainTest {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws Throwable {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class clazz = classLoader.loadClass("com.ilucky.spring.reflect.User");
//通过反射机制创建User对象,相当于new User();
Constructor constructors= clazz.getDeclaredConstructor((Class[])null);//空构造函数.
User user = (User)constructors.newInstance();
//通过反射机制设置属性.相当于setUsername("ilucky")和setPassword("123456").
Method setUsername = clazz.getMethod("setUsername", String.class);//String.class方法中的参数.
setUsername.invoke(user, "Ilucky");
Method setPassword = clazz.getMethod("setPassword", String.class);
setPassword.invoke(user, "123456");
//输出用户信息.
Method getUserInfo = clazz.getMethod("getUserInfo");
getUserInfo.invoke(user);
}
}
/**
输出结果:
Ilucky:123456
*/
2.类装载器ClassLoader工作机制.JRE扩展目录ext中的JAR类包,AppClassLoader负责装载Classpath路径下的类包.直接通过代码看他们之间的关系:
package com.ilucky.spring.classloader;
/**
* @author IluckySi
* @date 20140424
*/
public class ClassLoaderTest {
public static void main(String[] args) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
System.out.println("current loader" + classLoader);
System.out.println("parent loader" + classLoader.getParent());
System.out.println("parent loader" + classLoader.getParent().getParent());
}
}
/**
输出结果:
current loadersun.misc.Launcher$AppClassLoader@192d342
parent loadersun.misc.Launcher$ExtClassLoader@6b97fd
parent loadernull
*/
通过上面的输出结果,可以得知加载class路径下的类包的装载器是AppClassLoader,AppClassLoader的父亲是ExtClassLoader,直接通过代码比较.
package com.ilucky.spring.reflect2;
/**
* @author IluckySi
* @date 20140424
*/
public class PrivateUser {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@SuppressWarnings("unused")
private void getUserInfo() {
System.out.println(username + ":" + password);
}
}
package com.ilucky.spring.reflect2;
/**
* @author IluckySi
* @date 20140424
*/
public class RegularMainTest {
public static void main(String[] args) {
PrivateUser user = new PrivateUser();
user.setUsername("Ilucky");
user.setPassword("123456");
//如下语句报错: The method getUserInfo() from the type PrivateUser is not visible.
//user.getUserInfo();
}
}
package com.ilucky.spring.reflect2;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/**
* @author IluckySi
* @date 20140424
*/
public class ReflectMainTest {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) throws Throwable {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class clazz = classLoader.loadClass("com.ilucky.spring.reflect2.PrivateUser");
//通过反射机制创建User对象,相当于new User();
Constructor constructors= clazz.getDeclaredConstructor((Class[])null);//空构造函数.
PrivateUser user = (PrivateUser)constructors.newInstance();
//通过反射机制设置属性.相当于setUsername("ilucky")和setPassword("123456").
Method setUsername = clazz.getMethod("setUsername", String.class);//String.class方法中的参数.
setUsername.invoke(user, "Ilucky");
Method setPassword = clazz.getMethod("setPassword", String.class);
setPassword.invoke(user, "123456");
//输出用户信息.
Method getUserInfo = clazz.getDeclaredMethod("getUserInfo");
getUserInfo.setAccessible(true);
getUserInfo.invoke(user, (Object[])null);
}
}
/**
输出结果:
Ilucky:123456
*/
对于java反射机制的知识先写到这里,这里只是做一下简单的回顾,要想了解更多关于java反射类的知识,上网查吧......