Spring的属性注入, byName和byType还有注入List属性

  昨天花了一晚上的时间又仔细研究了一下Spring的属性注入, 一个新的方法: 自动装载和autowire, 不过因为又想起来老师说不常用, 感觉这一晚上的时间有点亏, 还是自己太愚钝了, 反应太慢

  先贴代码

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- setter方法的属性注入 -->
    <bean id="hw" class="com.hanqi.HelloWorld">
        <property name="mingzi" value="长颈鹿"></property>
        <property name="c" ref="computer"></property>
    </bean>
    
    <!-- bean的id的值要与实体类中set方法后面的名字是一致的才能自动装载 <bean id="user" class="com.hanqi.User"> <property name="name" value="企鹅"></property> <property name="age" value="13"></property> <property name="sex" value="男"></property> <property name="minz" value="黎族"></property> </bean>-->
    
    <!-- 自动装配 -->
    <!-- byName -->
    <bean id="hw1" autowire="byName" class="com.hanqi.HelloWorld">
        <property name="mingzi" value="金丝猴"></property>
        <property name="dizhi" value="山东淄博"></property>
    </bean>
    <!-- <bean id="c" class="com.hanqi.Computer"> <property name="box" value="丽声音响"></property> <property name="cpu" value="Intel core-i7"></property> </bean> -->
    <!-- byType -->
    <bean id="hw2" autowire="byType" class="com.hanqi.HelloWorld">
        <property name="mingzi" value="李明"></property>
        <property name="dizhi" value="山东淄博张店"></property>
    </bean>
    
    <!-- p标签来进行注入属性 -->
    <bean id="hw3" class="com.hanqi.HelloWorld" p:c-ref="computer" p:user-ref="useruser">
        <property name="mingzi" value="向东整形医院"></property>
        <property name="dizhi" value="华光路不知道多少号"></property>
    </bean>
    <bean id="computer" class="com.hanqi.Computer" p:cpu="core-i5" p:box="铁三角"></bean>
    <bean id="useruser" class="com.hanqi.User" p:name="纸巾" p:age="29" p:minz="人族" p:sex="男"></bean>
    
    <!-- 实例化模式scope="singleton/prototype" -->
    <bean id="hw4" scope="singleton" class="com.hanqi.HelloWorld" p:c-ref="computer" p:user-ref="useruser">
        <property name="mingzi" value="向东整形医院"></property>
        <property name="dizhi" value="华光路不知道多少号"></property>
    </bean>
    
    <!-- 注入List属性 -->
    <bean id="hw5" scope="prototype" class="com.hanqi.HelloWorld">
        <property name="mingzi" value="昌国医院"></property>
        <property name="dizhi" value="309国道"></property>
        <property name="carlist">
            <list><ref bean="car"></ref></list>
        </property>
    </bean>
    <bean id="car" class="com.hanqi.Car" p:fadongji="潍柴" p:luntai="三角"></bean>
</beans>

User类

package com.hanqi; public class User { private int age; private String sex; private String minz; private String name; public User() {} public User(String name, String sex, int age, String minz) { super(); this.name = name; this.age = age; this.sex = sex; this.minz = minz; } 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; } public String getMinz() { return minz; } public void setMinz(String minz) { this.minz = minz; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "\n[name=" + name + ", age=" + age + ", sex=" + sex + ", minz=" + minz + "]"; } } 
View Code

Computer类

package com.hanqi; public class Computer { private String cpu; private String box; public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getBox() { return box; } public void setBox(String box) { this.box = box; } @Override public String toString() { return "\n[cpu=" + cpu + ", box=" + box + "]"; } }
View Code

HelloWorld类

package com.hanqi; import java.util.List; public class HelloWorld { private String mingzi; private String dizhi; private User user; private Computer c; private List<Car> carlist; public void sayHello() { System.out.println("Hello "+mingzi+c+"\n"+user+"\n"+dizhi); //System.out.println("Hello "+mingzi+user);
        for(Car car : carlist) { System.out.println(car); } } //属性mingzi的set和get方法
    public void setMingzi(String mingzi) { this.mingzi = mingzi; } public String getMingzi() { return mingzi; } //再定义一个属性的getter和setter方法
    public String getDizhi() { return dizhi; } public void setDizhi(String dizhi) { this.dizhi = dizhi; } //定义属性user的get和set方法
    public User getUser() { return user; } public void setUser(User user) { this.user = user; } //定义Computer的setter和getter方法
    public Computer getC() { return c; } public void setC(Computer c) { this.c = c; } //定义Car的setter和getter方法
    public List<Car> getCarlist() { return carlist; } public void setCarlist(List<Car> carlist) { this.carlist = carlist; } }
View Code

Car类

package com.hanqi; public class Car { private String luntai; private String fadongji; public String getLuntai() { return luntai; } public void setLuntai(String luntai) { this.luntai = luntai; } public String getFadongji() { return fadongji; } public void setFadongji(String fadongji) { this.fadongji = fadongji; } @Override public String toString() { return "Car [luntai=" + luntai + ", fadongji=" + fadongji + "]"; } }
View Code

整个过程是这样的, 大体上猜测了一下Spring的工作过程, 感觉这个东西真的越来越神秘了, 之前所学的都是setter方法的属性注入, 这个还是相当简单的

    <bean id="hw" class="com.hanqi.HelloWorld">
        <property name="mingzi" value="长颈鹿"></property>
        <property name="c" ref="computer"></property>
    </bean>
    
    <bean id="user" class="com.hanqi.User">
        <property name="name" value="企鹅"></property>
        <property name="age" value="13"></property>
        <property name="sex" value="男"></property>
        <property name="minz" value="黎族"></property>
    </bean>

定义一个bean, 将一个实体类赋值给这个bean, 然后在调用的时候给ref属性就好了

自动装配

  如果使用自动装配就在bean标签中加上一个autowire属性, 这个属性用的相对来讲比较多的是byName或是byType, 而在规则定义上, 说法还是很多的, 我从网上查了不少资料, 但是没有说的很详细的, 自己试了一下, 发现这个自动装配还是按照set方法后面的部分来定义是否该装配进去, 而且也不在乎大小写, 我试了很多, 具体代码不写了, 篇幅太长, 整个工作过程的话我猜测的是这样的(个人观点, 欢迎指正): 

byName

  在执行<bean id="hw" class="com.hanqi.HelloWorld">这一句的时候, Spring框架将HelloWorld这个实体类整个编译一下, 看看自己的这个类里面有几个属性(类型是实体类的属性, 比如User user), 然后对着ApplicationContext的配置文件一通对比, 如果有bean的id能跟自己的setter方法对上名字, 就把这个bean装载到自己的这个类里面

byType

  基本上是一样的道理, 但是稍有不同的是, byType是根据类型来的, 大多数在实际使用当中, 也就是使用的一个实体类(大多数), 就上面的这个例子来说, Spring先看看HelloWorld这个类里面都有什么属性, 有Computer, 也有User, 还有Car, 于是就开始按照这三个类找ApplicationContext配置文件里面有没有这样的类型, 找到了就装载上, 但是这里要注意一个问题, byType不能装在同一个类型的实体类, 比如这样的两个bean: 

<bean id="user1" class="com.hanqi.User" p:name="纸巾" p:age="29" p:minz="人族" p:sex="男"></bean>

<bean id="user2" class="com.hanqi.User">
    <property name="name" value="企鹅"></property>
    <property name="age" value="13"></property>
    <property name="sex" value="男"></property>
    <property name="minz" value="黎族"></property>
</bean>

  不管你怎么写, 这两个bean中的class指向的都是同一个类, 这时候你再使用byType就会报错了

  在实际使用中, byName要比byType使用的多, 可是我觉得吧, 还是觉得byType好用, 因在实际使用中, 实体类同时做成两个bean的时候应该不多吧(没有经验, 这也是猜的), 使用byName如果有一个别的bean跟这个名字对上了, 那就复杂了, 连后期的维护也比较困难.

  不管怎么说, 还是老老实实使用setter方法的手动注入最靠谱!

你可能感兴趣的:(Spring的属性注入, byName和byType还有注入List属性)