spring4-Automatic assembly beans-by annotation @autowired on the construction method

Source: Internet
Author: User

1. Create a MAVEN project, project name Springdemo19,

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8F/16/wKiom1jTQyCxZWieAAAUjNTMMzM679.png-wh_500x0-wm_ 3-wmp_4-s_1173999806.png "title=" Qq20170323113718.png "alt=" Wkiom1jtqycxzwieaaaujntmmzm679.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/M02/8F/14/wKioL1jTQ4KTMep-AABNONnzWi4782.png-wh_500x0-wm_ 3-wmp_4-s_2942975307.png "title=" Qq20170323113900.png "alt=" Wkiol1jtq4ktmep-aabnonnzwi4782.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/14/wKioL1jTQ7WyNzTBAABOjOZjDc4388.png-wh_500x0-wm_ 3-wmp_4-s_2617282909.png "title=" Qq20170323113949.png "alt=" Wkiol1jtq7wynztbaabojozjdc4388.png-wh_50 "/>


6. The contents of the entity Bean Forumpost are as follows
Package Com.mycompany.shequ.bean;import Org.springframework.beans.factory.annotation.autowired;public Class forumpost {private int pid;private String name;private Forum forum;public forumpost () {super ();} @Autowiredpublic 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;} 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://s4.51cto.com/wyfs02/M02/8F/14/wKioL1jTQ_qhGa7jAABMVuemUic392.png-wh_500x0-wm_ 3-wmp_4-s_194882464.png "title=" Qq20170323114100.png "alt=" Wkiol1jtq_qhga7jaabmvuemuic392.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 "> <bean id=" forumpost "class=" com.mycompany.sh  Equ.bean.ForumPost "> </bean> <bean id=" forum "class=" Com.mycompany.shequ.bean.Forum "> <property Name= "FID" value= "3" ></property> <property name= "name" value= "@Autowired on construction method" ></property> &L T;/bean></beans>


9. Create the Spring core profile applicationcontext.xml under Src/main/resources,

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8F/14/wKioL1jTRDjRcx2RAABJeQ61eMI624.png-wh_500x0-wm_ 3-wmp_4-s_3370643329.png "title=" Qq20170323114202.png "alt=" Wkiol1jtrdjrcx2raabjeq61emi624.png-wh_50 "/>


The contents of the 10.spring core configuration file Applicationcontext.xml 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" xmlns:context= "Http://www.springframework.org/schema/context" Xsi:schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /spring-context-4.0.xsd "> <context:annotation-config/><import resource=" Bean/spring-bean.xml "/>&L T;/beans>


11. Under Src/test/java, create the test class Forumposttest, package name (Com.mycompany.shequ.bean),

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8F/17/wKiom1jTRHOiWK6aAABKLf2di2Y842.png-wh_500x0-wm_ 3-wmp_4-s_3865740664.png "title=" Qq20170323114301.png "alt=" Wkiom1jtrhoiwk6aaabklf2di2y842.png-wh_50 "/>


12. 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 The automatic assembly beans, via annotations @autowired on the construction method */@Testpublic void Autowiredtest () {ApplicationContext context = new Classpathxmlappli    Cationcontext ("Applicationcontext.xml");    Forumpost forumpost = (forumpost) context.getbean ("Forumpost"); System.out.println (Forumpost.getforum (). GetName ());}}


13. 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/17/wKiom1jTRMbgvN9fAAEPP5PjDKU437.png-wh_500x0-wm_ 3-wmp_4-s_1066667912.png "title=" Qq20170323114421.png "alt=" Wkiom1jtrmbgvn9faaepp5pjdku437.png-wh_50 "/>

This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1909539

spring4-Automatic assembly beans-by annotation @autowired on the construction method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.