ioC/DI的实现
概念:IoC(Inversion of Control)控制反转,也称为DI(dependency injection)依赖注入
IoC就是将创建对象的权利,由Java代码转移到spring容器,由spring的容器控制对象的创建,就是控制反转;
DI就是“被注入对象依赖IoC容器来配置依赖对象”,即依赖注入
1. 创建项目,添加依赖
创建普通maven项目,在项目的pom.xml文件中添加spring依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.29</version>
</dependency>
</dependencies>
2. 创建一个类
创建Book类
package com.msb.pojo;
public class Book {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
System.out.println("id的setter方法");
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("name的setter方法");
this.name = name;
}
public Book() {
System.out.println("Book类空构造器");
}
public Book(int id, String name) {
System.out.println("Book类有参构造器");
this.id = id;
this.name = name;
}
}
3. 创建Spring配置文件
通过 配置文件 创建对象b
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="b" class="com.msb.pojo.Book"></bean>
</beans>
4. 编写测试类
public class Test {
public static void main(String[] args) {
// 创建spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取对象:从容器中取出【创建对象时,容器就已经存在】
Book book = (Book)context.getBean("b");
// 打印对象信息
System.out.println(book.getName() + "----" + book.getId());
}
}
此时该对象未赋值。赋值见下
属性注入【简单数据类型】
- 设值注入【以name注入为例,要与Book名称对应】-----> 以前:setter方式
<bean id="b" class="com.msb.pojo.Book">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
- 构造器注入【确保有参构造器存在,name要与参数名对应】-----> 以前:构造器方式
<bean id="b2" class="com.msb.pojo.Book">
<constructor-arg name="id" value="2"></constructor-arg>
<constructor-arg name="name" value="李四"></constructor-arg>
</bean>
属性注入【属性为引用数据类型】
-
不使用属性注入创建对象:
-
使用属性注入:ref引用对象
<bean id="boy" class="com.msb.pojo.Boy">
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
</bean>
<bean id="girl" class="com.msb.pojo.Girl">
<property name="age" value="19"></property>
<property name="name" value="露露"></property>
<property name="boyfriend" ref="boy"></property>
</bean>
public static void main(String[] args) {
// 创建spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取对象
Girl g = (Girl)context.getBean("girl");
System.out.println("女孩名字:" + g.getName() + ",男朋友名字:" + g.getBoyfriend().getName());
}
注解
概念:注解就是带@的特殊标记,将其当成修饰符使用,他们在被读取后进行相应处理
| 注解名称 | 解释 |
|---|---|
| @Component | 实例化Bean,默认名称为首字母变小写,无需指定setter方法 |
| @Repository | 作用和@component一样。用在持久层 |
| @Service | 作用和@component一样。用在业务层 |
| @Configuration | 作用和@component一样。用在持久层 |
| Autowired | 自动注入。默认byType,如果多个同类型bean,使用byName。不需要依赖setter |
| @Value | 给普通数据类型(八种基本+String)属性赋值。不需要依赖setter |
举例
在Girl类添加@Component
在 配置文件(aop换成context)添加contex约束,导入命名空间
<beans xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.msb.pojo"></context:component-scan>
<!-- 不再需要<bean>来属性注入 -->
IoC/DI注解
@Component
public class Girl {
@Value("21")
private int age;
@Value("丽丽")
private String name;
@Autowired
private Boy boyfriend;
public static void main(String[] args) {
// 创建spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取对象
Girl g = (Girl)context.getBean("girl");
System.out.println(g.getName() + "男朋友是" + g.getBoyfriend().getName());
}