spring4-through beans. property or bean. Method invocation

Source: Internet
Author: User

1. Create a MAVEN project, project name Springdemo45,

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8F/29/wKioL1jVzzmBfzhmAAA-pppB4BA938.png-wh_500x0-wm_ 3-wmp_4-s_4269836267.png "title=" Qq20170325095937.png "alt=" Wkiol1jvzzmbfzhmaaa-pppb4ba938.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>javax.annotation</groupid>  <artifactid>jsr250-api</ artifactid>  <version>1.0</version>  </dependency>     <dependency>  <groupId>org.springframework</groupId>  < artifactid>spring-test</artifactid>  <version>4.1.4.release</version>   </dependency>    <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/M00/8F/2B/wKiom1jVz4OTeQw5AABK5Bq7oSQ533.png-wh_500x0-wm_ 3-wmp_4-s_2483638846.png "title=" Qq20170325100052.png "alt=" Wkiom1jvz4oteqw5aabk5bq7osq533.png-wh_50 "/>


4. Entity Bean Forum content is as follows
Package com.mycompany.shequ.bean;import org.springframework.beans.factory.annotation.value;import  org.springframework.stereotype.Component; @Component ("Forumbean") public class forum { Private int fid;private string name;private int displayorder;public int  getdisplayorder ()  {return displayorder;} @Value ("1") Public void setdisplayorder (Int displayorder)  {this.displayOrder =  Displayorder;} Public int getfid ()  {return fid;} @Value ("1") Public void setfid (Int fid)  {this.fid = fid;} Public string getname ()  {return name;} @Value ("Spring el in Annotated Form") Public void setname (String name)  {this.name = name;} @Overridepublic  string tostring ()  {return  "{fid=>" +this.fid+ ",name=>" +this.name+ ", Displayorder=> "+this.displayorder+"} ";} Public string getaddress (string  address) {return address;}} 


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/2B/wKiom1jVz9GxdkgzAABLqSjHfq8367.png-wh_500x0-wm_ 3-wmp_4-s_642171319.png "title=" Qq20170325100205.png "alt=" Wkiom1jvz9gxdkgzaablqsjhfq8367.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.value;import  org.springframework.stereotype.Component; @Component ("Forumpostbean") public class forumpost  {private int pid;private String name;private Forum forum;private  String address;public string getaddress ()  {return address;} @Value ("#{forumbean.getaddress (' Jiangsu ')}")//through. Call the GetAddress method in Forumbean public void setaddress ( string address)  {this.address = address;} Public int getpid ()  {return pid;} @Value ("1") Public void setpid (Int pid)  {this.pid = pid;} Public string getname ()  {return name;} @Value ("#{forumbean.name}")//through. Call Forumbean in the Name property Public void setname (string name)  { This.name = name;} Public forum getforum ()  {return forum;} @Value ("#{forumbean}") PUBLIC&NBSP;VOID&NBsp;setforum (Forum forum)  {this.forum = forum;}} 


7. Under Src/main/resource, create the core configuration file Applicationcontext.xml,

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/2B/wKiom1jV0BPAYv5bAABCpwnap7w282.png-wh_500x0-wm_ 3-wmp_4-s_933427470.png "title=" Qq20170325100314.png "alt=" Wkiom1jv0bpayv5baabcpwnap7w282.png-wh_50 "/>


8. 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 "xmlns:p=" http://www.springframework.org/schema/p "xmlns:context="/HTTP/ Www.springframework.org/schema/context "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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/ Spring-context-4.0.xsd "><context:component-scan base-package=" Com.mycompany.shequ.bean "/></beans >



9. 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/M01/8F/29/wKioL1jV0EPwBzoFAABKdgjw_uQ620.png-wh_500x0-wm_ 3-wmp_4-s_1345552438.png "title=" Qq20170325100403.png "alt=" Wkiol1jv0epwbzofaabkdgjw_uq620.png-wh_50 "/>


10. The contents of the test file apptest are as follows
Package Com.mycompany.shequ.test;import Org.junit.test;import Org.springframework.context.configurableapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.mycompany.shequ.bean.ForumPost; public class Apptest {@Testpublic void Beantest () {Configurableapplicationcontext context = new Classpathxmlapplica Tioncontext ("Applicationcontext.xml"); Forumpost forumpost = (forumpost) context.getbean ("Forumpostbean"); System.out.println (Forumpost.getname ()); System.out.println (Forumpost.getaddress ()); System.out.println (Forumpost.getforum ());}}


11. Right-click on the Beantest method of the test class apptest and output the result

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8F/2B/wKiom1jV0ITRJMn9AAEV-lA3dw0058.png-wh_500x0-wm_ 3-wmp_4-s_2499599993.png "title=" Qq20170325100507.png "alt=" Wkiom1jv0itrjmn9aaev-la3dw0058.png-wh_50 "/>

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8F/29/wKioL1jV0I6Ss_SzAABwCKMO5XE032.png-wh_500x0-wm_ 3-wmp_4-s_972209315.png "title=" Qq20170324102905.png "alt=" Wkiol1jv0i6ss_szaabwckmo5xe032.png-wh_50 "/>

This article from the "Vegetarian Yan" blog, declined to reprint!

spring4-through beans. property or bean. Method invocation

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.