1. Create a MAVEN project, project name Springdemo17,
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8F/15/wKiom1jTN5mwr-2zAAAVZbvPl7I842.png-wh_500x0-wm_ 3-wmp_4-s_3468380499.png "title=" Qq20170323104810.png "alt=" Wkiom1jtn5mwr-2zaaavzbvpl7i842.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://s3.51cto.com/wyfs02/M02/8F/15/wKiom1jTN9LDE70GAABOPiQgl6w454.png-wh_500x0-wm_ 3-wmp_4-s_4082738988.png "title=" Qq20170323104907.png "alt=" Wkiom1jtn9lde70gaabopiqgl6w454.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;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;}}
5. Create the entity Bean Forumpost under Src/main/java, package name (Com.mycompany.shequ.bean)
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8F/13/wKioL1jTOD_gmLVrAABNLQL8DJI524.png-wh_500x0-wm_ 3-wmp_4-s_2655317831.png "title=" Qq20170323105053.png "alt=" Wkiol1jtod_gmlvraabnlql8dji524.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 F Orumpost () {super ();} Public Forumpost (Forum forum) {Super (); this.forum = Forum;} public int 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;}}
7. Create a bean configuration file under Src/main/resources, Profile name Spring-bean.xml,
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8F/13/wKioL1jTOIeSfItcAABHSEt93oo905.png-wh_500x0-wm_ 3-wmp_4-s_4156823277.png "title=" Qq20170323105209.png "alt=" Wkiol1jtoiesfitcaabhset93oo905.png-wh_50 "/>
The contents of the 8.spring-bean.xml file are as follows
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "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 "><!--automatically assemble beans by property by the data type of the constructor parameter--<bean Id= "Forumpost" class= "Com.mycompany.shequ.bean.ForumPost" autowire= "constructor" > </bean> <bean I d= "Forum" class= "Com.mycompany.shequ.bean.Forum" > <property name= "FID" value= "3" ></property> < Property name= "Name" value= "Big account" ></property> </bean></beans>
9. Under Src/test/java, create the test class Forumposttest, package name (Com.mycompany.shequ.bean),
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8F/15/wKiom1jTOPCAzwjyAABKA6Y1lGE580.png-wh_500x0-wm_ 3-wmp_4-s_1095141705.png "title=" Qq20170323105353.png "alt=" Wkiom1jtopcazwjyaabka6y1lge580.png-wh_50 "/>
10. The contents of the test class forumposttest are as follows
Package Com.mycompany.shequ.bean;import Org.junit.test;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;public class Forumposttest {/** * spring Automatic assembly beans, the bean is automatically assembled by attributes by the data type of the constructor parameter */@Testpublic void Constructortest () {ApplicationContext context = new CLASSPATHXM Lapplicationcontext ("/bean/spring-bean.xml"); Forumpost forumpost = (forumpost) context.getbean ("Forumpost"); System.out.println (Forumpost.getforum (). GetName ());}}
11. Right-click on the Bytypetest method of the test class forumposttest and output the result
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8F/13/wKioL1jTOTex1P4eAAEAfHkkKUU149.png-wh_500x0-wm_ 3-wmp_4-s_3585018224.png "title=" Qq20170323105504.png "alt=" Wkiol1jtotex1p4eaaeafhkkkuu149.png-wh_50 "/>
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1909519
spring4-Automatic assembly beans-automatically assemble beans by property by the data type of the constructor parameter