美文网首页
spring-SpEL

spring-SpEL

作者: 风雪_夜归人 | 来源:发表于2023-11-15 20:52 被阅读0次

    Spring管理对象的DI注入

    • 管理对象属性的注入: 基本类型 int float boolean ... String
    • 赋值同时做一些运算
      1.算数: +、-、*、/、%等 ^在spring中是幂运算
      2.比较 > 、>=、 < 、<=、 !=、 == ; gt、 ge、 lt、 le、 ne、 eq
      3.赋值的时候可能遇到对象的形式

      {beanId} or #{beanName}

      可以操作对象 操作对象的属性 和对象的方法
    • 配置文件如下:
    <?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:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean name="logo" class="domain.Logo" c:name="huawei" c:color="银色">
    
        </bean>
        <bean id="computer" class="domain.Computer">
    <!--        <constructor-arg name="name" value="#{'mi'}"></constructor-arg>-->
            <constructor-arg name="price" value="#{20000.22}"></constructor-arg>
            <constructor-arg name="siez" value='#{((2 * 4) ^ 2 -1 * 4 + 4 * 2)/2}'></constructor-arg>
    <!--        <constructor-arg name="status" value="#{true}"></constructor-arg>-->
    <!--        <constructor-arg name="status" value="#{(10 lt 20)}"></constructor-arg>  < -->
    <!--        <constructor-arg name="status" value="#{(10 gt 20)}"></constructor-arg>  > -->
    <!--        <constructor-arg name="status" value="#{(10 ge 20)}"></constructor-arg>  >= -->
    <!--        <constructor-arg name="status" value="#{(10 le 20)}"></constructor-arg>  <= -->
    <!--        <constructor-arg name="status" value="#{(10 eq 20)}"></constructor-arg>  ==-->
    <!--        <constructor-arg name="status" value="#{30 ne 20}"></constructor-arg>    !=-->
    <!--        <constructor-arg name="status" value="#{not (1>2)}"></constructor-arg>-->
            <constructor-arg name="status" value="#{true and (12>2)}"></constructor-arg>
            <constructor-arg name="logo" value="#{logo}"></constructor-arg>
            <constructor-arg name="name" value="#{logo.test()}"></constructor-arg>
            
        </bean>
    </beans>
    

    代码如下:
    Computer 类

    public class Computer {
        private String name;
        private Float price;
        private int siez;
        private boolean status;
        private Logo logo;
    
        public Computer(String name, Float price, int siez, boolean status, Logo logo) {
            this.name = name;
            this.price = price;
            this.siez = siez;
            this.status = status;
            this.logo = logo;
        }
    
        public Computer(){}
    
    
        @Override
        public String toString() {
            return "Computer{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    ", siez=" + siez +
                    ", status=" + status +
                    ", logo=" + logo +
                    '}';
        }
    }
    

    Logo类

    public class Logo {
        private String name;
        private String color;
    
        public Logo(String name, String color) {
            this.name = name;
            this.color = color;
        }
    
        @Override
        public String toString() {
            return "Logo{" +
                    "name='" + name + '\'' +
                    ", color='" + color + '\'' +
                    '}';
        }
    
        public String test(){
            System.out.println("test");
            return "test";
        }
    }
    

    相关文章

      网友评论

          本文标题:spring-SpEL

          本文链接:https://www.haomeiwen.com/subject/ixidwdtx.html