java學習

          java學習

           

          spring bean 初始化方法和銷毀方法的配置

          三種初始化方式
          public class Cat {
          @PostConstruct//初始化
          public void afterPropertiesSet() throws Exception {
          System.out.println(" cat init");
          }
          @PreDestroy//銷毀
          public void destroy() throws Exception {
          System.out.println("cat destroy");
          }
          }
          public class User implements InitializingBean,DisposableBean{
          public String toString() {
          return "444";
          }
          @Override//初始化
          public void afterPropertiesSet() throws Exception {
          System.out.println(" user afterPropertiesSet");
          }
          @Override//銷毀
          public void destroy() throws Exception {
          System.out.println("user destroy");
          }
          }
          public class Room {
                  //初始化
          public void afterPropertiesSet() throws Exception {
          System.out.println(" room afterPropertiesSet");
          }
                  //銷毀
          public void destroy() throws Exception {
          System.out.println("room destroy");
          }
          }
          @Configuration
          public class Cfg1 {
          @Bean
          public Cat getCat() {
          return new Cat();
          }
          @Bean
          public User getUser() {
          return new User();
          }
          @Bean(initMethod="afterPropertiesSet",destroyMethod="destroy")
          public Room getRoom() {
          return new Room();
          }
          }

          posted on 2018-03-06 13:56 楊軍威 閱讀(239) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 多伦县| 武山县| 永吉县| 色达县| 芦山县| 奈曼旗| 桐梓县| 高阳县| 武城县| 清涧县| 隆林| 江城| 聂拉木县| 鄂温| 景泰县| 刚察县| 拉萨市| 封丘县| 泰宁县| 会昌县| 蚌埠市| 汪清县| 扎鲁特旗| 汉沽区| 冷水江市| 郑州市| 敦煌市| 红安县| 休宁县| 龙陵县| 神农架林区| 花垣县| 阳原县| 潍坊市| 沐川县| 修文县| 贵南县| 黄平县| 合作市| 湾仔区| 唐山市|