简体   繁体   English

spring如何实现这项技术?

[英]how does spring implement this technology?

today,i learned that we could use spring's @AutoWired annotation to complete auto-injection, @AutoWired could be used in many conditions ,like 今天,我了解到我们可以使用spring的@AutoWired批注完成自动注入,@ AutoWired可以在很多情况下使用,例如

@AutoWired
public void setInstrument(Instrument instrument){
  this.instrument = instrument;
}

but we can also put the @AutoWired on a private field,like this 但是我们也可以将@AutoWired放在私有字段中,像这样

@AutoWired
private Instrument instrument;

i was wondering ,how could spring inject an object into a private field,i know we could use reflection of java to get some meta data,when i use reflection to set a object on a private field ,here comes a problem ,following is the stacktrace 我想知道,如何将一个对象春季注入私有字段,我知道我们可以使用java的反射来获取一些元数据,当我使用反射在私有字段上设置对象时,出现了一个问题,以下是堆栈跟踪

 java.lang.IllegalAccessException: Class com.wire.with.annotation.Main can not access a member of class com.wire.with.annotation.Performer with modifiers "private"

some body can explain it ? 有人可以解释吗? why spring could inject an object into a private field with no setter method . 为什么spring可以使用setter方法将对象注入私有领域。 thanks a lot 非常感谢

This is done using reflection you need to Filed.setAccessible(true) to access the private fields. 这是通过使用反射完成的,您需要Filed.setAccessible(true)来访问private字段。

privateField.setAccessible(true);//works ,if java security manager is disable

update:- 更新:

eg- 例如-

public class MainClass {
    private String string="Abcd";

    public static void main(String... arr) throws SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchFieldException{
        MainClass mainClass=new MainClass();
        Field stringField=MainClass.class.getDeclaredField("string");
        stringField.setAccessible(true);//making field accessible 
        /*if SecurityManager enable then,  
        java.security.AccessControlException: access denied will be thrown here*/
        stringField.set(mainClass, "Defgh");//seting value to field as it's now accessible
        System.out.println("value of string ="+stringField.get(mainClass));//getting value from field then printing it on console
    }
}

Java Security manager(if enable) also prevents Spring from accessing private fields Java安全管理器(如果启用)还阻止Spring访问私有字段

I guess you forgot to set setAccessible(true) on the field you're trying to access: 我猜您忘了在要访问的字段上设置setAccessible(true)

public class Main {

    private String foo;

    public static void main(String[] args) throws Exception {
        // the Main instance
        Main instance = new Main();
        // setting the field via reflection
        Field field = Main.class.getDeclaredField("foo");
        field.setAccessible(true);
        field.set(instance, "bar");
        // printing the field the "classic" way
        System.out.println(instance.foo); // prints "bar"
    }

}

Please read this related post too. 请也阅读此相关文章

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

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