1. Create a MAVEN project, project name Springdemo40,
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8F/25/wKioL1jU2kDxtGNUAABA7ZIUBPk908.png-wh_500x0-wm_ 3-wmp_4-s_3668145047.png "title=" Qq20170324163408.png "alt=" Wkiol1ju2kdxtgnuaaba7ziubpk908.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/M00/8F/25/wKioL1jU2oXiBlcYAABV3xP3y1E344.png-wh_500x0-wm_ 3-wmp_4-s_2142749871.png "title=" Qq20170324163534.png "alt=" Wkiol1ju2oxiblcyaabv3xp3y1e344.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;private int displayorder;public int Getdisplayorder () {return displayorder;} public void Setdisplayorder (int displayorder) {this.displayorder = Displayorder;} 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+ ",displayorder=>" + This.displayorder+ "}";}}
5. Create the interface Iforumservice under Src/main/java, package name (Com.mycompany.shequ.service)
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/25/wKioL1jU2vbDIuITAABUiSDZlDk716.png-wh_500x0-wm_ 3-wmp_4-s_3757941399.png "title=" Qq20170324163727.png "alt=" Wkiol1ju2vbdiuitaabuisdzldk716.png-wh_50 "/>
6. The contents of the interface Iforumservice are as follows
Package Com.mycompany.shequ.service;import Com.mycompany.shequ.bean.forum;public interface Iforumservice {public Forum Findforumbyid (int fid);}
7. Create the implementation class Forumserviceimpl for the interface Iforumservice under Src/main/java, package name (COM.MYCOMPANY.SHEQU.SERVICE.IMPL)
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8F/27/wKiom1jU21qBVESdAABT42sl9us542.png-wh_500x0-wm_ 3-wmp_4-s_636183291.png "title=" Qq20170324163908.png "alt=" Wkiom1ju21qbvesdaabt42sl9us542.png-wh_50 "/>
8. The contents of the implementation class Forumserviceimpl of the interface Iforumservice are as follows
Package Com.mycompany.shequ.service.impl;import Org.springframework.beans.factory.disposablebean;import Org.springframework.beans.factory.initializingbean;import Com.mycompany.shequ.bean.forum;import Com.mycompany.shequ.service.iforumservice;public class Forumserviceimpl implements Iforumservice, InitializingBean, Disposablebean {public void Destroy () throws Exception {System.out.println ("destroy is called");} public void Afterpropertiesset () throws Exception {System.out.println ("Afterpropertiesset is called");} Public Forum Findforumbyid (int fid) {Forum forum = New Forum (); Forum.setname ("Initializingbean and Disposablebean"); return Forum;}}
9. Under Src/main/resource, create the bean profile spring-bean.xml,
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M02/8F/27/wKiom1jU267QGeQ9AABKPUB-dEA433.png-wh_500x0-wm_ 3-wmp_4-s_4069058148.png "title=" Qq20170324164030.png "alt=" Wkiom1ju267qgeq9aabkpub-dea433.png-wh_50 "/>
10. The contents of the configuration file Spring-bean.xml 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 "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 "><bean id=" Forumservicebean " class=" Com.mycompany.shequ.service.impl.ForumServiceImpl "></BEAN></BEANS>
11. Under Src/main/resource, create the core configuration file Applicationcontext.xml,
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/25/wKioL1jU2_yxP71AAABHfN-wFPU808.png-wh_500x0-wm_ 3-wmp_4-s_1221928260.png "title=" Qq20170324164149.png "alt=" Wkiol1ju2_yxp71aaabhfn-wfpu808.png-wh_50 "/>
12. 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 "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>
13. Create a test file under Src/test/java apptest, package name (com.mycompany.shequ.test)
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8F/27/wKiom1jU3DPwfq9nAABJPTqrNcc707.png-wh_500x0-wm_ 3-wmp_4-s_1189971378.png "title=" Qq20170324164244.png "alt=" Wkiom1ju3dpwfq9naabjptqrncc707.png-wh_50 "/>
14. 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.service.iforumservice;public class Apptest {@Testpublic void Beantest () { Configurableapplicationcontext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml"); Forumservice Forumservice = (iforumservice) context.getbean ("Forumservicebean"); System.out.println (Forumservice.findforumbyid (1)); Context.close ();}}
15. Right-click on the Beantest method of the test class apptest and output the result
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8F/25/wKioL1jU3IHz0HgmAAE7tCvk344786.png-wh_500x0-wm_ 3-wmp_4-s_2242171050.png "title=" Qq20170324164402.png "alt=" Wkiol1ju3ihz0hgmaae7tcvk344786.png-wh_50 "/>
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8F/25/wKioL1jU3IqjWtztAABwCKMO5XE108.png-wh_500x0-wm_ 3-wmp_4-s_2892195291.png "title=" Qq20170324102905.png "alt=" Wkiol1ju3iqjwtztaabwckmo5xe108.png-wh_50 "/>
This article from the "Vegetarian Yan" blog, declined to reprint!
Spring4-initializingbean and Disposablebean use