zoukankan      html  css  js  c++  java
  • 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]

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

  • 相关阅读:
    powershel学习(1)
    JS作用域链(转载)
    C# 对QuotedPrintable进行解码的方法
    SortedList、SortedSet、HashSet、Hashtable、Dictionary、SortedDictionary 排序/可重复排序/过滤重复排序等简单对比
    .net windows 服务中返回服务的安装目录
    c# 中图像的简单二值化处理
    windows下开多个CMD窗口多个进程输出
    生命游戏
    PowerDesigner工具简介
    七大面向对象设计原则(转)
  • 原文地址:https://www.cnblogs.com/panchangde/p/11684133.html
Copyright © 2011-2022 走看看