春季限定词和财产占位符 [英] Spring Qualifier and property placeholder

查看:131
本文介绍了春季限定词和财产占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道我是否能够使用属性占位符作为限定符中的表达式?我似乎不能得到这个工作。



我使用的是春季3.0.4。

  @Controller 
public class MyController {
@Autowired
@Qualifier($ {service.class})
服务服务;
}

@Service
@Qualifier(ServiceA)
ServiceA实现服务{
public void print(){
System。 out.println(打印ServiceA.print());
}
}

@Service
@Qualifier(ServiceB)
ServiceB实现服务{
public void print(){
System.out.println(打印ServiceB.print());
}
}

XML:

 < bean id =propertyConfigurerclass =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer> 
< property name =locationvalue =file:/etc/config.properties/>
< / bean>

config.properties:

  config.properties 
service.class = serviceB


解决方案

这样做。如果您使用默认的spring bean名称,则可以忽略服务名称。 serviceA vs ServiceA等。

  @Controller 
class MyController {
@Autowired(required = false)
@Qualifier(Service)
服务服务;

public static void main(String [] args){
ApplicationContext context = new ClassPathXmlApplicationContext(app-ctx.xml,MyController.class);
for(String s:context.getBeanDefinitionNames()){
System.out.println(s);
(String t:context.getAliases(s)){
System.out.println(\t+ t);
}
}
context.getBean(MyController.class).service.print();
}
}

public interface Service {
void print();
}

@Service(value =ServiceA)
public class ServiceA实现example.Service {
public void print(){
System。 out.println(打印ServiceA.print());
}
}

@Service(value =ServiceB)
public class ServiceB实现example.Service {
public void print(){
System.out.println(打印ServiceB.print());
}
}

XML:

 < beans> 
< alias name =$ {service.class}alias =Service/>
< context:property-placeholder location =example / app.properties/>
< context:component-scan base-package =example/>
< beans>

道具:

 code> service.class = ServiceB 


Does anyone know if I should be able to use property placeholder as an expression in a Qualifier? I can't seem to get this working.

I am using spring 3.0.4.

@Controller
public class MyController {
   @Autowired
   @Qualifier("${service.class}")
   Service service;
}

@Service
@Qualifier("ServiceA")
ServiceA implements Service {
   public void print() {
       System.out.println("printing ServiceA.print()");
   } 
}

@Service
@Qualifier("ServiceB")
ServiceB implements Service {
   public void print() {
      System.out.println("printing ServiceB.print()");
   } 
}

XML:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:/etc/config.properties"/>
</bean>

config.properties:

config.properties
service.class=serviceB

解决方案

This works. You can leave off the service names if you just use the default spring bean name. serviceA vs ServiceA, etc.

@Controller
class MyController {
@Autowired(required=false)
@Qualifier("Service")
Service service;

public static void main(String[] args) {
   ApplicationContext context = new ClassPathXmlApplicationContext("app-ctx.xml", MyController.class);
   for(String s:context.getBeanDefinitionNames()){
       System.out.println(s);
       for(String t:context.getAliases(s)){
           System.out.println("\t" + t);
       }
   }
   context.getBean(MyController.class).service.print();
  }
}

public interface Service {
    void print();
}

@Service(value="ServiceA")
public class ServiceA implements example.Service {
    public void print() {
        System.out.println("printing ServiceA.print()");
    } 
}

@Service(value="ServiceB")
public class ServiceB implements example.Service {
    public void print() {
        System.out.println("printing ServiceB.print()");
    } 
}

XML:

<beans>
    <alias name="${service.class}" alias="Service"/>
    <context:property-placeholder location="example/app.properties"/>
    <context:component-scan base-package="example"/>
<beans>

Props:

service.class=ServiceB

这篇关于春季限定词和财产占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆