Назначить property в спринг с помощью @PropertySource

Рейтинг: 3Ответов: 2Опубликовано: 02.02.2015

Как читать инфу из propery файла с помощью аннотаций? Делаю, как тут, свой пример сильно упростил, но и он не работает. Имеются такие классы:

public class Customer {

private String name;

public Customer() {
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}}

Конфигурационный:

@Configuration
@PropertySource("database.properties")
public class AppConfiguration {
  @Value("#{jdbc.username}")
  private String name;

  @Bean(name = {"customerBean"})
  @Scope(value = "prototype")
  @DependsOn({"conf"})
  public Customer customer() {
    Customer customer = new Customer();
    customer.setName(name);
    return customer;
  }

  @Bean(name = {"conf"})
  public static PropertySourcesPlaceholderConfigurer conf() {
    PropertySourcesPlaceholderConfigurer conf
            = new PropertySourcesPlaceholderConfigurer();
    return conf;
}}

Файл properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mk
jdbc.username=root
jdbc.password=root 

лежит в папке target/classes, как на рисунке:

alt text

При попытке получить доступ к полю бина customerBean.getName лезут исключения -

Property or field 'jdbc' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?

И не понимаю, почему так. Если сделать бин conf non-static, то исключение более не лезет, но поле customerBean.getName == null. Какой нормальный подход к этому?

Ответы

Ответов пока нет.