一、Bean作用域

spring容器创建的时候,会将所有配置的bean对象创建出来,默认bean都是单例的。代码通过getBean()方法从容器获取指定的bean实例,容器首先会调用Bean类的无参构造器,创建实例对象

那么?我们如何说明出bean是单例的呢?

构建出两份学生对象,执行,发现两个对象的内存地址相同,内存中只有一份

如何使它成为多例的呢?那么则需要在配置文件中添加scope="prototype"该属性即可!

scope="prototype" 原型模式(N个对象):真正使用时才会创建,每获取一次,都会创建不同对象
scope="singleton" 单例模式:容器初始化时需要使用name建,每次获取的都是同一个对象,默认值


二、基于xml的DI(Dependency Injection)

注入类型:

定义学生Student实体类和小汽车Car实体类:进行封装和生成ToString(),并自定义属性Car

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Student {
private String name;
private String age;
private Car car;
//无参构造
public Student() {
    //System.out.println("Student.Student()");
}
//带参构造
public Student(String name, String age, Car car) {
    this.name = name;
    this.age = age;
    this.car = car;
}
@Override
public String toString() {
    return "Student [name=" + name + ", age=" + age + ", car=" + car + "]";
}
public Car getCar() {
    return car;
}
public void setCar(Car car) {
    this.car = car;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
}

 Car:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class Car {
private String color;
private String brand;
//无参构造
public Car() {
     
}
//带参构造
public Car(String color, String brand) {
    super();
    this.color = color;
    this.brand = brand;
}
@Override
public String toString() {
    return "Car [color=" + color + ", brand=" + brand + "]";
}
public String getColor() {
    return color;
}
public void setColor(String color) {
    this.color = color;
}
public String getBrand() {
    return brand;
}
public void setBrand(String brand) {
    this.brand = brand;
}
}

 1.1设值注入(set方法注入):本质上是调用了Bean的setXXX()进行值的注入。分为普通属性和域属性

测试类:

1
2
3
4
5
6
7
8
public class Test01 {
@Test
public void addTest(){
    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
     
    Student student=(Student) ctx.getBean("stu");
    System.out.println(student);
}

实现效果:


1.2构造注入

实现效果:


1.3命名空间p注入
使用前要先要在Spring配置文件中引入p命名空间

实现效果:


三、集合属性注入[List、Set、Map]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class MyCollection {
private List<String> list;
private Set<String> set;
private Map<String,String> map;
public Map<String, String> getMap() {
    return map;
}
public void setMap(Map<String, String> map) {
    this.map = map;
}
public Set<String> getSet() {
    return set;
}
public void setSet(Set<String> set) {
    this.set = set;
}
public List<String> getList() {
    return list;
}
public void setList(List<String> list) {
    this.list = list;
}

Spring配置文件:

List与Set同理:

Map双列集合:

测试类:调用对应的方法:

1
2
3
4
5
6
7
8
9
10
public class Test01 {
@Test
public void addTest(){
    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
    MyCollection collection=(MyCollection) ctx.getBean("collection");
    //System.out.println(collection.getList());
    //System.out.println(collection.getSet());
    System.out.println(collection.getMap());
}


四、基于注解的DI

注:在项目中添加Spring AOP相关的JAR文件以及xsd约束文件。

由于是基于注解的DI,所以无需再Spring配置文件中进行节点配置,只需配置包扫描器即可!

配置包扫描器用途:

该包下以及子包中的类才可以被Spring扫描,去寻找被注解的类和属性,让Spring容器管理赋值

Student类:

指定@Component中的value即可在测试类中的getBean()中植入即可。

@Value为该属性赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Component(value="stu")
public class Student {
@Value("呵呵")   
private String name;
@Value("13")
private String age;
/*
 * JDK注解 @Resource(name="car2")
 */
/*
 * Spring注解
 */
@Autowired
@Qualifier(value="car2")
private Car car;
@Override
public String toString() {
    return "Student [name=" + name + ", age=" + age + ", car=" + car + "]";
}

Car类:

1
2
3
4
5
6
7
8
9
10
11
12
13
@Component(value="car2")
public class Car {
@Value("黑色")
private String color;
@Value("奥迪")
private String brand;
@Override
public String toString() {
    return "Car [color=" + color + ", brand=" + brand + "]";
}

测试类:

1
2
3
4
5
6
7
8
9
public class Test01 {
@Test
public void addTest(){
    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
     
    Student student=(Student) ctx.getBean("stu");
    System.out.println(student);
}
}

实现效果: 

等价于@Component的注解:

@Component[不分层的情况下]

@Repository() [Dao层] 

@Service() [Biz层]

@Controller() [Action类]

转载于:https://www.cnblogs.com/hr1997/p/5993387.html

第二章 spring相关推荐

  1. SpringBoot学习笔记-2:第二章 Spring Boot 配置

    第二章 Spring Boot 配置 1.YAML 配置 SpringBoot 全局配置文件 application.properties application.yml YAML 以数据为中心,比 ...

  2. 第二章 Spring Boot四大核心组件

    文章目录 前言 一.Spring Boot Starter 1.1 Starter的应用示例 1.2 Spring Boot之前的Thymeleaf和Mybatis应用 1.2.1 Thymeleaf ...

  3. 第二章 ---- spring注解开发

    文章目录 参考视频 注解驱动的意义 常用注解(重点) 启动注解驱动 IoC bean定义(@Component .@Controller.@Service. @Repository) @Scope b ...

  4. spring boot @value_spring+vue全栈开发实战-第二章Spring Boot 基础配置-笔记0302-2020

    Spring Boot 基础配置 1. Web 容器配置 2.Properties 配置 3.类型安全配置属性 1. Web 容器配置 a.常规配置 在 Spring Boot 项 目 中,可以内置 ...

  5. 第二章 Spring MVC入门 —— 跟开涛学SpringMVC

    2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架. Spring Web MVC也是服务到工作者 ...

  6. 框架设计--第一章 Spring的基本应用--习题答案

    摘要:微信搜索[三桥君] 课程介绍:"框架技术"是软件工程专业的核心课程,是本专业限选课,是Java 应用开发课程,是本专业学生就业的主要方向. 说明:框架设计其他章节的习题答案也 ...

  7. 19年8月 字母哥 第一章 spring boot 2.x基础及概念入门 这里全部看完了 热部署没出来 第二章在前面2页 用热点公司网不行

    http://springboot.zimug.com/1233100   文档 http://www.zimug.com/page/5     字母哥个人博客 11111 第一章 spring bo ...

  8. 《Spring Recipes》第二章笔记:Creating Beans by Invokin...

    2019独角兽企业重金招聘Python工程师标准>>> <Spring Recipes>第二章笔记:Creating Beans by Invoking an Insta ...

  9. 《深入理解 Spring Cloud 与微服务构建》第二章 微服务应该具备的功能

    <深入理解 Spring Cloud 与微服务构建>第二章 微服务应该具备的功能 文章目录 <深入理解 Spring Cloud 与微服务构建>第二章 微服务应该具备的功能 一 ...

最新文章

  1. Windows10+Virtual box+ubuntu17.10
  2. Python 技巧篇-让我的程序暂停一下
  3. Black Hat|英特尔CPU设计漏洞为恶意软件打开后门
  4. mybatis学习(47):嵌套查询--一对一
  5. kafka 生产和消费信息入门
  6. Java程序员实现完美代码的十大要素
  7. 《tcpip详解卷一》:150行代码拉开协议栈实现的篇章
  8. poi数据导入arcgis_在Excel中处理和使用地理空间数据(如POI数据)
  9. MySQL如何执行关联查询
  10. netstat 的10个基本用法
  11. FPGA抗辐射加固方法
  12. 不良资产证券化之后,谁来买单?
  13. centos 7 JDK 环境部署
  14. 引领企业电销革新,外呼系统是不可缺的电销工具
  15. Java后端开发常考面试题大全
  16. 1020 正负奇偶判断
  17. Modbus RTU转Modbus TCP模块,RS232/485转以太网模块,WJ102
  18. mysql group by 后求和_MySQL分组求和GROUP BY
  19. 通常我们将python语言程序保存在一个后缀_知到毛概章节测试答案2020
  20. 服务器系统装音频设备,远程服务器未安装音频设备

热门文章

  1. Java实现栅格数据格式文件读取及加法操作
  2. 产品研究分析--王者荣耀的那些套路
  3. java设置窗体关闭时执行某些操作
  4. “RPC 好,还是 RESTful 好?” 不要选错了!
  5. 深入理解Golang包导入
  6. Java线上应用故障排查之一:高CPU占用
  7. 网络:forward和redirect的区别
  8. 8.公有继承 保护继承 私有继承
  9. Hibernate实现limit查询报错 unexpected token: ? near line 1, column 30 [from cn.com.bean.Layer limit ? ,
  10. 天津php二次开发培训,天津PHP后台开发培训短期班