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对此也提供了相应的解决方案 ...
随机推荐
- Android7.0后JNI库必须保留Section Headers
此修改在官网的描述如下: Each ELF file has additional information contained in the section headers. These header ...
- Vagrant 手册之 Vagrantfile - 提示及技巧
原文地址 Vagrantfile 是一种非常灵活的配置格式.语法基于 Ruby,可以用它做很多事情.在本页使用一些提示和技巧时,请注意正确使用它们. 1. 使用循环定义虚拟机 如果你想对多机器应用稍微 ...
- redis连接报错:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to...
连接redis报错: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persis ...
- (appium+python)UI自动化_09_unittest批量运行测试用例&生成测试报告
前言 上篇文章[(appium+python)UI自动化_08_unittest编写测试用例]讲到如何使用unittets编写测试用例,并执行测试文件.接下来讲解下unittest如何批量执行测试文件 ...
- Java + selenium 元素定位(6)之iframe切换(即对富文本框的操作)
在元素定位中,对富文本框的元素定位是特别的,当我们使用普通的元素定位方法对富文本框进行操作时,我们会发现不管我们之前介绍的八种方法中的任何方法,我们都不能成功定位到富文本框,并对其进行操作.那是因为富 ...
- JSP基础--会话跟踪技术、cookie、session
会话跟踪技术 1 什么是会话跟踪技术 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而10 ...
- OAUTH2.0协议-菜鸟级
OAUTH2.0入门必看 一.摘要 OAUTH协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即 ...
- MFC---导出 Excel 方法
本方法通过Excel驱动写入 请添加头文件 #include"afxdb.h" 第一步创建Excel文件 安装驱动 CString FileName = L"first. ...
- Java数组相关算法一
一.数组反转 1.方法一:创建新数组 int[] arr = {6,29,0,4,3}; int[] arr2 = new int[arr.length]; for (int i = 0; i < ...
- JVM(Java虚拟机)详解(JDK7)
1.Java内存区域 运行时数据区域: Java 虚拟机在执行Java程序时,定义了若干种程序运行期间会使用到的运行时数据区,其中有一些会随着虚拟机启动而创建,随着虚拟机退出而销毁.另外一些则是与线程 ...