3、Spring入门配置级DL依赖注入

5、Spring入门配置

5.1、起别名

给项目起别名

![]

5.2、import

导入其他xml

5.3、Bean的配置

  • 最重要的,又很多配置,我们先学一点

6、依赖注入(DL)

很重要

6.1、set注入

三种方式:

  • set注入(重要)
<?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">
<bean id="student" class="bing.pojo.Student">
    <!--    Set注入-->
    <property name="name" value="小冰"/>
<!--    引用注入,address-->
    <property name="address" ref="address"/>
<!--    数组注入-->
    <property name="advantages">
        <array>
            <value>帅</value>
            <value>情商高</value>
            <value>智慧</value>
            <value>沉稳</value>
            <value>有钱</value>
        </array>
    </property>
<!--    set-->
    <property name="course">
        <set>
            <value>赚钱</value>
            <value>情商</value>
            <value>心理学</value>
            <value>经济学</value>
            <value>哲学</value>
            <value>英语</value>
            <value>数学</value>
            <value>计算机</value>
        </set>
    </property>
<!--    map注入-->
<property name="grades">
    <map>
        <entry key="java" value="10000"/>
        <entry key="math" value="200"/>
        <entry key="English" value="300"/>
        <entry key="psychology" value="400"/>
    </map>
</property>
</bean>
    <bean id="address" class="bing.pojo.Address">
        <property name="address" value="大亳州"/>
    </bean>

</beans>

记住这些类型的注入就行了

6.2、构造器注入

<?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">
<bean id="user" class="bing.User">
<!--    第一种方式:通过index获取-->
    <constructor-arg index="0" value="bingbing"/>
</bean>

    <bean id="student" class="bing.Student">
<!--        方式二:通过key value-->
        <constructor-arg name="gender" value="小仙女"></constructor-arg>
        <constructor-arg name="age" value="19"/>
    </bean>

    <bean id="student2" class="bing.Student">
<!--        方式三:通过 类型获取,不推荐可能会出现歧义比如两个String-->
<!--        这里以及上面为什么使用全类名? 肯定用到了反射-->
        <constructor-arg type="java.lang.String" value="女"/>
        <constructor-arg type="int" value="18"/>
    </bean>

<bean id="teacher" class="bing.Teacher">
<!--    我们来试一下两个String类型会发生什么情况-->
    <constructor-arg type="java.lang.String" value="girl"/>
    <constructor-arg type="java.lang.String" value="刘老师"/>
</bean>

<!--    相当于new对象,对象名为person,只有这一个对象-->
    <bean id="person" class="bing.Person">

    </bean>

</beans>

注意:我们一般选用 key value注入,见名知意

6.3、拓展注入

为了简化我们可以引入p/c命名空间

使用方式:

<?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,命名空间,p本质上是什么? 就是properties,可以给简单的属性命名,目的是为了简化-->
<bean id="user" class="bing.pojo.User" p:name="zhangsan" p:age="18">

</bean>
    <!--    c命名空间,怎么用?  是给constructor 构造器初始化的,这里就要求必须要有有参构造-->
    <bean id="user2" class="bing.pojo.User" c:name="bingbing" c:age="19" p:age="20"/>
</beans>

注意点:

使用前要导入:

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
  • 类比:

p相当于标签:properties 其实就是set注入,不过可以简化简单的操作

c详单与 :constructor:就是用来给有参构造器初始化的

6.4、bean标签作用域

bean是什么?就是一个对象,也就是类的实例

我们可以给他设置单例模式等等

  • 单例模式
<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
  • 原型模式
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
posted @ 2020-05-26 22:49  贝加尔湖畔╭  阅读(310)  评论(0)    收藏  举报