Spring---基础配置
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---基础配置的更多相关文章
- Spring基础配置
从毕业到现在我一直从事Android开发,但是对JavaEE一直念念不忘,毕业校招的时候,一个礼拜拿了三个offer,岗位分别是Android.JavaEE和JavaSE,后来觉得Android比较简 ...
- 01—Spring基础配置IOC
- SpringMVC基础配置(通过注解配置,非xml配置)
SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...
- Spring Boot实战(1) Spring基础
1. Spring基础配置 Spring框架本身有四大原则: 1) 使用POJO进行轻量级和最小侵入式开发 2) 通过依赖注入和基于接口编程实现松耦合 3) 通过AOP和默认习惯进行声明式编程 4) ...
- Spring Boot学习第一部分(Spring 4.x)第一章(Spring 基础)
1.spring概述 1.1.spring的简史 第一阶段:XML配置spring 1.x时代, 第二阶段:注解配置spring 2.x时代, @Controller @Service @Compon ...
- 转 RabbitMQ 基础概念及 Spring 的配置和使用 推荐好文 举例讲解
从不知道到了解—RabbitMQ 基础概念及 Spring 的配置和使用 原理同上 请求地址:http://localhost:8080/home?type=3&routing_key=myO ...
- Spring Boot 基础配置
之前简单接触了一些Spring Boot ,并且写了一个简单的 Demo .本文就来简单学习一下 Spring Boot 的基础配置. 一.Spring Boot 项目入口 上文中有写到,Spring ...
- Java进阶知识15 Spring的基础配置详解
1.SSH各个的职责 Struts2:是web框架(管理jsp.action.actionform等).Hibernate:是ORM框架,处于持久层.Spring:是一个容器框架,用于配置bean,并 ...
- Spring基础——在Spring Config 文件中配置 Bean
一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...
- Spring基础篇——通过Java注解和XML配置装配bean
自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...
随机推荐
- 27 August
高精度 struct bigint{ int a[1000],an; bigint operator = (int b){ an=0; while (b){a[an++]=b%10;b/=10;} r ...
- HTML与CSS中的文本个人分享
文本 标题元素 注意: 在一个HTML页面中最好只使用一个<h1>标题 因为浏览器只会抓取一个多了没用 示例代码: <body> <!-- 标题元素 - <h1&g ...
- (转)k8s集群部署二:flannel网络
转:https://blog.csdn.net/sinat_35930259/article/details/79946146 Overlay Network模式 覆盖网络,在基础网络上叠加的一种虚拟 ...
- iReport+JasperReport+JSP 输出HTML方式预览
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...
- Jenkins+GitLab持续集成
向GitLab提交代码之后自动触发Jenkins构建 https://baijiahao.baidu.com/s?id=1630678692475452408&wfr=spider&f ...
- 学习使用Delphi for android 调用Java类库
http://blog.csdn.net/laorenshen/article/details/41148253 学习使用Delphi for android 调用Java类库 2014-11-15 ...
- Python专题三字符串的基础知识
Python专题三字符串的基础知识 在Python中最重要的数据类型包括字符串.列表.元组和字典等.该篇主要讲述Python的字符串基础知识. 一.字符串基础 字符串指一有序的字符序列集合,用单引号. ...
- Caffe深入分析(源码)
Caffe的整体流程图: 程序入口:main() int main(int argc, char** argv) { ..... ]))(); .... } g_brew_map实现过程,首先通过 t ...
- MapReduce(3): Partitioner, Combiner and Shuffling
Partitioner: Partitioning and Combining take place between Map and Reduce phases. It is to club the ...
- c# 调用 webService
开局几张 照着做就完事 说明下 这个wsdl 文件是根据别人提供的webService 接口 打开后改变后缀来的 这样就引用完成了 接下来就是重点了 怎么调用 localhost.WsSyncDu ...