码迷,mamicode.com
首页 > 编程语言 > 详细

SpringInAction--Bean参数的自动注入

时间:2017-02-24 14:25:52      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:tac   如何   class   key   figure   配置   value   常量   默认   

前面我已经学过了,将一个bean引用注入到另一个bean的属性或构造器参数中,这边指的是将一个对象与另一个对象进行关联。

我们学过的方法是在创建的时候根据new对象的时候,注入参数,如下:

   @Bean
    public CompactDisc randomCd() {
        int choice = (int) Math.floor(Math.random() * 4);
        switch (choice) {
            case 0:
                return new RandomCd("dangNianQing");
            case 1:
                return new RandomCd("qianNvYouHun");
            case 2:
                return new RandomCd("wo");
            default:
                return new RandomCd("chenMoShiJing");
        }
    }

也可以在xml中配置的时候,注入:

  <bean id="randomCd" class="com.bean.xml.RandomCd">
        <constructor-arg index="0" value="当年情"/>
        <constructor-arg index="1" value="张国荣"/>
    </bean>

这些方法属于的值属于 硬编码值。


 

有时候硬编码是可以的,但有的时候,我们可能会希望避免硬编码值,而是想让这些值在运行时再确定。为了实现这些功能,Spring提供了两种在运行时求值的方式:

  • 属性占位符(Property placeholder)。
  • Spring表达式语言(SpEL)。

 

先看第一种的方法,属性占位符,主要是使用@PropertySource注解和Environment

@Configuration
@PropertySource(value = "/com/bean/java/cd.properties")
public class CDPlayerConfig {

    @Autowired
    Environment environment;

    @Bean
    public CompactDisc randomCd() {

        int choice = (int) Math.floor(Math.random() * 4);
        switch (choice) {
            case 0:
                return new RandomCd(environment.getProperty("cd.dnq"));
            case 1:
                return new RandomCd(environment.getProperty("cd.qnyh"));
            case 2:
                return new RandomCd(environment.getProperty("cd.w"));
            default:
                return new RandomCd(environment.getProperty("cd.cmsj"));
        }
    }

}
cd.properties内容为:
cd.dnq= dnq
cd.qnyh = qnyh
cd.w = w
cd.cmsj = cmsj

cd.properties在运行的时候会被加载到Spring的Environment 通过从Environment检索可以得到文件中的属性值,取值的方式有以下四种种:

  • String getProperty(String var1);
  • String getProperty(String var1, String var2);
  • <T> T getProperty(String var1, Class<T> var2);
  • <T> T getProperty(String var1, Class<T> var2, T var3);

第一个就是上面所用的key-value 键值对对于的key

第二个是指当指定key,没有特定的value的时候,后面的值即是为默认值

第三个跟第四种相似,只不过他特定了一个类型,方便你用吗,无需取完之后再去转换

 

当然了Environment除了这些属性,还有:

  • String[] getActiveProfiles():返回激活profile名称的数组;
  • String[] getDefaultProfiles():返回默认profile名称的数组;
  • boolean acceptsProfiles(String... profiles):如果environment支持给定profile的话,就返回true。

上面的是java配置解析的 ,我们也来看看如何用xml跟自动扫描的方式来解析占位符

  public CompactDisc randomCd(@Value("${cd.dnq}") String str) {
        System.out.println(str);
}

xml方式

<bean id="randomCd" class="com.bean.xml.RandomCd">
        <constructor-arg index="0" value="${cd.dnq}"/>
        <constructor-arg index="1" value="张国荣"/>
</bean>

当然使用这些方法是要有前提的,需要配置PropertySourcesPlaceholderConfigurer,因为它能够基于Spring Environment及其属性源来解析占位符。

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer(){
         return  new PropertySourcesPlaceholderConfigurer();
    }

xml中需要添加:

  <context:property-placeholder />

 

让我们再来看下第二种方法:Spring表达式语言进行装配。SpEL表达式要放到“#{ ... }”之中,这与属性占位符有些类似,属性占位符需要放到“${ ... }”之中

SpEL的表达式学问还是很深的,我这里就学习点皮毛:

比如表达式为一个常量

#{3}

为一个小小的函数(T为类型)

#{T(System).currentTimeMillis()}

也可以引起其他bean的属性,这里的dangNianQing为一个bean,可以获得他的title属性和方法

#{dangNianQing.title}
#{dangNianQing.play()}

还可以通过systemProperties对象引用系统属性:

#{SystemProperties[‘cd.dnq‘]}

 

就先介绍这一点,等学的更多的时候,在贴上来。。。

 

以上就是Bean自动注入的学习笔记,如有错,欢迎指出,谢谢~

 

SpringInAction--Bean参数的自动注入

标签:tac   如何   class   key   figure   配置   value   常量   默认   

原文地址:http://www.cnblogs.com/eoooxy/p/6438109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!