1. Create a MAVEN project, project name Springdemo16,
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8F/12/wKioL1jTM5TQRwIZAAAVVs18AUg447.png-wh_500x0-wm_ 3-wmp_4-s_2174713354.png "title=" Qq20170323103102.png "alt=" Wkiol1jtm5tqrwizaaavvs18aug447.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://s1.51cto.com/wyfs02/M01/8F/12/wKioL1jTM8zQScZSAABOEikSa0o153.png-wh_500x0-wm_ 3-wmp_4-s_2486797535.png "title=" Qq20170323103155.png "alt=" Wkiol1jtm8zqsczsaaboeiksa0o153.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://s2.51cto.com/wyfs02/M02/8F/13/wKioL1jTNAGjKjSYAABM2q5g-aE399.png-wh_500x0-wm_ 3-wmp_4-s_1482399535.png "title=" Qq20170323103250.png "alt=" Wkiol1jtnagjkjsyaabm2q5g-ae399.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. Create a bean configuration file under Src/main/resources, Profile name Spring-bean.xml,
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/8F/13/wKioL1jTNDHBc8IZAABGqZVy3T0745.png-wh_500x0-wm_ 3-wmp_4-s_181751708.png "title=" Qq20170323103338.png "alt=" Wkiol1jtndhbc8izaabgqzvy3t0745.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--<bean id= by attribute data type Forumpost "class=" Com.mycompany.shequ.bean.ForumPost "autowire=" Bytype "> </bean> <bean id=" forum " class= "Com.mycompany.shequ.bean.Forum" > <property name= "FID" value= "3" ></property> <property name = "Name" value= "Seedlings" ></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://s5.51cto.com/wyfs02/M01/8F/13/wKioL1jTNHuzIxRNAABJCICMRic871.png-wh_500x0-wm_ 3-wmp_4-s_920071379.png "title=" Qq20170323103451.png "alt=" Wkiol1jtnhuzixrnaabjcicmric871.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, automatic assembly of beans by attribute data type */@Testpublic void Bytypetest () {ApplicationContext context = new Classpathxmlapplicati Oncontext ("/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://s5.51cto.com/wyfs02/M02/8F/13/wKioL1jTNN2A6Yq3AAD-N548SBQ729.png-wh_500x0-wm_ 3-wmp_4-s_3783410783.png "title=" Qq20170323103630.png "alt=" Wkiol1jtnn2a6yq3aad-n548sbq729.png-wh_50 "/>
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1909501
spring4-Automatic assembly beans-automatically assemble beans by attribute data type