小编rom*_*yak的帖子

在运行时更新 bean 属性

我有一个包含一些配置的 bean:

public class CustomerService{
  private Config config;

  @Required
  public void setConfig(Config config){
    this.config = config;
  }
}

public Config {
  private String login;
  private String password;

  //setters/getters
}
Run Code Online (Sandbox Code Playgroud)

应用上下文.xml:

<bean id="config" class="Config"/>
<bean id="customerService" class="CustomerService">
   <property name="config" ref="config"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

并且在运行时获取配置值(通过调用 api)。如何在运行时更新这些值?我可以使用 setter 来做到这一点:

customerService.getConfig().setLogin("login");
Run Code Online (Sandbox Code Playgroud)

java spring spring-ioc

3
推荐指数
1
解决办法
1万
查看次数

以点分隔的字符串访问(私有)成员

我可以通过以下语法访问实例字段:student.address.city

public class Student {
   private Address address;
   //getters&setters
}

public class Address {
    private String town;
    private String street;
    private String city;
   //getters&setters
}
Run Code Online (Sandbox Code Playgroud)

我认为可以通过反射以某种方式完成。基本上我需要这样的东西:

String city = getPropertyValue("student.address.city", student);
Run Code Online (Sandbox Code Playgroud)

像在js中一样,我们可以访问对象属性。

java reflection

0
推荐指数
1
解决办法
903
查看次数

标签 统计

java ×2

reflection ×1

spring ×1

spring-ioc ×1