Reflection
@Test public void doTransTest() { A a= new A(); A.setC(111L); B b= new B(); doTrans(a, b); } /** *@param sour 来源vo *@param dest 待转换vo */ public void doTrans(Object sour, Object dest) { if (sour == null) { dest = null; return; } //反射原理应用 Class<?> sourceClass = sour.getClass(); Class<?> destClass = dest.getClass(); for (Field field : sourceClass.getDeclaredFields()) { String fieldName = field.getName(); Object value; if (fieldName.equals("serialVersionUID")) { continue; } try { PropertyDescriptor propertyDescriptor = new PropertyDescriptor(fieldName, sourceClass); value = propertyDescriptor.getReadMethod().invoke(sour); } catch (Exception e) { //log continue; } if (value == null) { continue; } try { PropertyDescriptor propertyDescriptor = new PropertyDescriptor(fieldName, destClass); propertyDescriptor.getWriteMethod().invoke(dest, value); } catch (Exception e) { //log continue; } } }Application of SpringIOC reflection:
/** * springIOC和反射test * * @throws Exception */ @Test public void reflexTest() throws Exception { /** * 第一步,springIOC解析如下bean * <bean id="cartBean" class="com.mmall.pojo.Cart"></bean> */ //解析bean字符串xxxxx String beanId = "cartBean"; String beanClass = "com.mmall.pojo.Cart"; Class<?> cls = Class.forName(beanClass); Object object = cls.newInstance(); //模拟spring容器 Map container = Maps.newHashMap(); //bean注入spring容器,实例化成功 container.put(beanId, object); /** * 第二步:模拟ref注入 * <bean id="cartBean" class="com.mmall.pojo.Cart"></bean> * <property name="user" ref="userBean"></property> * <bean> */ //初始化refBean(userBean) String beanIdUser = "cartBean"; String beanClassUser = "com.mmall.pojo.User"; Class<?> clsUser = Class.forName(beanClassUser); Object objectUser = clsUser.newInstance(); //并注入spring容器 container.put(beanIdUser,objectUser); //得到cartBean的setUser方法 String setUserName = "setUser"; Object paramBean = container.get(beanIdUser); //cls是cart对象 Method setter = cls.getMethod(setUserName, objectUser.getClass()); //注入set方法 setter.invoke(object, paramBean); }
The above is the detailed content of java reflection application. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.