我有一个包含一些配置的 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) 我可以通过以下语法访问实例字段: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中一样,我们可以访问对象属性。