Spring 的依赖
用来设置两个 bean 的创建顺序。
IoC 容器默认情况下是通过 spring.xml 中 bean 的配置顺序来决定创建顺序的,配置在前⾯的 bean 会先创建。
在不更改 spring.xml 配置顺序的前提下,通过设置 bean 之间的依赖关系来调整 bean 的创建顺序。
<bean id="account" class="com.southwind.entity.Account" depends-on="user">
</bean> <bean id="user" class="com.southwind.entity.User"></bean>
上述代码的结果是先创建 User,再创建 Account。
Spring 读取外部资源
实际开发中,数据库的配置⼀般会单独保存到后缀为 properties 的⽂件中,方便维护和修改,如果使用Spring 来加载数据源,就需要在spring.xml 中读取 properties 中的数据,这就是读取外部资源。
jdbc.properties
user = root
password = root
url = jdbc:mysql://localhost:3306/library
driverName = com.mysql.cj.jdbc.Driver
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
">
<!-- 导⼊外部资源 -->
<context:property-placeholder location="classpath:jdbc.properties">
</context:property-placeholder>
<!-- SpEL -->
<bean id="dataSource" class="com.southwind.entity.DataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverName" value="${driverName}"></property>
<property name="url" value="${url}"></property>
</bean>
</beans>
Spring p 命名空间
p 命名空间可以⽤来简化 bean 的配置。
<bean id="student" class="com.southwind.entity.Student" p:id="1" p:name="张三"
p:age="22" p:classes-ref="classes">
</bean> <bean id="classes" class="com.southwind.entity.Classes" p:id="1" p:name="⼀班">
</bean>
Spring 工厂方法
IoC 通过工厂模式创建 bean 有两种⽅式:
静态工厂⽅法
实例工厂⽅法
区别在于静态工厂类不需要实例化,实例工厂类需要实例化静态工厂方法
静态工厂⽅法
1、创建 Car 类
@Data
@AllArgsConstructor
public class Car {
private Integer num;
private String brand; }
2、创建静态工厂类、静态工厂方法
public class StaticCarFactory{
private static Map<Integer, Car> carMap;
static {
carMap = new HashMap<>();
carMap.put(1,new Car(1,"奥迪"));
carMap.put(2,new Car(2,"奥拓"));
}
public static Car getCar(Integer num){
return carMap.get(num);
}
}
3、spring.xml
<bean id="car1" class="com.southwind.factory.StaticCarFactory" factorymethod="getCar">
<constructor-arg value="1"></constructor-arg>
</bean>
factory-method 指向静态⽅法
constructor-arg 的 value 属性是调⽤静态⽅法传⼊的参数
实例工厂方法
1、创建实例工厂类、工厂方法
public class InstanceCarFactory {
private Map<Integer, Car> carMap;
public InstanceCarFactory(){
carMap = new HashMap<>();
carMap.put(1,new Car(1,"奥迪"));
carMap.put(2,new Car(2,"奥拓"));
}
public Car getCar(Integer num){
return carMap.get(num);
}
}
2、spring.xml
<!-- 实例⼯⼚ -->
<bean id="instanceCarFactory"
class="com.southwind.factory.InstanceCarFactory"></bean>
<!-- 通过实例⼯⼚获取Car -->
<bean id="car2" factory-bean="instanceCarFactory" factory-method="getCar" >
<constructor-arg value="2"></constructor-arg>
</bean>
区别:
静态工厂方法创建 Car 对象,不需要实例化工厂对象,因为静态工厂的静态⽅法,不需要创建对象即可
调⽤,spring.xml 中只需要配置⼀个 bean ,即最终的结果 Car 即可。
实例工厂⽅法创建 Car 对象,需要实例化工厂对象,因为 getCar ⽅法是⾮静态的,就必须通过实例化
对象才能调⽤,所以就必须要创建工厂对象,spring.xml 中需要配置两个 bean,⼀个是工厂 bean,⼀个是 Car bean。
spring.xml 中 class + factory-method 的形式是直接调⽤类中的工厂⽅法
spring.xml 中 factory-bean + factory-method 的形式则是调⽤工厂 bean 中的工厂⽅法,就必须先创建工厂 bean。