spring的属性注入和构造器注入

spring在向IOC容器中注入Bean的时候,有三种注入方式:

  • 属性注入
  • 构造器注入
  • 工厂方法注入
    平常中用到的前两种方法较多,下面对前两种方法举例。

一、属性注入

1、创建一个car类,作为注入的bean

package com.lzj.spring;
public class Car {

    private String brand;
    private float price;

    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price + "]";
    }

}

注意:如果要把定义的类通过xml的属性注入IOC容器中,定义的类一定不要定义有参的构造器,否则会提示没有默认的无参构造器错误。属性注入是通过Car类的set方法进行注入的,因此一定要对类的属性定义set方法。
2、配置bean.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="car" class="com.lzj.spring.Car">
        <property name="brand" value="baoma"></property>
        <property name="price" value="100000"></property>
    </bean>

</beans>

bean标签中的class一定要写成全类名的形式,因为spring是根据类的全类名进行反射获取类的。在进行属性注入的时候,由于Car类有两个属性brand和price,所以定义两个property标签,name分别为Car类的两个属性,值分别为初始化属性的值。通过属性注入后,在IOC容器中就有了一个Bean为car的bean,即IOC容器中有一个Car类的实例car,实例的两个属性值分别为baoma和100000。
3、测试类

package com.lzj.springtest;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lzj.spring.Car;

public class SpringTest {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext ctx = 
                new ClassPathXmlApplicationContext("bean.xml");

        /*1、在容器中可以通过bean的id名字获取bean,但是获取到的bean默认为object类型,需要转型为Car类型*/
        //Car car = (Car) ctx.getBean("car");
        /*2、在容器中也可以通过类的名字获取bean,但前提是容器只有一个指定类的bean,否则出错:没有唯一的bean*/
        Car car = ctx.getBean(Car.class);
        System.out.println(car);
    }

}

经测试,Console中输出:
Car [brand=baoma, price=100000.0]

二、构造器注入

1、创建Person类

package com.lzj.spring;
public class Person {

    private String name;
    private int age;
    private String sex;
    public Person(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }

}

由于需要把Person类进行构造器注入到IOC容器中,所以不需要定义set方法,但要定义有参构造器,因为spring是通过Person的构造器进行初始化的。
2、配置bean.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="person" class="com.lzj.spring.Person">
        <!--注意constuctor的顺序是按Person类的构造器的初始化顺序-->
        <constructor-arg value="lzj"></constructor-arg>
        <constructor-arg value="20"></constructor-arg>
        <constructor-arg value="female"></constructor-arg>
    </bean>

</beans>

在进行构造器注入的时候,也可以写成

<bean id="person" class="com.lzj.spring.Person">
        <constructor-arg value="lzj" index="0"></constructor-arg>
        <constructor-arg value="20" index="1"></constructor-arg>
        <constructor-arg value="female" index="2"></constructor-arg>
    </bean>

用index来定义构造器中参数的顺序。也可以通过属性的类型进行定义。例如:

    <bean id="person" class="com.lzj.spring.Person">
        <constructor-arg value="lzj" type="java.lang.String"></constructor-arg>
        <constructor-arg value="20" type="int"></constructor-arg>
        <constructor-arg value="female" index="2"></constructor-arg>
    </bean>

也可以通过属性名进行注入值,例如:

<bean id="person" class="com.lzj.spring.Person">
        <constructor-arg value="lzj" type="java.lang.String"></constructor-arg>
        <constructor-arg value="20" type="int"></constructor-arg>
        <!--也可以通过属性名进行注入值-->
        <constructor-arg name="sex" value="female"></constructor-arg>
    </bean>

3、测试类

public class SpringTest {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext ctx = 
                new ClassPathXmlApplicationContext("bean.xml");
        Person person = ctx.getBean(Person.class);
        System.out.println(person);
    }

}

输出内容为:

Person [name=lzj, age=20, sex=female]

以上即为属性输入和构造器注入的区别。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值