Writing User.java
1 Packagecom.example.spring;2 3 Public classUser {4 PrivateString name;5 PrivateInteger age;6 PrivateString Country;7 8 PublicUser (string name, Integer age, String country) {9 This. Name =name;Ten This. Age =Age ; One This. Country =Country; A } - - PublicString toString () { the returnName + ' is ' + age + ' years old ', living in ' +Country; - } -}
Writing Beans.xml
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">5 6 <BeanID= "User"class= "Com.example.spring.User" >7 <Constructor-argname= "Name"type= "Java.lang.String"value= "Jacky" />8 <Constructor-argname= "Age"type= "Java.lang.Integer"value= "All"/>9 <Constructor-argname= "Country"type= "Java.lang.String"value= "China"/>Ten </Bean> One A </Beans>
Writing Application.java
Packagecom.example.spring;ImportOrg.springframework.context.support.AbstractApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classApplication { Public Static voidMain (string[] args) {//Where the bean configuration file is located d:\\ideaprojects\\spring\\src\\beans.xml//using the Abstractapplicationcontext containerAbstractapplicationcontext context =NewClasspathxmlapplicationcontext ("File:d:\\ideaprojects\\spring\\src\\beans.xml"); User User= (user) Context.getbean ("user")); SYSTEM.OUT.PRINTLN (user); }}
Run output
Jacky is years-old, living in China
Java Framework Spring Boot learning Note (vii): constructor-based Dependency injection