SSH与SSM学习之Spring07——Spring属性注入之spel注入

  • SSH与SSM学习之Spring07Spring属性注入之spel注入
    • 一spel注入
    • 二Car类
    • 三User类
    • 四配置文件
    • 五测试代码
    • 六运行结果
    • 七源码下载

SSH与SSM学习之Spring07——Spring属性注入之spel注入

一、spel注入

spel注入: spring Expression Language 。sping表达式语言

其实就是使用

#{变量}

这种来个属性赋值。

例如

<bean name="user7" class="com.qwm.spring1.bean.User">
    <property name="name" value="#{user2.name}"/>
    <property name="age" value="#{user2.age}"/>
    <property name="car" ref="car"/>
bean>

下面我们来测试

使用到了 User 和Car 类。


二、Car类


package com.qwm.spring1.bean;
/**
 * @author: wiming
 * @date: 2017-09-27 14:59:50  星期三
 * @decription:
 */
public class Car {
    private String color;
    private String name;

    public String getColor() { return color; }
    public void setColor(String color) { this.color = color; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

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

三、User类

package com.qwm.spring1.bean;

import java.io.Serializable;

/**
 * @author: wiming
 * @date: 2017-09-25 14:45:56  星期一
 * @decription:
 */
public class User implements Serializable {
    private String name;
    private int age;
    private Car car;

    public User() {
        System.out.println("User实例化了----" + this);
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("构造方法: User(String name, int age)");
    }

    public User(int name, int age) {
        this.name = name + "";
        this.age = age;
        System.out.println("构造方法: User(int name, int age)");
    }

    public User(int age, String name) {
        this.name = name;
        this.age = age;
        System.out.println("构造方法: User(int age, String name)");
    }

    public User(String name, int age, Car car) {
        this.name = name;
        this.age = age;
        this.car = car;
        System.out.println("构造方法: User(String name, int age, Car car)");
    }

    public void init() { System.out.println("---User---init---"); }
    public void destory() { System.out.println("---User---destory---"); }

    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 Car getCar() { return car; }
    public void setCar(Car car) { this.car = car; }

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

四、配置文件


<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">

    
    <bean name="car" class="com.qwm.spring1.bean.Car">
        
        <property name="name" value="兰博基尼"/>
        <property name="color" value="红色"/>
    bean>

    
    
    
    
    
    
    <bean name="user2" class="com.qwm.spring1.bean.User">
       <constructor-arg name="name" value="qiwenming"/>
       <constructor-arg name="age" value="18"/>
       <property name="car" ref="car"/>
    bean>


    
   
    <bean name="user7" class="com.qwm.spring1.bean.User">
        <property name="name" value="#{user2.name}"/>
        <property name="age" value="#{user2.age}"/>
        <property name="car" ref="car"/>
    bean>
beans>

五、测试代码

@Test
public void test7(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("com/qwm/spring1/c_inject/applicationContext.xml");
    User user = (User) ac.getBean("user7");
    System.out.println(user);
}

六、运行结果

构造方法: User(int age, String name)
User实例化了----User{name='null', age=0, car=null}
User{name='qiwenming', age=18, car=Car{color='红色', name='兰博基尼'}}

七、源码下载

https://github.com/wimingxxx/spring01/tree/master/src/com/qwm/spring1/c_inject

你可能感兴趣的:(SSH与SSM学习)