Spring Ioc、Ioc DI、Spring 继承,been

Spring 两⼤核心机制

IoC:工厂模式
AOP:代理模式

IoC

IoC 是 Spring 框架的灵魂,控制反转。

开发步骤
1、创建 Maven ⼯程,pom.xml 导⼊依赖。

<dependencies>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>5.2.3.RELEASE</version>
 </dependency>
</dependencies>

2、在 resources 路径下创建 spring.xml。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
 <bean id="student" class="com.southwind.entity.Student"></bean>
</beans>

3、IoC 容器通过读取 spring.xml 配置⽂件,加载 bean 标签来创建对象。

4、调⽤ API 获取 IoC 容器中已经创建的对象。

ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("spring.xml");
Student student = (Student)applicationContext.getBean("student");
System.out.println(student);

IoC 容器创建 bean 的两种方式

无参构造函数

<bean id="student" class="com.southwind.entity.Student"></bean>

给成员变量赋值

<bean id="student" class="com.southwind.entity.Student">
 <property name="id" value="1"></property>
 <property name="name" value="张三"></property>
 <property name="age" value="22"></property>
</bean>

有参构造函数

<bean id="student3" class="com.southwind.entity.Student">
 <constructor-arg index="1" value="王五"></constructor-arg>
 <constructor-arg index="0" value="3"></constructor-arg>
 <constructor-arg index="2" value="18"></constructor-arg>
</bean>
<bean id="student3" class="com.southwind.entity.Student">
 <constructor-arg name="name" value="王五"></constructor-arg>
 <constructor-arg name="id" value="3"></constructor-arg>
 <constructor-arg name="age" value="18"></constructor-arg>
</bean>

从 IoC 容器中取 bean

通过 id 取值

Student student = (Student)applicationContext.getBean("student");

通过类型取值。

Student student = (Student)applicationContext.getBean(Student.class);

bean 的属性中如果包含特殊字符,如下处理即可

<bean id="classes" class="com.southwind.entity.Classes">
 <property name="id" value="1"></property>
 <property name="name">
 <value><![CDATA[<⼀班>]]></value>
 </property>
</bean>

IoC DI

DI 指 bean 之间的依赖注⼊,设置对象之间的级联关系。

Classes

@Data
public class Classes {
 private Integer id;
 private String name; }

Student

@Data
@NoArgsConstructor
public class Student {
 private Integer id;
 private String name;
 private Integer age;
 private Classes classes;
 public Student(Integer id, String name, Integer age) {
 System.out.println("通过有参构造创建对象");
 this.id = id;
 this.name = name;
 this.age = age;
 }
 public Student(Integer id, String name) {
 this.id = id;
 this.name = name;
 }
}

spring-di.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
 <!-- Classes -->
 <bean id="classes" class="com.southwind.entity.Classes">
 <property name="id" value="1"></property>
 <property name="name" value="⼀班"></property>
 </bean>
 <!-- Student -->
 <bean id="student" class="com.southwind.entity.Student">
 <property name="id" value="100"></property>
 <property name="name" value="张三"></property>
 <property name="age" value="22"></property>
 <property name="classes" ref="classes"></property>
 </bean>
</beans>

bean 之间的级联需要使⽤ ref 属性完成映射,⽽不能直接使⽤ value ,否则会抛出类型转换异常。

Classes

@Data
public class Classes {
 private Integer id;
 private String name;
 private List<Student> studentList; }

spring.xml

<!-- Classes -->
<bean id="classes" class="com.southwind.entity.Classes">
 <property name="id" value="1"></property>
 <property name="name" value="⼀班"></property>
 <property name="studentList">
 <list>
 <ref bean="student"></ref>
 <ref bean="student2"></ref>
 </list>
 </property>
</bean>
<!-- Student -->
<bean id="student" class="com.southwind.entity.Student">
 <property name="id" value="100"></property>
 <property name="name" value="张三"></property>
 <property name="age" value="22"></property>
 <property name="classes" ref="classes"></property>
</bean> <bean id="student2" class="com.southwind.entity.Student">
 <property name="id" value="200"></property>
 <property name="name" value="李四"></property>
 <property name="age" value="18"></property>
 <property name="classes" ref="classes"></property>
</bean>

Spring 中的 bean

bean 是根据 scope 来⽣成,表示 bean 的作⽤域,scope 有 4 种类型:

singleton,单例,表示通过 Spring 容器获取的对象是唯⼀的,默认值。

prototype,原型,表示通过 Spring 容器获取的对象是不同的。

request,请求,表示在⼀次 HTTP 请求内有效。

session,会话,表示在⼀个⽤户会话内有效。

requset,session 适⽤于 Web 项⽬。
singleton 模式下,只要加载 IoC 容器,⽆论是否从 IoC 中取出 bean,配置⽂件中的 bean 都会被创建。
prototype 模式下,如果不从 IoC 中取 bean,则不创建对象,取⼀次 bean,就会创建⼀个对象。

Spring 的继承

Spring 继承不同于 Java 中的继承,区别:Java 中的继承是针对于类的,Spring 的继承是针对于对象(bean)。

Spring 的继承中,子bean 可以继承父 bean 中的所有成员变量的值

<bean id="user1" class="com.southwind.entity.User">
 <property name="id" value="1"></property>
 <property name="name" value="张三"></property>
</bean> <bean id="user2" class="com.southwind.entity.User" parent="user1">
 <property name="name" value="李四"></property>
</bean>

通过设置 bean 标签的 parent 属性建⽴继承关系,同时⼦ bean 可以覆盖⽗ bean 的属性值。
Spring 的继承是针对对象的,所以⼦ bean 和 ⽗ bean 并不需要属于同⼀个数据类型,只要其成员变量列表⼀致即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

走不尽的心路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值