2015-03-03 14 views
7

Yay kullanarak özellik dosyalarının nasıl okunacağını öğrenmeye çalışıyorum. Bir internet araştırmasından sonra, bunu elde etmek için @value ve @PropertySource ek açıklamalarını kullanabileceğimi buldum. ProjeninYaylı bilgi notlarını kullanarak dosya özelliklerini okuyun

Yapısı:

enter image description here

AppConfigMongoDB.java uygulanmasını:

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 aşağıdaki yapı ve sınıflar kodlarını içeren bir proje oluşturdu. java uygulaması:

Ben bu küçük projeyi test

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 özellikleri aradım config.properties gelen okuyorum o dosya

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()); 
    } 
} 

, aşağıdaki satırları içerdiğini ve aşağıdaki özel durumu içeren bir yığın izim var:

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 

Bahar çağırma fasulyesi sorunu mu? Ya da belki özellikler dosya yolu ya da başka bir şey sorun mu?

+0

Özellikler için ayarlayıcılar eklerseniz işe yarar mı? –

+0

Üzgünüz. Aynı istisnayı aldım –

cevap

18

Kodda birkaç sorun görüyorum.

1) Değerleriniz için yer tutucularınız #{mongodb.url} değil ${mogodb.url} biçiminde olmalıdır. "#" Farklı bir anlama sahiptir (Bkz. Spring Expressions).

2) değerlerinin enjeksiyon yapmak için bir PropertySourcesPlaceholderConfigurer fasulye ihtiyacımız olacak

3) Er ya da geç etrafında yüzen Fasulye bir dizi olacak ve ben izin @ComponentScan kullanırsınız içinde olan bağlam kez AppConfigMongoDB fasulye ben bu sınıfların sonra ile bitirmek

sağlamak için onlara bir

4) fasulye almak için ComponentScan kullanırsanız, yaşayacaksın teker bahsetmek gerek kalmadan bu bilmek tüm bunları yapıyor:

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

Projeyi oluşturup test ettikten sonra tam bir değişiklik kümesi sağladım. gözden geçirmek ve bu yayını diğerlerinden –

+0

için yararlı olacak şekilde sonra bir yorum temizleyebilir Lütfen evet Sorun değil şimdi teşekkür ederim –

+0

Hey kimse # {} ve {} $ arasındaki fark nedir biliyor musunuz? ya da herkes bana iyi bir kaynak gösterebilir ki bunu öğrenebilirim – Kim

İlgili konular