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. 批量搞机(二):分布式ELK平台、Elasticsearch介绍、Elasticsearch集群安装、ES 插件的安装与使用

    一.分布式ELK平台 ELK的介绍: ELK 是什么? Sina.饿了么.携程.华为.美团.freewheel.畅捷通 .新浪微博.大讲台.魅族.IBM...... 这些公司都在使用 ELK!ELK! ...

  2. 给数据库用户授权(对象多为系统表,如dba可以查看的表)

    我们知道,创建一个新用户时,网上各种的帖子包括书籍中经常用到一个grant connect,resource to user;,这样才能用这个用户登录数据库,那么这条语句的真正作用是什么呢? 首先,g ...

  3. 2019 上海网络赛 J stone name (01背包)

    题目:https://nanti.jisuanke.com/t/41420 题意:给你一个集合,然后让你拆成两个集合 x,y    求满足  x>y  &&  x-(x集合中最小 ...

  4. 神坑 Resources.Load 不能实时加载TXT文件

    Resources.Load(fileName) as TextAsset; 这句话并不能实时加载文本文件,对文本文件进行修改之后,若是没有刷新的话,加载的还是之前的文件: 要实时读取文本文件还是要以 ...

  5. wl

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  6. Java控制台输入字符串及字符串比较

    需求描述:茵茵很喜欢研究车牌号码,从车牌号码上可以看出号码注册的早晚,据研究发现,车牌号码是按字典序发放的,现在她收集了很多车牌号码,请你设计程序帮她判断注册较早的号码.车牌号码由5个字母或数字组成. ...

  7. noi.ac #712 练级

    分析 把船当作点 练级当作边 发现一个连通块大于n-1的边的条数的奇偶性影响这个连通块的答案 于是并查集维护即可 代码 #include<bits/stdc++.h> using name ...

  8. 数据可视化-D3js-展示古地理图和古地理坐标反算^_^gplates古地理坐标反算接口

    在线演示 <!DOCTYPE html> <html> <head> <link type="image/png" rel="i ...

  9. jenkins自动化打包报错:gradle: 未找到命令

    shell脚本如下: cd /home/wangju/gitProject/Automation echo "************************开始清理环境********** ...

  10. day40—JavaScript多物体运动框架

    转行学开发,代码100天——2018-04-25 今天继续学习JavaScript的运动实现——多物体运动框架的介绍及其应用. 首先来看一个简单的例子.如下图,要使图中3个红色盒子实现鼠标移入变宽,移 ...