【Spring学习笔记】2.IOC入门Ⅱ

1.Spring当中的各种值的注入

  1. 数组
  2. List
  3. Set
  4. Map 

如果其对应的值是简单的字面值,直接写就可以了,如果是一个其他类,那么使用内部bean的方式完成

people.java

public class People {

    private String name;
    private Integer age;
    private String[] friends;
    private List<Integer> nums;
    private List<Cat> cats;
    private Set<Pig> pig;
    private Map<String,User> users;
//getter and setter
}

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

    <bean class="com.cyk.pojo.People" id="people">
        <property name="name" value="cyk"/>
        <property name="age" value="20"/>

        <!--<property name="friends" value="abc def"/>  不科学-->

        <property name="friends">
            <array>
                <value>123</value>
                <value>456</value>
                <value>789</value>
                <value>011</value>
            </array>
        </property>

        <property name="nums">
            <list>
                <value>8</value>
                <value>7</value>
            </list>
        </property>

        <property name="cats">
            <list>
                <!--内部bean,无法被外部所引用,所以无需id-->
                <bean class="com.cyk.pojo.Cat">
                    <property name="leg" value="4"/>
                    <property name="skin" value="blue"/>
                </bean>
                <bean class="com.cyk.pojo.Cat">
                    <property name="leg" value="4"/>
                    <property name="skin" value="yellow"/>
                </bean>
            </list>
        </property>

        <property name="pig">
            <set>
                <bean class="com.cyk.pojo.Pig">
                    <property name="name" value="佩奇"/>
                    <property name="sleep" value="88"/>
                    <property name="taste" value="香辣"/>
                </bean>
                <bean class="com.cyk.pojo.Pig">
                    <property name="name" value="小宝"/>
                    <property name="sleep" value="99"/>
                    <property name="taste" value="酱香"/>
                </bean>
            </set>
        </property>

        <property name="users">
            <map>
                <entry key="user1">
                    <bean class="com.cyk.pojo.User">
                        <property name="name" value="韩雪"/>
                        <property name="address" value="梧桐村"/>
                    </bean>
                </entry>
                <entry key="user2">
                    <bean class="com.cyk.pojo.User">
                        <property name="name" value="林青霞"/>
                        <property name="address" value="台湾"/>
                    </bean>
                </entry>
            </map>
        </property>
    </bean>
</beans>

Test.java

@Test
    public void m1(){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        People people = ctx.getBean("people", People.class);

        System.out.println(Arrays.toString(people.getFriends()));
        System.out.println(people.getFriends().length);
        System.out.println(people.getNums());
        System.out.println(people.getCats());
        System.out.println(people.getPig());
        System.out.println(people.getUsers());
    }

 

2.自动注入

  1. byType:按照数据类型注入
  2. byName:按照bean对应的pojo里面的属性的名字来进行匹配
  3. byConstructor:优先按照类型去匹配,如果匹配到一个那么直接注入,不止一个按照名字注入,如果一个都找不到,注入失败。
  4. default:
  5. no:不注入

user.java

public class User {
    private String name;
    private String address;
    private Pig pig;
//getter and setter
}

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

    <!--<bean class="com.cyk.pojo.User" id="user" autowire="byType">-->
        <!--<property name="name" value="陈慧琳"/>-->
        <!--<property name="address" value="香港"/>-->
    <!--</bean>-->

    <bean class="com.cyk.pojo.User" autowire="constructor" id="user">
        <constructor-arg name="name" value="韩红"/>
    </bean>

    <!--<bean class="com.cyk.pojo.User" id="user" autowire="default">-->
        <!---->
    <!--</bean>-->

    <bean class="com.cyk.pojo.Pig" name="pig2">
        <property name="name" value="巨大宝"/>
    </bean>

    <bean class="com.cyk.pojo.Pig" name="pig">
        <property name="name" value="大宝"/>
    </bean>

    <bean class="com.cyk.Service.ProviderService" id="providerService" autowire="byType">

    </bean>

    <bean class="com.cyk.dao.ProviderDao" id="providerDao">

    </bean>

</beans>

Test.java

@Test
    public void m2(){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("autowired.xml");
        User user = ctx.getBean("user", User.class);

        System.out.println(user.getPig().getName());
    }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值