使用Spring IoC可以设置通过setter公开的bean属性:
public class Bean {
private String value;
public void setValue(String value) {
this.value = value;
}
}bean的定义是:
<bean class="Bean">
<property name="value" value="Hello!">
</bean>是否有任何现有的Spring Framework插件/类允许直接将bean字段公开为属性,而无需定义setter?具有相同bean定义的内容如下所示:
public class Bean {
@Property
private String value;
}发布于 2010-10-04 18:59:49
您可以:
@Value注解并注入一个属性(使用表达式语言)发布于 2010-10-04 23:29:00
Spring为JSR-250 @Resource批注支持基于批注的字段注入out of the box。Spring自己的@Autowired和JSR 330的@Inject also work。
你只需要add this line to your context.xml
<context:annotation-config/>参考:
@Autowired and @Inject@Resource发布于 2014-03-29 09:28:37
你所要求的是不可能的。Spring订阅约定优先于配置。所以它期望有setter和getter。虽然Spring可以直接注入字段;Spring使用反射来实现这一点,但Spring没有提供反向这个过程来使用反射来访问没有setter或getter的字段。即使是Spring AOP实现也希望找到方法来构造它的代理。
https://stackoverflow.com/questions/3854428
复制相似问题