2017-03-03 59 views
0

在属性文件(config.properties)我定义几个属性:解决@Value失败 - 弹簧的java

my.property.first=xyz 
my.property.second=12 

我创建了一个类来读取这些属性:

package my.package.first; 
............ 

@Configuration 
public Class MyProperties { 

    @Value("${my.property.first}") private String propertyFirst; 

    @Value ("${my.property.second}") private String propertySecond; 

    public String getPropertyFirst() { 
     return propertyFirst; 
    } 

    public int getPropertySecond() { 
     return propertySecond 
    } 

    @Bean 
    public MyProperties getInstance() { 
     return this; 
    } 
} 

现在我想放在一些其他包中的类来使用这些属性:

package my.otherpackage.third; 

import my.property.package.first.MyProperties; 
............................. 
public class GetMyProperties{ 

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyProperties.class); 

    MyProperties myProperties =context.getBean("getInstance",MyProperties.class); 

    //This returns ${my.property.first} 
    String propertyFirst = myProperties.getPropertyFirst(); 

    // This returns ${my.property.second} 
    int propertySecond = context.getBeanFactory().resolveEmbeddedValue(("${my.property.first}")); 
} 

我尝试溶胶通过仅使用注释来实现这一点。

我使用Spring 4

感谢

回答

0

MyProperties是不是一个真正的@Configuration类,它是一个bean。给它注释@Component并将@Bean声明移动到另一个@Configuration类。无论Spring在何处定义bean(只要它位于配置类中),它都会在您的应用程序中有@ComponentScan或@SpringBootApplication的地方选择它,我很确定你做。您MyProperties类,你让@Component

你会想:

@Component 
public class MyProperties { 
    @Value("${my.property.first}") private String propertyFirst; 

    @Value ("${my.property.second}") private String propertySecond; 

    public String getPropertyFirst() { 
     return propertyFirst; 
    } 

    public void setPropertyFirst(String propertyFirst){ 
     this.propertyFirst = propertyFirst; 
    } 

    public int getPropertySecond() { 
     return propertySecond 
    } 

    public void setPropertySecond(String propertySecond){ 
     this.propertySecond= propertySecond; 
    } 
} 



@Configuration 
public class Config { 
    @Bean 
    public MyProperties getInstance() { 
     return new MyProperties(); 
    } 
} 



package my.otherpackage.third; 

import my.property.package.first.MyProperties; 
..... 
@EnableAutoConfiguration 
@ComponentScan(basePackages = {"my"}) 
public class GetMyProperties{ 
    public static void main(String[] args){ 
     AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(GetMyProperties.class); 

     MyProperties myProperties =context.getBean("getInstance",MyProperties.class); 

     //This returns the value of ${my.property.first} 
     String propertyFirst = myProperties.getPropertyFirst(); 

     // This returns the value of ${my.property.second} 
     int propertySecond = myProperties.getPropertySecond(); 
    } 
} 
+0

我增加了豆的getInstance()只是为了得到myProperties实例。如果我删除此bean,如何访问类GetMyProperties中的属性? – uztrew

+0

在另一个地方定义bean,你的bean不能同时配置和bean。无论Spring在何处定义bean(只要它在配置类中),它都会在你的应用程序中有\\ComponentScan或\ @SpringBootApplication的地方选择它,而我很确定你做到了。 – user2323354

+0

我的意思是有没有一种方法来访问这些属性而根本不使用这个bean? – uztrew