Spring4-three ways to inject values into bean properties

Source: Internet
Author: User

1. Create a MAVEN project, project name Springdemo24,

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8F/1A/wKiom1jTiGfg3qgdAAAVkdb-6N8123.png-wh_500x0-wm_ 3-wmp_4-s_443017751.png "title=" Qq20170323162901.png "alt=" Wkiom1jtigfg3qgdaaavkdb-6n8123.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://s4.51cto.com/wyfs02/M02/8F/18/wKioL1jTiO2RC6fjAABHwYLGVjk964.png-wh_500x0-wm_ 3-wmp_4-s_271414214.png "title=" Qq20170323163510.png "alt=" Wkiol1jtio2rc6fjaabhwylgvjk964.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;} @Overridepublic String toString () {return "{fid=>" +this.fid+ ",name=>" +this.name+ "}";}


5. Create the Spring-bean.xml configuration file under Src/main/resources,

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/8F/1A/wKiom1jTiXDi_MOZAABKWcLigv4074.png-wh_500x0-wm_ 3-wmp_4-s_1828022709.png "title=" Qq20170323163721.png "alt=" Wkiom1jtixdi_mozaabkwcligv4074.png-wh_50 "/>


The contents of the 6.spring-bean.xml configuration file 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 "xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd "><! -- 1. Normal mode  --><bean id= "Forum1"  class= "Com.mycompany.shequ.bean.Forum" >< Property name= "FID"  ><value>1</value></property><property name= "name" ><value> normal way </value></property></bean><!-- 2. Shortcuts  --><bean  Id= "forum2"  class= "Com.mycompany.shequ.bean.Forum" ><property name= "FID"  value= "2" >< /property><property name= "Name"  value= "Shortcut" ></property></bean><!-- 3. P-mode  --><bean id= "forum3"  class= "Com.mycompany.shequ.bean.Forum"  p:fid= "3"  p: Name= "P-mode" ></bean> </beans>


7. Create the Applicationcontext.xml configuration file under Src/main/resources,

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/1A/wKiom1jTidejMhLkAABHc2TZtMg455.png-wh_500x0-wm_ 3-wmp_4-s_3633642776.png "title=" Qq20170323163901.png "alt=" Wkiom1jtidejmhlkaabhc2tztmg455.png-wh_50 "/>


The contents of the 8.applicationcontext.xml configuration file are 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>


9. Create a test file under Src/test/java apptest, package name (com.mycompany.shequ.test)

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8F/1A/wKiom1jTiliwQUy6AABH7s6Q0QM578.png-wh_500x0-wm_ 3-wmp_4-s_3943331522.png "title=" Qq20170323164112.png "alt=" Wkiom1jtiliwquy6aabh7s6q0qm578.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.applicationcontext;import  org.springframework.context.support.classpathxmlapplicationcontext;import com.mycompany.shequ.bean.forum; public class apptest {@Testpublic  void beantest () {     Applicationcontext context = new classpathxmlapplicationcontext ("ApplicationContext.xml"); forum forum1 =  (Forum)  context.getbean ("Forum1"); System.out.println (FORUM1); forum forum2 =  (Forum)  context.getbean ("forum2"); System.out.println (FORUM2); forum forum3 =  (Forum)  context.getbean ("forum3"); System.out.println (FORUM3);}} 


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/M01/8F/1A/wKiom1jTiruzu_hZAAEuE3AFxcs411.png-wh_500x0-wm_ 3-wmp_4-s_4228797757.png "title=" Qq20170323164250.png "alt=" Wkiom1jtiruzu_hzaaeue3afxcs411.png-wh_50 "/>

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

Spring4-three ways to inject values into bean properties

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.