当我有一个类,如下所示为什么@Required不起作用:Spring注解:当类@Autowired
public class MyConfig {
private Integer threshold;
@Required
public void setThreshold(Integer threshold) { this.threshold = threshold; }
}
我用它如下:
public class Trainer {
@Autowired
private MyConfig configuration;
public void setConfiguration(MyConfig configuration) { this.configuration = configuration; }
}
并初始化培训师在XML上下文如下:
<bean id="myConfiguration" class="com.xxx.config.MyConfig">
<!--<property name="threshold" value="33"/>-->
</bean>
出于某种原因@Required注解不适,和上下文开始withou这是一个问题(它应该抛出一个异常说明字段阈值是必需的......)。
为什么?
检查您是否已经配置'RequiredAnnotationBeanPostProcessor'。否则'@必需的'将不会被扫描。 – sura2k