簡體   English   中英

如何使用Spring注釋讀取多個資源

[英]how to read multiple resources using spring annotations

我有以下context.xml。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:jms="http://www.springframework.org/schema/jms" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          ">
  <context:annotation-config />
  <bean id="MyPropertyHolderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="locations">
      <list>
        <!-- Note: The 'server.config.override.location' variable is setup during start of the Virgo Server -->
        <value>file:#{systemProperties['con.file.pathtoFile']}/myservice-service.properties</value>
        <value>file:#{systemProperties['con.file.pathtoFile']}/myservice-mynotes.txt</value>
        <value>classpath:myservice-service.properties</value>
         <value>classpath:myservice-mynotes.txt</value>
      </list>
    </property>
  </bean>

</beans>

我可以使用以下命令從.properties文件訪問屬性,以訪問其中的鍵值。

@Value("${myfile.path}")
private static String myfile.path;

但是我不知道如何從上下文訪問其他資源“ myservice-mynotes.txt”。 基本上,我想在程序中獲取此“ myservice-mynotes.txt”文件的路徑,然后在程序中讀取它。

例如:在這里,我需要使用注釋獲取mynotesFilePath,然后將此字符串傳遞給接受文件路徑的方法。

Stirng mynotesFilePath = "C:/test/myservice-mynotes.txt";

接着

File f = new File(mynotesFilePath);

請幫忙。

我認為這里有些混亂。

PropertyPLaceholderConfigurer讀取屬性並將其傳播到您的bean。

如果您不需要屬性及其值,而是想獲取一些文件位置,那么您在談論的是資源(可以使用上下文獲得),請參見: http : //docs.spring.io/spring/docs/current/spring -framework-reference / html / resources.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM