2013-01-17 39 views
10

में क्लासपाथ वाइल्डकार्ड मैं अपनी बीन बनाने के लिए स्प्रिंग जावा कॉन्फ़िगरेशन का उपयोग कर रहा हूं। लेकिन यह बीन 2 अनुप्रयोगों के लिए आम है। दोनों में एक संपत्ति फ़ाइल abc.properties है लेकिन विभिन्न वर्गपाथ स्थानों के साथ। जब मैं की तरह@PropertySource

@PropertySource("classpath:/app1/abc.properties") 

स्पष्ट classpath डाल तो यह काम करता है, लेकिन फिर यह काम नहीं करता जब मैं की तरह

@PropertySource("classpath:/**/abc.properties") 

वाइल्डकार्ड का उपयोग करने का प्रयास करें। मैं वाइल्डकार्ड के कई संयोजनों को आजमाता हूं लेकिन यह अभी भी काम नहीं कर रहा है। क्या वाइल्डकार्ड @ProeprtySource में काम करता है क्या @Configurations के साथ चिह्नित कक्षा में संपत्ति को पढ़ने का कोई अन्य तरीका है।

उत्तर

13

@PropertySource एपीआई: Resource location wildcards (e.g. **/*.properties) are not permitted; each location must evaluate to exactly one .properties resource.

वैकल्पिक हल: कोशिश

@Configuration 
public class Test { 

    @Bean 
    public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer() 
      throws IOException { 
     PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); 
     ppc.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/abc.properties")); 
     return ppc; 
    } 
+1

जोड़ा जा रहा है 'static' संशोधक दिया, जबकि तैनाती चेतावनी पाएगा '@ Autowired',' @ संसाधन 'और '@ PostConstruct' जैसी घोषणाओं को संसाधित करने में विफलता विधि की घोषणा' @ कॉन्फ़िगरेशन 'कक्षा के भीतर। इन कंटेनर लाइफसाइकिल समस्याओं से बचने के लिए इस विधि में' स्थैतिक 'संशोधक जोड़ें;' @Bean देखें पूर्ण विवरण के लिए 'जावाडोक।' लेकिन धन्यवाद, यह एक आकर्षण की तरह काम किया। –

6

Addidtionally dmay को वैकल्पिक हल:

के बाद से वसंत 3.1 PropertySourcesPlaceholderConfigurer PropertyPlaceholderConfigurer से अधिक प्राथमिकता इस्तेमाल किया जाना चाहिए और सेम स्थिर होना चाहिए । । `@ Bean` विधि Test.getPropertyPlaceholderConfigurer गैर स्थिर है और एक वस्तु वसंत के BeanFactoryPostProcessor इंटरफ़ेस के लिए आबंटित रिटर्न यह एक में परिणाम होगा - 310:" ConfigurationClassEnhancer:

@Configuration 
public class PropertiesConfig { 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() { 
    PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer(); 
    propertyConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/abc.properties")); 
    return propertyConfigurer; 
    } 

} 
+1

+1 आपने अपना दिन – sjngm

+0

बचाया यह दिखाता है कि 'अनचाहे अपवाद प्रकार IOException' – Lucky