Spring(一)Spring配置、构造注入、bean作用域、bean自动装配

x33g5p2x  于2022-07-06 转载在 Spring  
字(5.5k)|赞(0)|评价(0)|浏览(277)

1 简介

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html

4 IOC创建对象的方式

ApplicationContext.xml

  1. 默认使用无参构造对象
<bean id="user" class="com.steak.pojo.User">
    <property name="name" value="有勇气的牛排"/>
</bean>
  1. 假使我们要使用有参构造创建对象

(1) 下标赋值

<!-- 1.有参构造-下标赋值 -->
<bean id="user" class="com.steak.pojo.User">
    <constructor-arg index="0" value="有勇气的牛排1"/>
</bean>

(2) 类型

<!-- 2.有参构造-参数类型匹配  不推荐使用-->
<bean id="user" class="com.steak.pojo.User">
    <constructor-arg type="java.lang.String" value="有勇气的牛排2"/>
</bean>

(3) 参数名

<!-- 3.有参构造-直接通过参数名-->
<bean id="user" class="com.steak.pojo.User">
    <constructor-arg name="name" value="有勇气的牛排3" />
</bean>

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了

仓库地址:
https://gitee.com/courage_steak/steak_java_spring/tree/master/spring-03-ioc2

5 Spring配置

5.1 别名

ApplicationContext.xml

<!-- 设置别名 -->
<alias name="user" alias="uasr_a"/>

5.2 Bean的配置

<!--
    id: bean的唯一标识符,类似于对象名
    class: bean对象所对应的全限定名:包名+类型
    name: 也是别名,可以同时取多个别名
-->
<bean id="userT" class="com.steak.pojo.UserT" name="user_2 u2,u3;u4">
    <!-- <bean id="userT" class="com.steak.pojo.UserT"> -->
    <property name="name" value="大佬"/>
</bean>

5.3 import

一般用于团队开发使用,它可以将多个配置文件导入合并为一个。
别名相同,会合并

<?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">

    <!-- 导入 -->
    <import resource="beans1.xml"/>

</beans>

仓库地址:
https://gitee.com/courage_steak/steak_java_spring/tree/master/spring-03-ioc2/src/main/resources

6 依赖注入

6.1 构造器注入

6.2 Set方式注入(重点)

依赖注入(Set注入)

  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中的所有属性,由容器来注入
<!-- 2. Bean注入, ref -->
<bean id="address" class="com.steak.pojo.Address">
    <property name="address" value="西安"/>
</bean>

<bean id="student" class="com.steak.pojo.Student">
    <!-- 1. 普通依赖注入 -->
    <property name="name" value="有勇气的牛排"/>

    <!-- 2. Bean注入, ref引用注入 -->
    <property name="address" ref="address"/>

    <!-- 3. 数组注入, ref -->
    <property name="books">
        <array>
            <value>遥远的救世主</value>
            <value>背叛</value>
            <value>天幕红尘</value>
        </array>
    </property>

    <!-- 4. List -->
    <property name="hobbys">
        <list>
            <value>听音乐</value>
            <value>看书</value>
            <value>打篮球</value>
        </list>
    </property>

    <!-- 4. Map -->
    <property name="card">
        <map>
            <entry key="身份证" value="123456"/>
            <entry key="银行卡" value="234561"/>
        </map>
    </property>

    <!-- 5. Set -->
    <property name="games">
        <set>
            <value>QQ飞车</value>
            <value>贪吃蛇</value>
            <value>坦克大战</value>
        </set>
    </property>

    <!-- 5. null -->
    <property name="wife">
        <null/>
    </property>

    <!-- 6. Properties -->
    <property name="info">
        <props>
            <prop key="学号">20220625</prop>
            <prop key="性别">男</prop>
            <prop key="姓名">导演</prop>
        </props>
    </property>

</bean>

https://gitee.com/courage_steak/steak_java_spring/tree/master/spring-04-di

6.3 拓展方式

  1. p命名 和 c命名
<!-- p命名 -->
xmlns:p="http://www.springframework.org/schema/p"
<!-- c命名 -->
xmlns:c="http://www.springframework.org/schema/c"

user_beans.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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- p命名空间注入,可以直接注入属性的值 -->
    <bean id="user" class="com.steak.pojo.User" p:name="导演"/>

    <!-- c命名空间注入,通过构造器注入(有参):construct-args -->
    <bean id="user2" class="com.steak.pojo.User" c:age="18" c:name="导演"/>

</beans>

User.java

public class User {
    private String name;
    private int age;

    // 无参构造
    public User() {

    }

    // 有参构造
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    ...
}

MyTest.java

@Test
public void test2(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("user_beans.xml");

    // User user =context.getBean("user",User.class);
    User user =(User) context.getBean("user");

    System.out.println(user.toString());
}

6.4 bean的作用域

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes

  1. 单例模式(Spring默认机制):singleton
<bean id="user2" class="com.steak.pojo.User" c:age="18" c:name="导演" scope="singleton"/>
  1. 原型模式:prototype

每次从容器中get的时候,都会产生一个新对象

<bean id="user2" class="com.steak.pojo.User" c:age="18" c:name="导演" scope="prototype"/>
  1. 其余的request、session、application,只能在web开发中使用

参考地址:
https://www.bilibili.com/video/BV1WE411d7Dv

相关文章