博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 之依赖注入DI
阅读量:4209 次
发布时间:2019-05-26

本文共 4442 字,大约阅读时间需要 14 分钟。

IOC容器

这里写图片描述

1. 配置元数据

2.实例化容器

基础包:

spring-beans :

BeanFactory提供配置结构和基本功能 加载并初始化bean

spring-context

ApplicationContext保存Bean对象并使用

2.1 本地文件

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:/file.txt");

2.2 Classpath

ApplicationContext context =    new ClassPathXmlApplicationContext(new String[] {
"services.xml", "daos.xml"});

2.3 web应用

使用监听器

org.springframework.web.context.ContextLoadListener

使用Servlet

contextLoadServlet
org.springframework.web.context.ContextLoadServlet
1

3.使用容器

// 创建并配置beansApplicationContext context =    new ClassPathXmlApplicationContext(new String[] {
"services.xml", "daos.xml"});// 取得配置的实例PetStoreService service = context.getBean("petStore", PetStoreService.class);// 使用实例List
userList = service.getUsernameList();

Bean概述

属性名 说明
class “实例化bean”
name “bean的命名”
scope “Bean作用域”
constructor arguments “依赖注入”
properties “依赖注入”
autowiring mode “自动装配协作者”
lazy-initialization mode “延迟初始化bean”
initialization method “初始化回调函数”
destruction method “析构回调函数”

1.内部类名.

如果你想配置使用静态的内部类,你必须用内部类的二进制名称。

例如,在com.example包下有个Foo类,这里类里面有个静态的内部类Bar,这种情况下bean定义的class属性

com.example.Foo$Bar

使用$字符来分割外部类和内部类的名称。

使用静态工厂方法实例化

/** * UserServiceLoader * Created by heqianqian on 2017/4/24. */public class UserServiceLoader {
private static UserService userService = new UserServiceImpl("haha"); private UserServiceLoader() { } public static UserService getInstance() { return userService; }}

依赖注入DI

指对象之间的依赖关系

一起协作的其他对象只通过构造器的参数工厂方法的参数或者由构造函数或者工厂方法创建的对象设置属性。因此容器的工作就是创建bean并注入那些依赖关系。

这个过程实质通过直接使用类的构造函数或者服务定位模式来反转控制bean的实例或者其依赖关系的位置,因此它有另外一个名字叫控制反转 (IoC)。

DI主要有两种注入方式,即构造器注入Setter注入。

构造器注入

1.参数类型定义不存在潜在的歧义【不同类型的引用类型】

package x.y;public class Foo {    public Foo(Bar bar, Baz baz) {        // ...    }}

2. 参数为基本类型或者String类型

2.1 使用type区别

public class Student {    private Integer sNum;    private String sName;    public Student() {    }    public Student(Integer sNum, String sName) {        this.sNum = sNum;        this.sName = sName;    }    @Override    public String toString() {        return "Student{" +                "sNum=" + sNum +                ", sName='" + sName + '\'' +                '}';    }}

2.2 使用index区别 index从0开始

2.3 使用构造器参数命名

代码编译时要打开编译模式,这样Spring可以检查构造方法的参数。

如果你不打开调试模式(或者不想打开),也可以使用 @ConstructorProperties JDK注解明确指出构造函数的参数

Setter注入

public class User {    private String name;    private int age;    public User() {    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public String toString() {        return "User{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }}

注入Properties

方法一:

name=MR.LI class=1405

方法二:

MR.Huang
1405

注入List

1405
1406
1407
1408

注入Map

注入Set

5-206
5-206
5-206
5-207

Bean作用域

singleton:

(默认的) 每个 String IoC 容器作用域中一个 bean 定义只对应一个对象实例。

prototype:

一个 bean 定义对应多个对象实例

request

一个 bean 定义作用于 HTTP request 生命周期;是指每个 HTTP request 拥有自己的通过一个 bean 定义创建的实例。仅在基于 web 的 Spring ApplicationContext 中有效。

session

一个 bean 定义作用于 HTTP session 生命周期。仅在基于 web 的 Spring ApplicationContext 中有效。

global session

一个 bean 定义作用于全局的 HTTP session 生命周期。仅在 portlet context 中使用才有效。仅在基于 web 的 Spring ApplicationContext 中有效。

application

一个 bean 定义作用于整个 ServletContext 生命周期。仅在基于 web 的 Spring ApplicationContext 中有效。

定制bean特性

生命周期回调

1.初始化回调函数

public class ExampleBean {    public void init() {        // do some initialization work    }}

2.析构回调函数

public class ExampleBean {    public void cleanup() {        // do some destruction work (like releasing pooled connections)    }}

转载地址:http://nxqli.baihongyu.com/

你可能感兴趣的文章
Java图形界面中单选按钮JRadioButton和按钮Button事件处理
查看>>
小练习 - 排序:冒泡、选择、快排
查看>>
SparkStreaming 如何保证消费Kafka的数据不丢失不重复
查看>>
Spark Shuffle及其调优
查看>>
数据仓库分层
查看>>
常见数据结构-TrieTree/线段树/TreeSet
查看>>
Hive数据倾斜
查看>>
TopK问题
查看>>
Hive调优
查看>>
HQL排查数据倾斜
查看>>
DAG以及任务调度
查看>>
LeetCode——DFS
查看>>
MapReduce Task数目划分
查看>>
ZooKeeper分布式锁
查看>>
3126 Prime Path
查看>>
app自动化测试---ADBInterface驱动安装失败问题:
查看>>
RobotFramework+Eclipse安装步骤
查看>>
测试的分类
查看>>
photoshop cc2019快捷键
查看>>
pycharm2019版本去掉下划线的方法
查看>>