简体   繁体   English

Java-反射。 设置动态创建的类对象的值

[英]Java - Reflection. Set value for the class object which are dynamically created

Hi have a class[many] for which I create object dynamically during run time. 嗨,我有一个我在运行时动态创建对象的类[许多]。 now I want to set value for the fields which are private fields . 现在我想为which are private fields设置值。 How do I set them. 我该如何设置它们。

I have seen many examples which explain this but we need to know the field name and only than the values can be set. 我已经看到许多示例来解释这一点,但是我们需要知道字段名称,而且只能设置值。

for my case I have some set of default values for set of primitive and non primitive types and find the field type during run time and set the default values for them. 就我而言,我为一组原始类型和非原始类型提供了一组默认值,并在运行时查找字段类型并为其设置了默认值。

For example: 例如:

LoginBean loginBean = new LoginBean();
Method setUserName = loginBean.getClass().getMethod("setUserName", new Class[]{String.class});
setUserName.invoke(loginBean, "myLogin");

My case is different and i don't even know the field name but have to set the default value according to field type. 我的情况不同,我什至不知道field name但必须根据字段类型设置默认值。

how to do this using reflection or even better in spring. 如何使用反射甚至在春季更好地做到这一点。

You can say yourBean.class.getFields(); 您可以说yourBean.class.getFields(); which will give array of Field . 这将给出Field的数组。

Using Field you can find its name and type , and do the desired work (setting some value, if its type is == some primitive type) 使用Field可以找到其nametype ,并完成所需的工作(如果其类型为==一些原始类型,则可以设置一些值)

This example sets default values on several fields within a class using reflection. 本示例使用反射在类中的几个字段上设置默认值。 The fields have private access, which is toggled on and off via reflection. 这些字段具有私有访问权限,可通过反射将其打开和关闭。 Field.set() is used to set the values of the field on a particular instance instead of using the setter method. Field.set()用于在特定实例上设置字段的值,而不是使用setter方法。

import java.lang.reflect.Field;
import java.util.Date;


public class StackExample {

    private Integer field1 = 3;
    private String field2 = "Something";
    private Date field3;

    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
        StackExample se = new StackExample();

        Field[] fields = se.getClass().getDeclaredFields();

        for(Field f:fields){
            if(!f.isAccessible()){
                f.setAccessible(true);
                Class<?> type = f.getType();

                if(type.equals(Integer.class)){
                    f.set(se, 100); //Set Default value
                }else if(type.equals(String.class)){
                    f.set(se, "Default");
                }else if (type.equals(Date.class)){
                    f.set(se, new Date());
                }
                f.setAccessible(false);
            }
            System.out.println(f.get(se)); //print fields with reflection
        }
    }
}

1) By Using Spring Constructor/Setter Injection. 1)通过使用Spring构造器/ Setter注入。 You dont need to know the attribute name , just type will do. 您不需要知道属性名称,只需键入即可。 Like as following: 如下所示:

<bean id="myBean" class="myBean">
  <constructor-arg type="int"><value>1</value></constructor-arg>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM