当前位置: 首页 > 面试题库 >

Spring限定符和属性占位符

云镜
2023-03-14
问题内容

有谁知道我是否应该可以在预选赛中使用属性占位符作为表达式?我似乎无法正常工作。

我正在使用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

问题答案:

这可行。如果仅使用默认的spring bean名称,则可以省略服务名称。serviceA与ServiceA等

@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


 类似资料:
  • 我不明白为什么不能在Spring-Boot中向application.properties文件中注入值。外部属性添加到logging.file变量中。我有一个application.properties文件,看起来如下所示 具有相应的Spring-boot应用程序类 请注意,如果我自己注入服务器端口号,那么在注入和启动应用程序时没有任何问题。 我在这个问题上兜圈子,弄不清自己做错了什么。

  • 问题内容: 我有我的配置: 我得到错误 我知道这可能缺少属性文件,但是我在类路径中恰好有它。有什么不见了? 我的web.xml: 问题答案: 你的应用程序中可能有多个。尝试在超类的方法上设置一个断点,看看在应用程序启动时是否多次调用了该断点。如果不止一个,则可能需要查看配置属性,以便你的应用程序可以正常启动。

  • 在Spring Boot中更改属性占位符的前缀和后缀的最简单方法是什么? 默认值是,但是这在Kotlin中看起来很难看,因为它需要转义-${something}是Kotlin中用于字符串模板的语言功能。

  • 我正在尝试使用骆驼JavaDSL将文件路由到SFTP服务器,如下所示: 但是,当消息到达此终结点时,Camel 会引发以下异常: 我可以看到在堆栈跟踪中打印的交易所上设置了目标目录属性。如果我替换 exchangeProperty(destinationDir) 在具有实际目标目录(tmp/目标/dir 1/)的路由中,它工作正常。问题是,我需要目标目录是动态的。我尝试过在路由中使用( 在调试Ca

  • 问题内容: 我有一个使用Logback的Spring Boot控制台应用程序。所有属性(针对应用程序和Logback)都被外部化为类路径中的标准application.properties文件。在应用程序本身中可以很好地拾取这些属性,但在logback.xml文件中不会拾取这些属性。在Spring Boot启动之前,似乎好像处理了logback.xml,因此未处理EL占位符。 以FileNameP

  • 我有一个使用Logback的Spring Boot控制台应用程序。所有属性(应用程序和日志)都外部化到类路径中的标准application.properties文件中。这些属性在应用程序本身中可以很好地提取,但在logback.xml文件中不能提取。看起来好像是在Spring Boot启动之前处理了logback.xml,因此没有处理EL占位符。 使用FileNamePattern作为示例,在ap