2015-03-03 36 views
7

Tôi đang cố gắng tìm hiểu cách đọc tệp thuộc tính bằng cách sử dụng spring. Sau khi tìm kiếm trên internet tôi thấy rằng tôi có thể sử dụng các chú thích @value@PropertySource để đạt được điều đó. Tôi tạo ra một dự án trong đó có các cấu trúc và các lớp mã sau:Đọc thuộc tính tệp bằng chú thích Spring

Cấu trúc của dự án:

enter image description here

AppConfigMongoDB.java thực hiệ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. triển khai 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()); 
    } 
} 

Các thuộc tính tập tin mà tôi đọc từ gọi config.properties, nó chứa các dòng sau:

mongodb.url=1.2.3.4 
mongodb.db=dataBase 

Tôi đã thử nghiệm dự án nhỏ này và tôi có một dấu vết ngăn xếp có chứa ngoại lệ sau:

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 

Đây có phải là vấn đề của hạt đậu mùa xuân không? Hoặc có lẽ nó là một vấn đề của đường dẫn tập tin thuộc tính hoặc cái gì khác?

+0

Liệu nó có tác dụng nếu bạn thêm setters cho các thuộc tính? –

+0

Không xin lỗi. Tôi có cùng ngoại lệ –

Trả lời

18

Tôi có thể thấy một số vấn đề trong mã.

1) Người giữ chỗ của bạn cho các giá trị phải ở dạng ${mogodb.url}, không phải #{mongodb.url}. Chữ "#" có nghĩa khác (Xem Spring Expressions).

2) Bạn sẽ cần một bean PropertySourcesPlaceholderConfigurer làm việc tiêm các giá trị

3) Sớm hay muộn bạn sẽ có một số Đậu nổi xung quanh, và trong tôi sẽ sử dụng @ComponentScan cho phép bối cảnh để biết những mà không cần phải đề cập đến từng cái một

4) Nếu bạn sử dụng ComponentScan để có được những hạt cà phê, bạn sẽ phải cung cấp AppConfigMongoDB đậu một lần

tôi kết thúc với các lớp sau làm tất cả những điều đó:

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

Tôi đã cung cấp một bộ thay đổi hoàn chỉnh sau khi tôi đã thực hiện dự án và đã thử nghiệm nó. Vui lòng xem lại và sau đó chúng tôi có thể xóa các nhận xét để bài đăng này hữu ích cho những người khác –

+0

yes Giờ bạn cảm ơn bạn –

+0

Có ai biết sự khác biệt giữa # {} và $ {} không? hoặc bất cứ ai có thể chỉ cho tôi nguồn tốt mà tôi có thể tìm hiểu nó – Kim

Các vấn đề liên quan