| 類 | 作用 |
|---|---|
| Class | 運行時類信息入口,獲取構造器、方法、字段、注解等 |
| Constructor | 構造器信息,創建實例 |
| Method | 方法信息,調用方法 |
| Field | 字段信息,讀寫屬性值 |
| Annotation | 注解信息,運行時讀取元數據 |
public class BeanCopier {
public static void copy(Object source, Object target) {
Class> sourceClass = source.getClass();
Class> targetClass = target.getClass();
Field[] fields = sourceClass.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
Field targetField = targetClass.getDeclaredField(field.getName());
targetField.setAccessible(true);
targetField.set(target, field.get(source));
} catch (NoSuchFieldException ignored) {
// 目標無此字段,跳過
} catch (Exception e) {
throw new RuntimeException("拷貝失敗: " + field.getName(), e);
}
}
}
}
// 使用
UserDTO dto = new UserDTO();
UserVO vo = new UserVO();
BeanCopier.copy(dto, vo);
public class SimpleDIContainer {
private final Map, Object> singletons = new ConcurrentHashMap<>();
public T getBean(Class clazz) {
return clazz.cast(singletons.computeIfAbsent(clazz, this::createInstance));
}
private T createInstance(Class clazz) {
try {
// 查找帶 @Autowired 的構造器
Constructor>[] constructors = clazz.getDeclaredConstructors();
Constructor> target = Arrays.stream(constructors)
.filter(c -> c.isAnnotationPresent(Autowired.class))
.findFirst()
.orElseGet(() -> clazz.getDeclaredConstructor());
target.setAccessible(true);
Object[] args = Arrays.stream(target.getParameterTypes())
.map(this::getBean) // 遞彡注入依賴
.toArray();
return clazz.cast(target.newInstance(args));
} catch (Exception e) {
throw new RuntimeException("創建 Bean 失敗: " + clazz, e);
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RequestMapping {
String value();
RequestMethod method() default RequestMethod.GET;
}
@Controller
public class UserController {
@RequestMapping("/users")
public List list() { ... }
@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
public User get(@PathVariable Long id) { ... }
}
// 掃描註冊
public class HandlerMapping {
public Map scan(String basePackage) {
Map mapping = new HashMap<>();
// 掃描包下所有類...
for (Method method : clazz.getDeclaredMethods()) {
RequestMapping rm = method.getAnnotation(RequestMapping.class);
if (rm != null) {
mapping.put(rm.value(), method);
}
}
return mapping;
}
}
public class LoggingProxy implements InvocationHandler {
private final Object target;
public LoggingProxy(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
long start = System.nanoTime();
try {
return method.invoke(target, args);
} finally {
long cost = System.nanoTime() - start;
System.out.printf("[%s] %s 耗時: %d ms%n",
target.getClass().getSimpleName(), method.getName(), cost / 1_000_000);
}
}
public static Object createProxy(Object target) {
return Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new LoggingProxy(target)
);
}
}
public class PluginLoader {
public List loadPlugins(String jarDir) {
List plugins = new ArrayList<>();
File dir = new File(jarDir);
for (File jar : dir.listFiles((d, n) -> n.endsWith(".jar"))) {
try (URLClassLoader loader = new URLClassLoader(new URL[]{jar.toURI().toURL()})) {
ServiceLoader loader = ServiceLoader.load(Plugin.class, loader);
for (Plugin plugin : loader) {
plugins.add(plugin);
}
}
}
return plugins;
}
}
suppressAccessChecks 權限opens package to module 才能深度反射反射是框架開發的基石 (Spring、Hibernate、MyBatis 等),業務代碼慎用,工具類/框架代碼善用。掌握上述 5 大實戰場景,覆蓋 90% 反射應用需求。