如果spring--bean是单例,那么web项目一启动就会加载到内存,如果bean是多例,项目会在刚刚使用的时候,就是走到这个请求地址的时候,类才会加载---spring 默认为单例

来源:互联网 发布:知乎机构号 编辑:程序博客网 时间:2024/05/20 21:21
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * Created by Administrator on 14-4-9.
 *
 * @author leizhimin 14-4-9 上午10:55
 */
@Controller
@RequestMapping("/demo/lsh/ch5")
@Scope("prototype")
public class MultViewController {
    private static int st = 0;      //静态的
    private int index = 0;          //非静态


    static{
    System.out.println("if i apear first, i am single");
    }
    @RequestMapping("/test")
    public String test() {
        System.out.println(st++ + " | " + index++);
        return "/lsh/ch5/test";
    }
}




如果spring--bean是单例,那么web项目一启动就会加载到内存,如果bean是多例,项目会在刚刚使用的时候,就是走到这个请求地址的时候,类才会加载
0 0