2015-03-03 58 views
7

我在學習如何使用spring讀取屬性文件。在互聯網搜索後,我發現我可以使用@value@PropertySource註釋來實現這一點。我創建了具有以下結構和類代碼的項目:使用Spring註釋讀取文件屬性

項目的結構:

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。 java實現:

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閱讀,它包含以下行:

mongodb.url=1.2.3.4 
mongodb.db=dataBase 

我測試了這個小項目我得到一個包含以下異常的堆棧跟蹤:

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 

這是Spring調用bean的問題嗎?或者,這可能是屬性文件路徑或其他問題?

+0

如果爲屬性添加setter,它會起作用嗎? – 2015-03-03 10:12:32

+0

沒有抱歉。我得到了同樣的例外 – 2015-03-03 10:21:52

回答

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。java

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

我在完成項目並對其進行測試後提供了一整套更改。請檢查,然後我們可以清理註釋,以便此帖子對其他人有用 – 2015-03-03 11:37:25

+0

是 現在很好,謝謝 – 2015-03-03 15:55:15

+0

嘿,有人知道#{}和$ {}之間有什麼區別嗎?或者任何人都可以給我看看我可以學到的很好的源碼 – Kim 2017-03-23 09:20:08

相關問題