1、@Scope

      1.1、描述了Spring容器如何新建Bean的实例;

      1.2、@Scope(value="")  value值有:

          1.2.1、singleton

                一个Spring容器只创建一个Bean实例,也是Spring默认的

                单例,普通成员变量、类成员变量都被共享;

package com.an.controller;

import com.an.service.MyFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/14 20:29
* @since:
*/
@RestController
@Scope(value = "singleton")
public class MyFieldController { @Autowired
private MyFieldService myFieldService;
private Integer count=0;
private static Integer staticCount=0; @RequestMapping(value = "/testMyField",method = RequestMethod.GET)
public String testMyField(){
System.out.println("count:"+(count++)+"<++++++++++++>"+"staticCount:"+(staticCount++));
return myFieldService.testMyField();
}
} 结果:
count:0<++++++++++++>staticCount:0
bookName:null
count:1<++++++++++++>staticCount:1
bookName:null
count:2<++++++++++++>staticCount:2
bookName:null

  

          1.2.2、prototype

                每次调用都会创建一个Bean实例;

                多例,普通成员变量不会被共享,类成员变量会共享;

package com.an.controller;

import com.an.service.MyFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/14 20:29
* @since:
*/
@RestController
@Scope(value = "prototype")
public class MyFieldController { @Autowired
private MyFieldService myFieldService;
private Integer count=0;
private static Integer staticCount=0; @RequestMapping(value = "/testMyField",method = RequestMethod.GET)
public String testMyField(){
System.out.println("count:"+(count++)+"<++++++++++++>"+"staticCount:"+(staticCount++));
return myFieldService.testMyField();
}
}

  结果:

count:0<++++++++++++>staticCount:0
bookName:null
count:0<++++++++++++>staticCount:1
bookName:null
count:0<++++++++++++>staticCount:2
bookName:null
count:0<++++++++++++>staticCount:3
bookName:null

          1.2.3、request

                web项目中,为每一个HTTP request 创建一个实例;

          1.2.4、session

                web项目中,为每一个session创建一个实例;

2、Spring EL和资源调用

      2.1、Spring EL表达式语言,支持在XML、注解中使用表达式;

            开发中,经常涉及调用各种资源的情况,包含普通文件、网址、配置文件、系统环境变量等;

            可以使用Spring的表达式语言  实现资源的注入;

      2.2、Spring主要 在注解@Value的参数中  使用表达式;

Spring---基础配置的更多相关文章

  1. Spring基础配置

    从毕业到现在我一直从事Android开发,但是对JavaEE一直念念不忘,毕业校招的时候,一个礼拜拿了三个offer,岗位分别是Android.JavaEE和JavaSE,后来觉得Android比较简 ...

  2. 01—Spring基础配置IOC

  3. SpringMVC基础配置(通过注解配置,非xml配置)

    SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...

  4. Spring Boot实战(1) Spring基础

    1. Spring基础配置 Spring框架本身有四大原则: 1) 使用POJO进行轻量级和最小侵入式开发 2) 通过依赖注入和基于接口编程实现松耦合 3) 通过AOP和默认习惯进行声明式编程 4) ...

  5. Spring Boot学习第一部分(Spring 4.x)第一章(Spring 基础)

    1.spring概述 1.1.spring的简史 第一阶段:XML配置spring 1.x时代, 第二阶段:注解配置spring 2.x时代, @Controller @Service @Compon ...

  6. 转 RabbitMQ 基础概念及 Spring 的配置和使用 推荐好文 举例讲解

    从不知道到了解—RabbitMQ 基础概念及 Spring 的配置和使用 原理同上 请求地址:http://localhost:8080/home?type=3&routing_key=myO ...

  7. Spring Boot 基础配置

    之前简单接触了一些Spring Boot ,并且写了一个简单的 Demo .本文就来简单学习一下 Spring Boot 的基础配置. 一.Spring Boot 项目入口 上文中有写到,Spring ...

  8. Java进阶知识15 Spring的基础配置详解

    1.SSH各个的职责 Struts2:是web框架(管理jsp.action.actionform等).Hibernate:是ORM框架,处于持久层.Spring:是一个容器框架,用于配置bean,并 ...

  9. Spring基础——在Spring Config 文件中配置 Bean

    一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...

  10. Spring基础篇——通过Java注解和XML配置装配bean

    自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...

随机推荐

  1. hdu 5511 Minimum Cut-Cut——分类讨论思想+线段树合并

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5511 题意:割一些边使得无向图变成不连通的,并且恰好割了两条给定生成树上的边.满足非树边两段一定在给定生成 ...

  2. C++ 拷贝构造函数与赋值函数的区别(很严谨和全面)

    这里我们用类String 来介绍这两个函数: 拷贝构造函数是一种特殊构造函数,具有单个形参,该形参(常用const修饰)是对该类类型的引用.当定义一个新对象并用一个同类型的对象对它进行初始化时,将显式 ...

  3. CSU 1556 Jerry's trouble

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1556 Description Jerry is caught by Tom. He ...

  4. HttpSessionBindingListener和HttpSessionAttributeListener区别

    HttpSessionBindingListener和HttpSessionAttributeListener是两个经常让初学者弄混的监听器,其实它们有很大的区别.这2个监听器在文章中简称为Bindi ...

  5. ElasticSearch删除索引

    curl -X DELETE http://{ES IP address}:9200/{index_name}

  6. CentOS7 - 安装 VirtualBox

    参考资料 最新的可用安装包可以从这里下载 VirtualBox 是 x86 硬件虚拟化产品,功能上与 VMware Server.KVM.及 Xen 类似,但是 VirtualBox 不修改 Linu ...

  7. 简单入门爬斗鱼颜值区妹子照片 v1.1

    这是个比较简单的入门爬虫.基于python3. urllib,urllib2,python3中用urllib.request代替,使用方法基本一致. #python3 import urllib.re ...

  8. 洛谷P2786 英语1(eng1)- 英语作文——map

    给一手链接 https://www.luogu.com.cn/problem/P2786 拿这道题当map模板练练手qwq #include<cstdio> #include<cst ...

  9. Java相关面试题总结+答案(四)

    [反射] 57. 什么是反射? 反射是在运行状态中,对于任意一个类,都能够知道该类的所有属性和方法,对于任意一个对象,都能够获得该对象的任一属性和方法:这种动态获取信息以及动态调用对象的方法的功能称之 ...

  10. [已解决]报错: Error response from daemon: conflict

    报错内容: Error response from daemon: conflict: unable to delete f5b6ef70d79b (must be forced) - image i ...