1. Create a MAVEN project, project name Springdemo37,
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8F/24/wKioL1jUyz7AyMyMAAA83B4Yf2M204.png-wh_500x0-wm_ 3-wmp_4-s_4053935944.png "title=" Qq20170324153022.png "alt=" Wkiol1juyz7aymymaaa83b4yf2m204.png-wh_50 "/>
2. Configure Maven, modify the Pom.xml file in the project, modify the content as follows
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ Xsd/maven-4.0.0.xsd "> <modelVersion>1.0.0</modelVersion> <groupId> Shequ</groupid> <artifactid>springdemo13</artifactid> <version> 0.0.1-snapshot</version> <properties> <java.version>1.7 </java.version> <project.build.sourceencoding>utf-8</project.build.sourceencoding > <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <repositories> <repository> <id>codelds</id> <url>https://code.lds.org/nexus/content/groups/main-repo </url> </repository&gT; </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency > <groupid>org.springframework</groupid> <artifactid>spring-core </artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId> Org.springframework</groupid> <artifactid> Spring-context</artifactid> <version>4.1.4.release </version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactId>spring-jdbc</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> < Groupid>mysql</groupid> <artifactid> mysql-connector-java</artifactid> <version>5.1.34 </version> </dependency> </dependencies> <build/></project>
3. Create the entity Bean Forum, package name (Com.mycompany.shequ.bean) under Src/main/java
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8F/26/wKiom1jUy5CxKcT4AABNt7upjrs408.png-wh_500x0-wm_ 3-wmp_4-s_802920479.png "title=" Qq20170324153144.png "alt=" Wkiom1juy5cxkct4aabnt7upjrs408.png-wh_50 "/>
4. Entity Bean Forum content is as follows
Package Com.mycompany.shequ.bean;public class Forum {private int fid;private String name;private int displayorder;public int Getdisplayorder () {return displayorder;} public void Setdisplayorder (int displayorder) {this.displayorder = Displayorder;} public int Getfid () {return FID;} public void Setfid (int fid) {this.fid = FID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic String toString () {return "{fid=>" +this.fid+ ",name=>" +this.name+ ",displayorder=>" + This.displayorder+ "}";}}
5. Create the entity Bean Forumpost under Src/main/java, package name (Com.mycompany.shequ.bean)
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/26/wKiom1jUy9Ph9DHhAABPp0NI7jc315.png-wh_500x0-wm_ 3-wmp_4-s_1715652306.png "title=" Qq20170324153250.png "alt=" Wkiom1juy9ph9dhhaabpp0ni7jc315.png-wh_50 "/>
6. The contents of the entity Bean Forumpost are as follows
Package Com.mycompany.shequ.bean;public class Forumpost {private int pid;private String name;private Forum forum;public I NT Getpid () {return PID;} public void setpid (int pid) {this.pid = pid;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public forum Getforum () {return forum;} public void Setforum (Forum forum) {this.forum = Forum;}}
7. Under Src/main/resource, create the bean profile spring-bean.xml,
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8F/24/wKioL1jUzBihxMV-AABLB2O7jZw530.png-wh_500x0-wm_ 3-wmp_4-s_2930589040.png "title=" Qq20170324153400.png "alt=" Wkiol1juzbihxmv-aablb2o7jzw530.png-wh_50 "/>
8. The contents of the configuration file Spring-bean.xml are as follows
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:p=" http://www.springframework.org/schema/p "xmlns:util=" http://www.springframework.org /schema/util "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/ schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/ Schema/util/spring-util-4.0.xsd "><!-- depends-on=" Forumbean ": You must create the forumbean before you create the Forumpostbean --><bean id= "Forumpostbean" class= "Com.mycompany.shequ.bean.ForumPost" depends-on= " Forumbean "><property name=" pid " value=" 1 "></property><property name=" name " value= "Simple dependency Check" ></property><property name= "forum" ref= "Forumbean" ></ Property></bean><bean id= "Forumbean" class= "Com.mycompany.shequ.bean.Forum" >< Property name= "FID" value= "1" ></property><property name= "name" value= "Dependent bean" ></property></bean></beans >
9. Under Src/main/resource, create the core configuration file Applicationcontext.xml,
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/24/wKioL1jUzFKzrtNyAABGjFNNeZ8105.png-wh_500x0-wm_ 3-wmp_4-s_1586046388.png "title=" Qq20170324153459.png "alt=" Wkiol1juzfkzrtnyaabgjfnnez8105.png-wh_50 "/>
10. The content of the core configuration file Applicationcontext.xml is as follows
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-4.0.xsd "><import resource=" Bean/spring-bean.xml "/> </beans>
11. Create a test file under Src/test/java apptest, package name (com.mycompany.shequ.test)
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/24/wKioL1jUzJ_ig7qtAABIZ2ikeHg125.png-wh_500x0-wm_ 3-wmp_4-s_3829879826.png "title=" Qq20170324153616.png "alt=" Wkiol1juzj_ig7qtaabiz2ikehg125.png-wh_50 "/>
12. The contents of the test file apptest are as follows
Package Com.mycompany.shequ.test;import Org.junit.test;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.mycompany.shequ.bean.forumpost;public class Apptest {@Testpublic void Beantest () {ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Forumpost forumpost = (forumpost) context.getbean ("Forumpostbean"); System.out.println (Forumpost);}}
13. Right-click on the Beantest method of the test class apptest and output the result
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8F/24/wKioL1jUzRCzyPRkAAEOBNx7XdE273.png-wh_500x0-wm_ 3-wmp_4-s_792557571.png "title=" Qq20170324153808.png "alt=" Wkiol1juzrczyprkaaeobnx7xde273.png-wh_50 "/>
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8F/26/wKiom1jUzVrR9YCCAABwCKMO5XE588.png-wh_500x0-wm_ 3-wmp_4-s_1772220833.png "title=" Qq20170324102905.png "alt=" Wkiom1juzvrr9yccaabwckmo5xe588.png-wh_50 "/>
This article from the "Vegetarian Yan" blog, declined to reprint!
Spring4-bean Dependency