Spring4-Init-method 與 destroy-method使用

來源:互聯網
上載者:User

標籤:spring4-init-method 與 destroy-method使用

1.建立Maven項目,項目名稱springdemo41,

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M00/8F/25/wKioL1jU3qyjQnbWAAA96spf4Tw720.png-wh_500x0-wm_3-wmp_4-s_4119511422.png" title="QQ20170324165317.png" alt="wKioL1jU3qyjQnbWAAA96spf4Tw720.png-wh_50" />


2.配置Maven,修改項目中的pom.xml檔案,修改內容如下
<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>  </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.在src/main/java下建立實體Bean Forum,包名(com.mycompany.shequ.bean)

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M01/8F/27/wKiom1jU3v3DaH43AABTgdZ6qGg183.png-wh_500x0-wm_3-wmp_4-s_499793351.png" title="QQ20170324165439.png" alt="wKiom1jU3v3DaH43AABTgdZ6qGg183.png-wh_50" />


4.實體Bean Forum的內容如下
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.在src/main/java下建立介面IForumService,包名(com.mycompany.shequ.service)

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/8F/25/wKioL1jU3y-ig_vPAABVC1i82DU733.png-wh_500x0-wm_3-wmp_4-s_2131228260.png" title="QQ20170324165528.png" alt="wKioL1jU3y-ig_vPAABVC1i82DU733.png-wh_50" />


6.介面IForumService的內容如下
package com.mycompany.shequ.service;import com.mycompany.shequ.bean.Forum;public interface IForumService {public Forum findForumById(int fid);}


7.在src/main/java下建立介面IForumService的實作類別ForumServiceImpl,包名(com.mycompany.shequ.service.impl)

650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/8F/25/wKioL1jU32iAeHbuAABVyRefsC8745.png-wh_500x0-wm_3-wmp_4-s_811402489.png" title="QQ20170324165621.png" alt="wKioL1jU32iAeHbuAABVyRefsC8745.png-wh_50" />


8.介面IForumService的實作類別ForumServiceImpl的內容如下
package com.mycompany.shequ.service.impl;import com.mycompany.shequ.bean.Forum;import com.mycompany.shequ.service.IForumService;public class ForumServiceImpl implements IForumService {public void destroyMethod(){System.out.println("destroy-method");}public void initMethod(){System.out.println("init-method");}public Forum findForumById(int fid) {Forum forum = new Forum();forum.setName("init-method 與 destroy-method");return forum;}}


9.在src/main/resource下建立bean的設定檔spring-bean.xml,

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/8F/25/wKioL1jU36mgjyn7AABKemBiI5M563.png-wh_500x0-wm_3-wmp_4-s_3915049817.png" title="QQ20170324165727.png" alt="wKioL1jU36mgjyn7AABKemBiI5M563.png-wh_50" />


10.設定檔spring-bean.xml的內容如下
<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" init-method="initMethod" destroy-method="destroyMethod"></bean></beans>


11.在src/main/resource下建立核心的設定檔applicationContext.xml,

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/8F/28/wKiom1jU3-DyWSe1AABHR8R7A4I838.png-wh_500x0-wm_3-wmp_4-s_2080233044.png" title="QQ20170324165825.png" alt="wKiom1jU3-DyWSe1AABHR8R7A4I838.png-wh_50" />


12.核心的設定檔applicationContext.xml的內容如下
<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.在src/test/java下建立測試檔案AppTest,包名(com.mycompany.shequ.test)

650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/8F/28/wKiom1jU4CfC6rYFAABJIowxjGY342.png-wh_500x0-wm_3-wmp_4-s_3768298917.png" title="QQ20170324165935.png" alt="wKiom1jU4CfC6rYFAABJIowxjGY342.png-wh_50" />


14.測試檔案AppTest的內容如下
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");IForumService forumService = (IForumService)context.getBean("forumservicebean");System.out.println(forumService.findForumById(1));context.close();}}


15.在測試類別AppTest的beanTest方法上右鍵運行,輸出結果

650) this.width=650;" src="https://s2.51cto.com/wyfs02/M00/8F/28/wKiom1jU4GCQY8fpAAETzqW0YT8751.png-wh_500x0-wm_3-wmp_4-s_618559589.png" title="QQ20170324170031.png" alt="wKiom1jU4GCQY8fpAAETzqW0YT8751.png-wh_50" />

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/8F/26/wKioL1jU4GuClTbkAABwCKMO5XE241.png-wh_500x0-wm_3-wmp_4-s_485565280.png" title="QQ20170324102905.png" alt="wKioL1jU4GuClTbkAABwCKMO5XE241.png-wh_50" />

本文出自 “素顏” 部落格,謝絕轉載!

Spring4-Init-method 與 destroy-method使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.