16 Spring EL bean引用实例

作者: 笑Skr人啊 | 来源:发表于2017-07-21 11:49 被阅读13次

    在Spring EL,可以使用点(.)符号嵌套属性参考一个bean。例如,“bean.property_name”。

    public class Customer {
        
        @Value("#{addressBean.country}")
        private String country;
    
    

    在上面的代码片段,它从“addressBean” bean注入了“country”属性到现在的“customer”类的“country”属性的值。

    Spring EL以注解的形式

    请参阅下面的例子,演示如何使用使用 SpEL 引用一个bean,bean属性也它的方法。

    package com.gp6.el.bean;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("addressBean")
    public class Address {
        @Value("哈哈,呵呵")
        private String street;
    
        @Value("666666")
        private int postcode;
    
        @Value("CN")
        private String country;
    
        public String getFullAddress(String prefix) {
    
            return prefix + " : " + street + " " + postcode + " " + country;
        }
    
        @Override
        public String toString() {
            return "Address [street=" + street + ", postcode=" + postcode
                    + ", country=" + country + "]";
        }
    
        public String getStreet() {
            return street;
        }
    
    
        public void setStreet(String street) {
            this.street = street;
        }
    
    
        public int getPostcode() {
            return postcode;
        }
    
    
        public void setPostcode(int postcode) {
            this.postcode = postcode;
        }
    
    
        public String getCountry() {
            return country;
        }
        
        public void setCountry(String country) {
            this.country = country;
        }
    
    }
    
    
    package com.gp6.el.bean;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component("customerBean")
    public class Customer {
    
        @Value("#{addressBean}")
        private Address address;
    
        @Value("#{addressBean.country}")
        private String country;
        
        @Value("#{addressBean.getFullAddress('小伙')}")
        private String fullAddress;
    
        @Override
        public String toString() {
            return "Customer [address=" + address + "\n, country=" + country
                    + "\n, fullAddress=" + fullAddress + "]";
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public String getCountry() {
            return country;
        }
    
        public void setCountry(String country) {
            this.country = country;
        }
    
        public String getFullAddress() {
            return fullAddress;
        }
    
        public void setFullAddress(String fullAddress) {
            this.fullAddress = fullAddress;
        }
    }
    
    
    配置文件
    <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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:component-scan base-package="com.gp6.el.bean" />
    
    </beans>
    
    测试文件
    package com.gp6.el.bean;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
         public static void main( String[] args ) {
                
             ApplicationContext context = new ClassPathXmlApplicationContext(
                        "com/gp6/el/bean/el_bean.xml");
    
             Customer customer = (Customer) context.getBean("customerBean");
             System.out.println(customer);
        }
        
    }
    
    
    

    输出结果

    Customer [address=Address [street=哈哈,呵呵, postcode=666666, country=CN]
    , country=CN
    , fullAddress=小伙 : 哈哈,呵呵 666666 CN]
    

    - 用xml文件配置可达到相同效果,具体配置参考上一篇文章

    相关文章

      网友评论

        本文标题:16 Spring EL bean引用实例

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