zoukankan      html  css  js  c++  java
  • 005运行时值注入

    01、属性占位符(Property placeholder)

    @Configuration
    @PropertySource("classpath:/com/soundsystem/app.properties")
    public class ExpressiveConfig {
        @Autowired
        Environment env;
        @Bean
        public BlankDisc disc(){
            return new BlaknDisc(env.getProperty("disc.title"), env.getProperty("disc.artist"))
        }
    }

    02、Spring的Environment

    String getProperty(String key)---->给定key的属性未定义,返回null
    String getProperty(String key, String defaultValue)---->当给定key的属性不存在,则返回defaultvalue
    
    T getProperty(String key, Class<T> type)---->获取字符串转成Class<T>实例
    T getProperty(String key, Class<T> type, T defaultValue)
    
    String getRequiredProperty(String key)---->不存在,则抛IllegalStateException异常
    
    Boolean containsProperty(String key)---->判断给定key的属性是否存在
    
    T getPropertyAsClass(String key, Class<T> type)
    
    String [] getActiveProfiles()
    String [] getDefaultProfiles()
    boolean acceptsProfiles(String ...profiles)

    03、占位符@Value注解(和@Autowired类似)

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer(){
        return new PropertySourcesPlaceholderConfigurer();//配置使能
    }
    public BlankDisc(@Value("${disc.title}") String title, @Value("${disc.artist}")){//使用@Value
        this.title = title;
        this.artist = artist;
    }

    04、Spring表达式语言(SpEL)

    1、使用方式与属性占位符类似
        public BlankDisc(@Value("#{systemProperties['disc.title']}" String title, @Value("#{systemProperties['disc.artist']}"))){
            this.title = title;
            this.artist = artist;
        }
    2、字面值
        #{1}    #{3.14159265}    #{9.86E4}    #{'Hello'}    #{false}
    3、引用bean、属性、方法
        #{beanID}    #{beanID.field} #{beanID.methodName()}
    4、使用类型
        #{T(java.lang.Math)}    #{T(java.lang.Math).PI}    #{T(java.lang.Math).random()}
    5、运算符
        算数运算    +  -  *  /  %  ^
        比较运算    <  >  ==  <=  >=  lt  gt  eq  le  ge
        逻辑运算    and  or  not  |
        条件运算    logic ? trueResult : falseResult    expression ?: 'null result'     
        正则表达式    matches
    6、条件运算&正则表达式
        #{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.com'}
        #{scoreboard.score > 1000 ? "Winner!" : "Loser"}
        #{disc.title ?: 'Default Result'}---->为null,则返回默认值
    7、集合
        #{jukebox.songs[4].title}
        #{jukebox.songs[T(java.lang.Math).random() * jukebox.songs.size()].title}
        #{'This is a test'[3]}
        #{jukebox.songs.?[artist eq 'Aerosmith']}---->查询歌曲中,作家为Aerosmith的歌曲,得到一个集合
        #{jukebox.songs.^[artist eq 'Aerosmith']}---->查询第一个作家为Aerosmith的歌曲
        #{jukebox.songs.$[artist eq 'Aerosmith']}---->查询最后一个作家为Aerosmith的歌曲
        #{jukebox.songs.![title]}---->投影,取出songs集合中的title组成集合
        #{jukebox.songs.?[artist eq 'Aerosmith'].![title]}---->查询作者为Aerosmith的歌曲,取title组成新集合
  • 相关阅读:
    linux sort,uniq,cut,wc命令详解
    Linux IO实时监控iostat命令详解
    uniq命令
    四层和七层负载均衡的区别
    keepalived 和 heartbeat对比
    LVS+Keepalived实现负载均衡
    Lvs+heartbeat高可用高性能web站点的搭建
    使用 awk 过滤文本或文件中的字符串
    最全mysql的复制和读写分离
    伤不起
  • 原文地址:https://www.cnblogs.com/geniushuangxiao/p/7301037.html
Copyright © 2011-2022 走看看