< 1 > Entity class person
PackageJava_spring.modle;/*** One entity class (person)*/ Public classPerson {Private intuserId; PrivateString UserName; PrivateString Userage; Public intgetUserId () {returnuserId; } Public voidSetuserid (intuserId) { This. UserId =userId; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString getuserage () {returnUserage; } Public voidsetuserage (String userage) { This. Userage =Userage; } @Override PublicString toString () {return"Person [userid= + userId +", username= "+ UserName +", userage= "+ Userage +"] "; }}
< 2 > configuration file spring.xml
<?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.xsd" > <!--Configure a person to power class Personone and assign values through spring - <BeanID= "Personone"class= "Java_spring.modle.Person"> < Propertyname= "UserId"value= "1"></ Property> < Propertyname= "UserName"value= "Zhang San Feng"></ Property> < Propertyname= "Userage"value= "Male"></ Property> </Bean></Beans>
< 3 > Test class Testone
PackageJava_spring.a_main;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportJava_spring.modle.Person; Public classTestone { Public Static voidMain (string[] args) {//Gets the IOC container object for spring, which is the path of the configuration file relative to SRCApplicationContext AC =NewClasspathxmlapplicationcontext ("Java_spring/config/spring.xml"); //gets the configured JavaBean object through the container object, the parameter is the ID in the configuration filePerson Personone = (person) ac.getbean ("Personone"); System.out.println (Personone); }}
Spring Simple configuration and operation (Create entity class, configure XML file, Debug)