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. Java项目案例之---定时器的使用

    定时器的使用 使用定时器,在当前时间的10秒后调用方法,输出语句 import java.text.SimpleDateFormat; import java.util.Calendar; impor ...

  2. AT3576 E Popping Balls——计数思路

    题目:https://code-festival-2017-qualb.contest.atcoder.jp/tasks/code_festival_2017_qualb_e 题解:https://w ...

  3. postgresql主从同步配置

    前言 不久前,公司的一台物理机器硬件坏了,导致运行在其上的虚拟机都挂了.很不凑巧的是,我负责的那台虚拟机的系统盘坏了(ps:感觉老天在玩我),导致里面的数据永远的离我而去(ps:当时我的内心是崩溃的) ...

  4. 元素隐藏visibility:hidden与元素消失display:none的区别

    visibility属性用来确定元素是显示还是隐藏的,这用visibility="visible|hidden"来表示(visible表示显示,hidden表示隐藏). 当visi ...

  5. Search Engine Hacking – Manual and Automation

    Search Engine Hacking – Manual and Automation Ethical Hacking Boot Camp OUR MOST POPULAR COURSE! CLI ...

  6. Gym 100917F Find the Length

    题目链接:http://codeforces.com/gym/100917/problem/F ---------------------------------------------------- ...

  7. mysql 开放远程连接权限连不上

    1.my.cof配置了:bind-address=addr  或   skip-networking,需要注释 2.防火墙限制3306端口: iptables -L -n --line-numbers ...

  8. HTML--JS 表单验证

    <html> <head> <title>验证表单</title> <script type="text/javascript" ...

  9. 【HANA系列】SAP HANA的特点总结

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA的特点总结   ...

  10. oracle创建sequence序列语法

    在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系 1.create sequence create sequence SEQ_LOG_ID minval ...