package cn.com.xf; public class User { private String name; private int age; private String remark; //Getter/setter method of omitting properties
@Override public
String toString () { return "User [n Ame= "+ name +", age= "+ Age +", remark= "+ remark +"] ";
Packagecn.com.xf; Public classUserutil {Privateuser User; PublicUser GetUser () {returnuser; } Public voidsetUser (user user) { This. user =user; } Public BooleanIsUser () {if(User! =NULL) { return true; } Else { return false; } }}
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
<!--Note that the following P-tags are used--xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <BeanID= "User"class= "Cn.com.xf.User"P:name= "Jikoy"P:age= "+"P:remark= "This is remark"></Bean> <BeanID= "Userutil"class= "Cn.com.xf.UserUtil"> < Propertyname= "User"ref= "User"></ Property> </Bean></Beans>
The following is a test class: All code pro-test pass.
Packagecn.com.xf;ImportOrg.springframework.context.ConfigurableApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTest { Public Static voidMain (string[] args) {
The ApplicationContext interface does not provide a shutdown method, and Eclipse prompts it to change its sub-interface configurableapplicationcontext ctx=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); User User= (user) Ctx.getbean ("user")); Ctx.close (); SYSTEM.OUT.PRINTLN (user); }}
Spring Series "2" Application setter injection to implement bean injection