spring依赖注入

一·spring的简单配置

可以用alias标签配置别名,在外部可以通过别名获取相同的对象。

<bean id="hello" class="com.he.pojo.Hello">
        <property name="str" value="spring"></property>
    </bean>
    <alias name="hello" alias="hello6"></alias>

也可以直接使用bean标签里的name属性来配置别名,达到相同的效果。 

 <bean id="hello7" class="com.he.pojo.Hello" name="hello8">
        <property name="str" value="spring!!!"></property>
    </bean>

import标签导入外部xml文件

一般用于团队开发中,将多个beans.xml合并成一个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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="beans.xml"/>

</beans>

二·集合注入

1.搭建环境:

Address实体类:

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

Girlfriend实体类:

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "GirlFriend{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Student实体类:

public class Student {
    private String name;
    private Address address;
    private String[] hobbies;
    private List<String> books;
    private Map<String,String> cards;
    private Set<String> games;
    private Properties info;
    private String wife;
    private List<GirlFriend> girlFriends;

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public List<GirlFriend> getGirlFriends() {
        return girlFriends;
    }

    public void setGirlFriends(List<GirlFriend> girlFriends) {
        this.girlFriends = girlFriends;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getHobbies() {
        return hobbies;
    }

    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }

    public List<String> getBooks() {
        return books;
    }

    public void setBooks(List<String> books) {
        this.books = books;
    }

    public Map<String, String> getCards() {
        return cards;
    }

    public void setCards(Map<String, String> cards) {
        this.cards = cards;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", hobbies=" + Arrays.toString(hobbies) +
                ", books=" + books +
                ", cards=" + cards +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                ", girlFriends=" + girlFriends +
                '}';
    }
}

2.xml中配置对象:

普通类型以及引用类型注入:

 <bean id="address" class="com.he.pojo.Address" name="地址">
        <property name="address" value="武汉"></property>
    </bean>
    <bean id="student" class="com.he.pojo.Student">
        <property name="name" value="何志康"></property>
        <property name="address" ref="地址"></property>
    </bean>

数据注入:

    <!--数组注入-->
        <property name="hobbies">
            <array>
                <value>听歌</value>
                <value>唱歌</value>
                <value>敲代码</value>
                <value>姐姐</value>
            </array>
        </property>

list注入:

<!-- list注入-->
        <property name="books">
            <list>
                <value>java</value>
                <value>myBatis</value>
                <value>spring</value>
            </list>
        </property>

map注入:

        <!--map注入-->
        <property name="cards">
            <map>
                <entry key="身份证" value="178278782782"></entry>
                <entry key="银行卡" value="6287272877827"></entry>
            </map>
        </property>

set注入: 

        <!--set注入-->
        <property name="games">
            <set>
                <value>QQ飞车</value>
                <value>和平精英</value>
                <value>王者荣耀</value>
            </set>
        </property>

properties注入:

    <!--properties注入-->
        <property name="info">
            <props>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
                <prop key="url">https://www.baidu.com</prop>
            </props>
        </property>

null值注入:

  <!--null值注入-->
        <property name="wife">
            <null/>
        </property>

list注入对象:

<!--list注入对象-->
        <property name="girlFriends">
            <list>
                <ref bean="girlFriend1"/>
                <ref bean="girlFriend2"/>
                <ref bean="girlFriend3"/>
            </list>
        </property>

    <bean id="girlFriend1" class="com.he.pojo.GirlFriend">
        <property name="name" value="姐姐1"></property>
        <property name="age" value="20"></property>
    </bean>
    <bean id="girlFriend2" class="com.he.pojo.GirlFriend">
        <property name="name" value="姐姐2"></property>
        <property name="age" value="19"></property>
    </bean>
    <bean id="girlFriend3" class="com.he.pojo.GirlFriend">
        <property name="name" value="姐姐3"></property>
        <property name="age" value="21"></property>
    </bean>

inner beans :

在一个bean的内部写一个bean作为属性,称为inner beans,与ref标签引用外部的bean效果相同!

<property name="girlFriends">
            <list>
                <ref bean="girlFriend1"/>
                <ref bean="girlFriend2"/>
                <ref bean="girlFriend3"/>

                <bean class="com.he.pojo.GirlFriend">
                    <property name="name" value="姐姐4"></property>
                    <property name="age" value="22"></property>
                </bean>
            </list>
        </property>

3.P命名空间,C命名空间:

都是p:或c:的形式

不能直接使用,需要导入相应的xml配置:

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

P的使用(相当于property标签):

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

    <bean name="classic" class="com.example.ExampleBean">
        <property name="email" value="someone@somewhere.com"/>
    </bean>

    <bean name="p-namespace" class="com.example.ExampleBean"
        p:email="someone@somewhere.com"/>
</beans>

C的使用(相当于构造器):

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

    <bean id="beanTwo" class="x.y.ThingTwo"/>
    <bean id="beanThree" class="x.y.ThingThree"/>

    <!-- traditional declaration with optional argument names -->
    <bean id="beanOne" class="x.y.ThingOne">
        <constructor-arg name="thingTwo" ref="beanTwo"/>
        <constructor-arg name="thingThree" ref="beanThree"/>
        <constructor-arg name="email" value="something@somewhere.com"/>
    </bean>

    <!-- c-namespace declaration with argument names -->
    <bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
        c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>

</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值