2.spring基础入门(二)

1.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。

2.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>

3.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>

4.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" factory-method="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。

5.Spring IoC ⾃动装载 autowire

⾃动装载是 Spring 提供的⼀种更加简便的⽅式来完成 DI,不需要⼿动配置 property,IoC 容器会⾃动

选择 bean 完成注⼊。

⾃动装载有两种⽅式:

  • byName,通过属性名完成⾃动装载。
  • byType,通过属性对应的数据类型完成⾃动装载。

byName 的操作如下所示。

1、创建 Person 实体类。

@Data
public class Person {
     private Integer Id;
     private String name;
     private Car car;
}

2、在 spring.xml 中配置 Car 和 Person 对应的 bean,并且通过⾃动装载完成依赖注⼊。

<bean id="person" class="com.southwind.entity.Person" autowire="byName">
     <property name="id" value="1"></property>
     <property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.southwind.entity.Car">
     <constructor-arg name="num" value="1"></constructor-arg>
     <constructor-arg name="brand" value="奥迪"></constructor-arg>
</bean>

6.Spring IoC 基于注解的开发(重点)

Spring IoC 的作⽤是帮助开发者创建项⽬中所需要的 bean,同时完成 bean 之间的依赖注⼊关系,

DI。

实现该功能有两种⽅式:

  • 基于 XML 配置。
  • 基于注解。

基于注解有两步操作,缺⼀不可:

1、配置⾃动扫包。

2、添加注解。

<context:component-scan base-package="com.southwind.entity">
</context:component-scan>
@Data
@Component
public class Repository {
 private DataSource dataSource;
}

DI

@Data
@Component
public class DataSource {
 private String user;
 private String password;
 private String url;
 private String driverName;
}

@Data
@Component(value = "myrepo")
public class Repository {
 @Autowired
 private DataSource dataSource;
}

@Autowired 默认是通过 byType 进⾏注⼊的,如果要改为 byName,需要配置 @Qualifier 注解来完成

@Autowired
@Qualifier(value = "ds")
private DataSource dataSource;

表示将 IoC 中 id 为 ds 的 bean 注⼊到 repository 中。

实体类中普通的成员变量(String、包装类等)可以通过 @Value 注解进⾏赋值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值