1. Create a MAVEN project, project name SPRINGDEMO41,
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/8F/26/wKioL1jU6HCCzzeOAABAztI0By4255.png-wh_500x0-wm_ 3-wmp_4-s_1111367677.png "title=" Qq20170324173450.png "alt=" Wkiol1ju6hcczzeoaabazti0by4255.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/M02/8F/28/wKiom1jU6MLTRYqGAABW1rdSsZk028.png-wh_500x0-wm_ 3-wmp_4-s_1440349380.png "title=" Qq20170324173619.png "alt=" Wkiom1ju6mltryqgaabw1rdsszk028.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/M01/8F/28/wKiom1jU6QeQ8FzJAABV-0JBhdo122.png-wh_500x0-wm_ 3-wmp_4-s_3876647622.png "title=" Qq20170324173726.png "alt=" Wkiom1ju6qeq8fzjaabv-0jbhdo122.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://s3.51cto.com/wyfs02/M02/8F/26/wKioL1jU6T6C9gjiAABWPxUdvek312.png-wh_500x0-wm_ 3-wmp_4-s_4241393899.png "title=" Qq20170324173822.png "alt=" Wkiol1ju6t6c9gjiaabwpxudvek312.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 Javax.annotation.postconstruct;import Javax.annotation.PreDestroy ; Import Com.mycompany.shequ.bean.forum;import Com.mycompany.shequ.service.iforumservice;public class Forumserviceimpl implements Iforumservice {@PreDestroypublic void Destroymethod () {System.out.println (" Destroy-method ");} @PostConstructpublic void Initmethod () {System.out.println ("Init-method");} Public Forum Findforumbyid (int fid) {Forum forum = New Forum (); Forum.setname ("Init-method and Destroy-method"); return forum ;}}
9. Under Src/main/resource, create the bean profile spring-bean.xml,
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/26/wKioL1jU6YGjcogRAABK5h43920113.png-wh_500x0-wm_ 3-wmp_4-s_3297557297.png "title=" Qq20170324173928.png "alt=" Wkiol1ju6ygjcograabk5h43920113.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 "><context:annotation-config/><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://s2.51cto.com/wyfs02/M02/8F/28/wKiom1jU6b2CBq5VAABGVuhKlXY100.png-wh_500x0-wm_ 3-wmp_4-s_2779567432.png "title=" Qq20170324174029.png "alt=" Wkiom1ju6b2cbq5vaabgvuhklxy100.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/M00/8F/28/wKiom1jU6e6gVZmMAABJvUhd2Sk005.png-wh_500x0-wm_ 3-wmp_4-s_551396623.png "title=" Qq20170324174119.png "alt=" Wkiom1ju6e6gvzmmaabjvuhd2sk005.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://s2.51cto.com/wyfs02/M00/8F/26/wKioL1jU6i7glx3BAAE5qXNJeS8256.png-wh_500x0-wm_ 3-wmp_4-s_910278157.png "title=" Qq20170324174222.png "alt=" Wkiol1ju6i7glx3baae5qxnjes8256.png-wh_50 "/>
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/28/wKiom1jU6jiDtf7jAABwCKMO5XE009.png-wh_500x0-wm_ 3-wmp_4-s_2198229371.png "title=" Qq20170324102905.png "alt=" Wkiom1ju6jidtf7jaabwckmo5xe009.png-wh_50 "/>
This article from the "Vegetarian Yan" blog, declined to reprint!
Use of [email protected] and @predestroy annotations