2015-03-03 11 views
7

का उपयोग करके फ़ाइल गुण पढ़ें, मैं सीखने की कोशिश कर रहा हूं कि वसंत का उपयोग करके गुण फ़ाइल को कैसे पढ़ा जाए। एक इंटरनेट खोज के बाद मैंने पाया कि मैं इसे प्राप्त करने के लिए @value और @PropertySource एनोटेशन का उपयोग कर सकता हूं।स्प्रिंग एनोटेशन

परियोजना की संरचना::

enter image description here

AppConfigMongoDB.java कार्यान्वयन:

package com.mongodb.properties; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.PropertySource; 


@PropertySource("classpath:config/config.properties") 
public class AppConfigMongoDB { 

@Value("#{mongodb.url}") 
private String mongodbUrl; 


@Value("#{mongodb.db}") 
private String defaultDb; 

public String getMongoDb() 
{ 
    return defaultDb; 
} 

public String getMongoDbUrl() 
{ 
    return mongodbUrl; 
} 
} 

SpringConfiguration मैं एक परियोजना है जो निम्नलिखित संरचना और कक्षाओं कोड है बनाया। जावा कार्यान्वयन:

mongodb.url=1.2.3.4 
mongodb.db=dataBase 

मैं इस छोटी परियोजना का परीक्षण किया:

package com.mongodb.properties; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
public class SpringConfiguration { 
@Bean 
public AppConfigMongoDB getAppConfigMongoDB(){ 
     return new AppConfigMongoDB(); 
    } 
} 

Main.java

package com.mongodb.properties; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 

public class Main { 

public static void main(String[] args) { 
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class); 
    AppConfigMongoDB mongo = applicationContext.getBean(AppConfigMongoDB.class); 
    System.out.println("db= "+mongo.getMongoDb()); 
    System.out.println("URL= "+mongo.getMongoDbUrl()); 
    } 
} 

गुण फ़ाइल है कि मैं कहा जाता config.properties से पढ़ रहा हूँ, यह निम्न रेखाएं हों, और मुझे एक स्टैक ट्रेस मिला जिसमें निम्नलिखित अपवाद शामिल हैं:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getAppConfigMongoDB': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.mongodb.properties.AppConfigMongoDB.mongodbUrl; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'mongodb' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public? 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84) 
at com.mongodb.properties.Main.main(Main.java:9) 

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'mongodb' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public? 
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:226) 
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:93) 
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81) 
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:51) 
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87) 
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120) 
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:242) 
at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161) 
... 18 more 

क्या यह स्प्रिंग कॉलिंग बीन्स की समस्या है? या शायद यह गुण फ़ाइल पथ या कुछ और की समस्या है?

+0

यदि आप गुणों के लिए सेटर्स जोड़ते हैं तो यह काम करता है? –

+0

कोई खेद नहीं है। मुझे एक ही अपवाद मिला –

उत्तर

18

मैं कोड में कई मुद्दों को देख सकता हूं।

1) मूल्यों के लिए आपके स्थान धारक ${mogodb.url} रूप में होना चाहिए, #{mongodb.url} नहीं। "#" का एक अलग अर्थ है (Spring Expressions देखें)।

2) आप एक PropertySourcesPlaceholderConfigurer सेम की जरूरत के लिए मूल्यों का इंजेक्शन क्या करने जा रहे

3) जल्द ही या बाद में आप के आसपास चल सेम की एक संख्या है करने जा रहे हैं, और मैं @ComponentScan का प्रयोग करेंगे अनुमति देने के लिए संदर्भ आप उन्हें एक

4) आप ComponentScan का उपयोग करते हैं सेम प्राप्त करने के लिए, आपके पास करने के लिए जा रहे हैं के बाद एक AppConfigMongoDB सेम एक बार

मैं इन कक्षाओं के बाद अंत प्रदान करने के लिए उल्लेख करने के लिए बिना इन पता करने के लिए यह सब कर रहा है:

Main.java

import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 

public class Main { 

public static void main(String[] args) { 
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class); 
    AppConfigMongoDB mongo = applicationContext.getBean(AppConfigMongoDB.class); 
    System.out.println("db= "+mongo.getMongoDb()); 
    System.out.println("URL= "+mongo.getMongoDbUrl()); 
    } 
} 

SpringConfiguration.java

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 

@Configuration 
@ComponentScan 
public class SpringConfiguration { 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { 
    return new PropertySourcesPlaceholderConfigurer(); 
    } 
} 

AppConfigMongoDB।जावा

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 

@Configuration 
@PropertySource("classpath:config/config.properties") 
public class AppConfigMongoDB { 

    @Value("${mongodb.url}") 
    private String mongodbUrl; 

    @Value("${mongodb.db}") 
    private String defaultDb; 

    public String getMongoDb() { 
    return defaultDb; 
    } 

    public String getMongoDbUrl() { 
    return mongodbUrl; 
    } 
} 
+1

मैंने परियोजना बनाने के बाद परिवर्तनों का एक पूरा सेट प्रदान किया और इसका परीक्षण किया। कृपया समीक्षा करें और फिर हम टिप्पणियां साफ़ कर सकते हैं ताकि यह पोस्ट दूसरों के लिए उपयोगी हो –

+0

हाँ यह ठीक है अब आपको धन्यवाद –

+0

अरे किसी को पता है कि # {} और $ {} के बीच क्या अंतर है? या कोई मुझे अच्छा स्रोत दिखा सकता है जिसे मैं इसे सीख सकता हूं – Kim

संबंधित मुद्दे