2015-03-03 94 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

相关问题