Spring bean的装配

2017-04-11  本文已影响0人  沥人土土
三种主要装配方式:
profile的激活

Spring在确定哪个profile处于激活状态时,需要依赖两个独立属性:
spring.profiles.active和spring.profiles.default。
有多重方法设置这两种属性:

处理自动装配的歧义性

如果不仅有一个bean能够匹配结果,歧义性会阻碍Spring自动装配属性、构造器参数或方法参数。比如用@Autowired标注了某个X的set方法,而X是一个有3个实现类的接口,且这三个类均使用了@Component注解。此时Spring会抛出NoUniqueBeanDefinitionException。

解决办法:
1)标示首选的Bean,通过@Primary。(同一接口下只能有一个Primary bean)
2)使用限定符

@Target({ElementType.CONSTRUCTOR, ElementType.FIELD,
           ElmentType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface YourAnnotation{ }

然后,

@Autowired
@Qualifier("自定义限定符名")
@YourAnnotation
public void setAA(AA aa){
  this.aa = aa;
}
Bean的作用域

Spring定义了多种作用域,可以基于这些作用域创建bean,包括:

在运行时值注入

有时,我们可能会希望避免硬编码值,并让这些值在运行时再确定。Spring有两种在运行时求值的方式:

注入外部的值

使用外部属性来装配一个bean:

@Configuration
@PropertySource("classpath:/路径/app.properties") //声明属性源
public class ExpressiveConfig{

  @Autowired
  Environment env;
  @Bean
  public XX x(){
    return new XX(
        env.getProperty("x.title"),
          env.getProperty("x.artist"));
  }
}

在本例中,@PropertySource引用了类路径中名为app.properties的文件。大致如下所示:

x.title = xxxxx
x.artist = xxxax

这个属性的文件会加载到Spring的Environment中,稍后可以从这里检索属性。同时,在x()中,会创建一个新的XX,他的构造器参数是从属性文件中获取的,而这时通过调用getProperty()实现的。

解析属性占位符

Spring支持将属性定义到外部的属性文件,并使用占位符值将其插入到Spring bean中。在Spring装配中,占位符的形式为使用${...}包装属性名称。

使用SpEL进行装配

SpEL(Spring Expression Languae)是Spring3引入的一种强大简洁的将值装配到bean属性和构造器参数中的方式。
SpELl特性:

SpEL需要放入“#{...}”之中。
SpEL表达式可以引用其他的bean或其他bean的属性:
#{id.attributename}
与@Value结合:

public ClassName{
  @Value("#{systemProperties['id.attributename']}") String attributename,
  @Value("#{systemProperties['id.anotherattributename']}") String anotherattributename){
    this.attributename= attributename;
    this.anotherattributename= anotherattributename;
  }

还有很多其他功能,有机会整理一篇具体功能再发。

上一篇 下一篇

猜你喜欢

热点阅读