zoukankan      html  css  js  c++  java
  • 为对象设计作用域,设置延迟加载,设置生命周期方法(了解)

     1 package com.cy.pj.common.cache;
     2 @Component
     3 @Scope("singleton")
     4 @Lazy        
     5 public class DefaultCache {
     6         public DefaultCache() {
     7            System.out.println("cache()");
     8         }
     9         
    10         @PostConstruct
    11         public void init() {
    12                 System.out.println("init()");
    13         }
    14 
    15         @PreDestroy
    16         public void destory() {
    17                 System.out.println("destory");
    18         }
    19 }
    • @Scope 是Spring中用于定义Bean对象作用域的一个注解,其常用的值有singleton(整个内存有一份Bean实例,此实例何时创建与类的延迟加载特性配置有关,此实例创建以后,生命周期会由spring框架管理),prototype(每次获取都会创建新实例,此实例会在需要时创建与lazy特性无关,这个实例创建以后,不会交给spring管理,spring可以对其初始化,但不负责销毁。)等。

    • @Lazy注解用于描述类,其目的是告诉spring框架此类支持延迟加载,通常会配合单例作用域使用。

    • @PostConstruct 注解用于描述bean对象生命周期方法中的初始化方法,此方法会在对象的构造方法之后执行。

    • @PreDestroy 注解用于描述Bean对象生命周期方法中的销毁方法,此方法会在对象销毁之前执行(当作用域为prototype时,此方法不会执行)

  • 相关阅读:
    HDU 2586 How far away?
    UVAlive 5796 Hedge Mazes
    HDU 4975 A simple Gaussian elimination problem.
    Poj 1149 PIGS
    HDU 3416 Marriage Match IV
    HDU 4912 Paths on the tree
    HDU 3277 Marriage Match III
    終於記起了帳號密碼
    codeforces194a
    codeforces195c
  • 原文地址:https://www.cnblogs.com/Zr0118/p/13778092.html
Copyright © 2011-2022 走看看