• Spring SpEL 各种写法示例


    项目路径

    先说一下三个bean都有哪些属性

    Address.java

    private String city;//城市
    private String street;//街道

    Car.java

    private String brand;//品牌
    private double price;//价格
    private double tyrePerimeter;//轮胎周长

    Person.java

    private String name;//姓名
    private Car car;
    private String city;//引用Address的city属性
    private String info;//若car的价格>30000?金领:白领

    以上bean都有对应的get/set方法和重写的toString方法

    一  使用SpEL表达式写字符串

    applicationContext.xml

        <bean id="address" class="com.tse.beans.Address">
            <!-- 使用SpEl表达式写字符串*意义不大 -->
            <property name="city" value="#{'北京'}"></property>
            <property name="street" value="王府井"></property>
        </bean>

    Main方法中测试

        public static void main(String[] args) {
            ApplicationContext actx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
            Address address = (Address) actx.getBean("address");
            System.out.println(address);
        }

    执行结果

    Address [city=北京, street=王府井]

    二  使用SpEl表达式引用静态变量

    applicationContext.xml中新增bean

    <bean id="car" class="com.tse.beans.Car">
            <property name="brand" value="Audi"></property>
            <property name="price" value="200000"></property>
            <!-- 使用SpEl表达式引用静态变量 -->
            <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"></property>
        </bean>

    Main中测试(基于上一条一起测试)

    public static void main(String[] args) {
            ApplicationContext actx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
            Address address = (Address) actx.getBean("address");
            System.out.println(address);
            Car car = (Car) actx.getBean("car");
            System.out.println(car);
        }

    测试结果

    Address [city=北京, street=王府井]
    Car [brand=Audi, price=200000.0, tyrePerimeter=251.32741228718345]

    三  使用SpEl表达式引用其他bean

          使用SpEl表达式引用其他bean 的属性

          使用SpEl表达式使用运算符

    applicationContext.xml

    <bean id="person" class="com.tse.beans.Person">
            <property name="name" value="Tom"></property>
            <!-- 使用SpEl表达式引用其他bean -->
            <property name="car" value="#{car}"></property>
            <!-- 使用SpEl表达式引用其他bean 的属性-->
            <property name="city" value="#{address.city}"></property>
            <!-- 使用SpEl表达式使用运算符 -->
            <property name="info" value="#{car.price > 300000 ? '金领':'白领'}"></property>
        </bean>

    Main

        public static void main(String[] args) {
            ApplicationContext actx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
            Address address = (Address) actx.getBean("address");
            System.out.println(address);
            Car car = (Car) actx.getBean("car");
            System.out.println(car);
            Person person = (Person) actx.getBean("person");
            System.out.println(person);
        }

    执行结果

    Address [city=北京, street=王府井]
    Car [brand=Audi, price=200000.0, tyrePerimeter=251.32741228718345]
    Person [name=Tom, car=Car [brand=Audi, price=200000.0, tyrePerimeter=251.32741228718345], city=北京, info=白领]

    源码下载链接

    https://download.csdn.net/download/lijian0420/10664813

  • 相关阅读:
    Spring bean相关
    Springboot消除switch-case方法
    Springcloud中的region和zone的使用
    SpringCloud-Eureka-服务注册是如何发起的
    SpringBoot-SpringCloud-版本对应关系
    SpringCloud-Eureka-Provider&Consumer
    激活IDEA
    搞懂spring事务
    部署spring Boot项目到tomcat
    springBoot项目打war包部署到tomcat上
  • 原文地址:https://www.cnblogs.com/gode/p/9641356.html
Copyright © 2020-2023  润新知