Spring-依赖注入

1.依赖注入(DI)

  依赖注入是IOC(ioc获取资源的方式:之前要主动获取,现在是被动接受)的具体实现方式,在依赖的对象设置相对应的方法(比如set方法,有参构造),以当前设置好的方法来接受Spring注入的对象。

如:Student类中有属性 id、name、age等,所以Student对象是依赖于这些属性的,在ioc容器中为它所依赖的属性赋值。

依赖注入:就是为当前类中的属性赋值的过程。

2. 依赖注入—setter注入

Student类

public class Student {
    private Integer sid;
    private String sname;
    private Integer sage;
    private String gender;
    public Integer getSid() {
        return sid;
    } 
    ......
}

spring配置

spring-ioc.xml

<!--
    property:通过成员变量的set方法进行赋值
    name:设置需要赋值的属性名(和set方法有关)
    value:设置为属性所赋的值
 -->
<bean id="students" class="com.pojo.Student">
    <property name="sid" value="001"/>
    <property name="sname" value="张三"/>
    <property name="sage" value="12"/>
    <property name="gender" value="男"/>
</bean>

测试

public void testDI(){
    //获取IOC容器
    ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
    //获取bean
    Student student = ioc.getBean("students", Student.class);
    System.out.println(student);
}

3.  依赖注入—构造器注入(constructor-arg)

<bean id="studentThree" class="com.atguigu.spring.pojo.Student">
        <constructor-arg value="1002"></constructor-arg>
        <constructor-arg value="李四"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
        <constructor-arg value="24" name="age"></constructor-arg>
</bean>

若姓名设为:sname="<王五>"
  方法:      <:&lt;
         >:&gt;
<property name="sname" value="&lt;王五&gt;"></property>

或者:

CDATA节其中的内容会原样解析<![CDATA[...]]>
CDATA节是xml中一个特殊的标签,因此不能写在一个属性中 (idea中输入CD)

<property name="sname"> <value><![CDATA[<王五>]]></value></property>

4. 依赖注入—为类类型属性赋值

public class Student implements Person {
    private Integer sid;
    private String sname;
    private Integer age;
    private String gender;
    private Double score;
    private String[] hobby;
    private Clazz clazz;
}
public class Clazz {
    private Integer cid;
    private String cname;
}

spring配置

<bean id="studentFive" class="com.atguigu.spring.pojo.Student">
    <property name="sid" value="1004"></property>
    <property name="sname" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="gender" value="男"></property>
       <!--ref:引用IOC容器中的某个bean的id-->
    <property name="clazz" ref="clazzOne"></property>
</bean>
<bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
        <property name="cid" value="0001"></property>
        <property name="cname" value="S班"></property>
</bean>

 或者

<property name="clazz">
   <!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
   <bean id="clazzInner" class="com.pojo.Clazz">
         <property name="cid" value="0002"></property>
         <property name="cname" value="A班"></property>
   </bean>
</property>

 5.  依赖注入—为数组属性赋值

private String[] hobby;

<property name="hobby">
            <array>
                <value>唱歌</value>
                <value>跳舞</value>
                <value>弹琴</value>
            </array>
</property>

6. 依赖注入—为list集合属性赋值

//Clazz类中:
private List<Student> students;

//方法一:
    <bean id="clazzOne" class="com.pojo.Clazz">
        <property name="cid" value="0002"></property>
        <property name="cname" value="A班"></property>
        <property name="students">
            <list>
                <ref bean="studentOne"></ref>
                <ref bean="studentTwo"></ref>
                <ref bean="studentThree"></ref>
            </list>
        </property>
    </bean>
//方法二:
   <bean id="clazzOne" class="com.pojo.Clazz">
        <property name="cid" value="0002"></property>
        <property name="cname" value="A班"></property>
        <property name="students" ref="studentList"></property>
    </bean>
        <!--配置一个集合类型的bean,需要使用util的约束-->
    <util:list id="studentList">
        <ref bean="studentOne"></ref>
        <ref bean="studentTwo"></ref>
        <ref bean="studentThree"></ref>
    </util:list>

 7. 依赖注入—为map集合属性赋值

<util:map id="teacherMap">
     <entry key="10086" value-ref="teacherOne"></entry>
     <entry key="10010" value-ref="teacherTwo"></entry>
</util:map>
或者内部属性设置:
<property name="teacherMap">
     <map>
          <entry key="10086" value-ref="teacherOne"></entry>
          <entry key="10010" value-ref="teacherTwo"></entry>
     </map>
</property>

<bean id="teacherOne" class="com.pojo.Teacher">
     <property name="tid" value="10086"></property>
     <property name="tname" value="移动"></property>
</bean>
<bean id="teacherTwo" class="com.pojo.Teacher">
     <property name="tid" value="10010"></property>
     <property name="tname" value="联通"></property>
</bean>

每个属性都有对应的两个属性:一个带ref,一个不带ref。

前者表示给字面量属性赋值(如:id,name等),后者表示给类类型属性引用某一bean的id。

8. spring管理数据源

导入依赖

<dependencies>
<!-- MySQL驱动 -->
  <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>8.0.16</version>
  </dependency>
<!-- 数据源 -->
  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.31</version>
  </dependency>
</dependencies>

jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456

spring配置

    <!--引入jdbc.properties,之后可以通过${key}的方式访问value-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

测试:

public class DataSourceTest {
    @Test
    public void testDataSource() throws SQLException {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-datasource.xml");
        DruidDataSource dataSource = ioc.getBean(DruidDataSource.class);
        System.out.println(dataSource.getConnection());
    }
}

 

posted @   浑浑噩噩一只小迷七  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 快收藏!一个技巧从此不再搞混缓存穿透和缓存击穿
· Blazor Hybrid适配到HarmonyOS系统
· 支付宝 IoT 设备入门宝典(下)设备经营篇
· 万字调研——AI生成内容检测
· 解决跨域问题的这6种方案,真香!
点击右上角即可分享
微信分享提示