Question of accessing injected variables after injection of Spring configuration files

Keywords: Spring xml Junit Java

Code directly
1.Class OneClass

public class OneClass {
    String s;

    public void setS(String s) {
        this.s = s;
    }
}

2.Class Demo

public class Demo {
    OneClass oneClass;

    public void setOneClass(OneClass oneClass) {
        this.oneClass = oneClass;
    }

    {
        System.out.println(oneClass);
    }

    public void method(){
        System.out.println(oneClass.s);
    }

}

3.Class Test2

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test2 {
   public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("applic.xml");
        Demo demo = (Demo) app.getBean("demo");
        demo.method();
    }
}

4. Configuration file xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="one" class="com.itheima.OneClass">
        <property name="s" value="asdfgh"></property>
    </bean>

    <bean id="demo" class="com.itheima.Demo">
        <property name="oneClass" ref="one"></property>
    </bean>

</beans>

Idea: Build an OneClass class, which has a member variable s. I will inject data "asdfgh" into the xml configuration file for S. See the configuration file for details.
Build a Demo class, which has a member variable OneClass object. I also injected the previous OneClass instance into the configuration file. A static code block and a method in the Demo class print OneClass objects. Tested in Teste2, the results are as follows:

D:\work\install\Jdk1.8\bin\java.exe "-javaagent:D:\work\install\IntelliJ IDEA 2018.2.2\lib\idea_rt.jar=50334:D:\work\install\IntelliJ IDEA 2018.2.2\bin" -Dfile.encoding=UTF-8 -classpath D:\work\install\Jdk1.8\jre\lib\charsets.jar;D:\work\install\Jdk1.8\jre\lib\deploy.jar;D:\work\install\Jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\work\install\Jdk1.8\jre\lib\ext\cldrdata.jar;D:\work\install\Jdk1.8\jre\lib\ext\dnsns.jar;D:\work\install\Jdk1.8\jre\lib\ext\jaccess.jar;D:\work\install\Jdk1.8\jre\lib\ext\jfxrt.jar;D:\work\install\Jdk1.8\jre\lib\ext\localedata.jar;D:\work\install\Jdk1.8\jre\lib\ext\nashorn.jar;D:\work\install\Jdk1.8\jre\lib\ext\sunec.jar;D:\work\install\Jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\work\install\Jdk1.8\jre\lib\ext\sunmscapi.jar;D:\work\install\Jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\work\install\Jdk1.8\jre\lib\ext\zipfs.jar;D:\work\install\Jdk1.8\jre\lib\javaws.jar;D:\work\install\Jdk1.8\jre\lib\jce.jar;D:\work\install\Jdk1.8\jre\lib\jfr.jar;D:\work\install\Jdk1.8\jre\lib\jfxswt.jar;D:\work\install\Jdk1.8\jre\lib\jsse.jar;D:\work\install\Jdk1.8\jre\lib\management-agent.jar;D:\work\install\Jdk1.8\jre\lib\plugin.jar;D:\work\install\Jdk1.8\jre\lib\resources.jar;D:\work\install\Jdk1.8\jre\lib\rt.jar;D:\workspace\autumn\bbq\target\classes;D:\work\install\maven_libs\maven_repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar;D:\work\install\maven_libs\maven_repository\org\springframework\spring-context\5.0.5.RELEASE\spring-context-5.0.5.RELEASE.jar;D:\work\install\maven_libs\maven_repository\org\springframework\spring-aop\5.0.5.RELEASE\spring-aop-5.0.5.RELEASE.jar;D:\work\install\maven_libs\maven_repository\org\springframework\spring-beans\5.0.5.RELEASE\spring-beans-5.0.5.RELEASE.jar;D:\work\install\maven_libs\maven_repository\org\springframework\spring-core\5.0.5.RELEASE\spring-core-5.0.5.RELEASE.jar;D:\work\install\maven_libs\maven_repository\org\springframework\spring-jcl\5.0.5.RELEASE\spring-jcl-5.0.5.RELEASE.jar;D:\work\install\maven_libs\maven_repository\org\springframework\spring-expression\5.0.5.RELEASE\spring-expression-5.0.5.RELEASE.jar;D:\work\install\maven_libs\maven_repository\org\springframework\spring-jdbc\5.0.5.RELEASE\spring-jdbc-5.0.5.RELEASE.jar;D:\work\install\maven_libs\maven_repository\org\springframework\spring-tx\5.0.5.RELEASE\spring-tx-5.0.5.RELEASE.jar;D:\work\install\maven_libs\maven_repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;D:\work\install\maven_libs\maven_repository\org\springframework\spring-test\5.0.5.RELEASE\spring-test-5.0.5.RELEASE.jar;D:\work\install\maven_libs\maven_repository\junit\junit\4.12\junit-4.12.jar;D:\work\install\maven_libs\maven_repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.itheima.Test2
//September 07, 2019 10:14:41 org.springframework.context.support.AbstractApplicationContext prepareRefresh
//Information: Refreshing org.spring framework.context.support.ClassPathXml Application Context@6477463f: startup date [Sat Sep 07 10:14:41 CST 2019]; root of context hierarchy
//September 07, 2019 10:14:41 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
//Information: Loading XML bean definitions from class path resource [application.xml]
//Static block printing result: null
//Method Print result: asdfgh

Process finished with exit code 0

Conclusion: in Spring configuration file injection, the injection results can not be accessed at the injected class member location, and the method is accessed without defect; old fellow 666.
This is related to the bottom of the Spring. His injection mark reference may be marked on the method, or it may be injected into the original variable copy.

Posted by AdamDowning on Sat, 05 Oct 2019 11:47:32 -0700