Packagecn.com.xf; Public classUser {PrivateString name; Private intAge ; PrivateString Remark; //Omit Setter/getter Method@Override PublicString toString () {return"User [name=] + name +", age= "+ Age +", remark= "+Remark+ "]"; }}
Packagecn.com.xf; Public classUserutil {Privateuser User; PublicUser GetUser () {returnuser; } //Note that this construction method PublicUserutil (User u) { This. user=u; } Public BooleanIsUser () {if(User! =NULL) { return true; } Else { return false; } }}
The following is the spring configuration file
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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-2.0.xsd"> <BeanID= "User"class= "Cn.com.xf.User"> < Propertyname= "Name"value= "Jikoy"></ Property> < Propertyname= "Age"value= "+"></ Property> < Propertyname= "Remark"value= "This is remark"></ Property> </Bean> <BeanID= "Userutil"class= "Cn.com.xf.UserUtil"> <Constructor-argref= "User"></Constructor-arg> </Bean></Beans>
Above, I was not the same as the previous one, did not use the P tag, but the effect is the same, note <constructor-arg ref= "user" ></constructor-arg>
Package Cn.com.xf;import Org.springframework.context.configurableapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test {public static void main ( String[] args) { configurableapplicationcontext ctx=new classpathxmlapplicationcontext ("Applicationcontext.xml "); User user= (user) Ctx.getbean ("user"); Ctx.close (); SYSTEM.OUT.PRINTLN (user);} }
The test class is still a sub-interface method that uses ApplicationContext and provides a close method.
Spring Series "3" Application of constructor injection method for Bean injection