给Bean属性注入null值
空字符串值可以使用<value/>元素可用来表示。例如:
<bean class="ExampleBean">
<property name="email"><value/></property>
</bean>
等同于Java代码: exampleBean.setEmail("")。
null值则可以使用<null/>元素可用来表示。例如:
<bean class="ExampleBean">
<property name="email"><null/></property>
</bean>
等同于Java代码:exampleBean.setEmail(null)。
Spring注入bean 为null的相关问题
问题描述
有一个类注入 发现里面的MongoLogInfoDAO始终为null
@Component("countFunction")
public class CountFunction implements AviatorFunction {
@Resource(name = "mongoLogInfoDAO")
private MongoLogInfoDAO mongoLogInfoDAO;
@Override
public String getName() {
return "count";
}
原因是竟然是因为使用了 new 方式使用 CountFunction 导致类中的mongoLogInfoDao 无法注入
AviatorEvaluator.addFunction(new CountFunction());
这个是不对的, 为啥?
这个对象是你new出来的 ,不是spring创建的 当然无法DI依赖注入
反之,如果你这个Bean是交易Spring IOC 容器管理的,那也应该报错NoSuchBeanDefinitionException异常
如何处理?
应对这个问题的最好办法是 不要new
还有一种情况spring 注入的时候是null
问题描述
@Component("functionExecutor")
public class FunctionExecutor {
protected static final Loggerx LOGGER = Loggerx.getLogger("system");
@Resource(name = "mongoDAO")
private MongoDAO mongoDAO;
@Resource(name = "prism4001Producer")
private SmartMQProducer prism4001Producer;
@Resource(name = "whereGreaterThenFunction")
private WhereGreaterThenFunction whereGreaterThenFunction;
@Resource(name = "countFunction")
private CountFunction countFunction;
@Resource(name = "devideFunction")
private DevideFunction devideFunction;
@Resource(name = "selectFunction")
private SelectFunction selectFunction;
@Resource(name = "whereEqualFunction")
private WhereEqualFunction whereEqualFunction;
@Resource(name = "timeFunction")
private TimeFunction timeFunction;
@Resource(name = "whereLessThenFunction")
private WhereLessThenFunction whereLessThenFunction;
@Resource(name = "whereGreaterThenEqualFunction")
private WhereGreaterThenEqualFunction whereGreaterThenEqualFunction;
@Resource(name = "whereInFunction")
private WhereInFunction whereInFunction;
@Resource(name = "greaterThenFunction")
private GreaterThenFunction greaterThenFunction;
@Resource(name = "regexFunction")
private RegexFunction regexFunction;
@Resource(name = "maxFunction")
private MaxFunction maxFunction;
@Resource(name = "minFunction")
private MinFunction minFunction;
@Resource(name = "equalFunction")
private EqualFunction equalFunction;
@Resource(name = "lessThenEqualFunction")
private LessThenEqualFunction lessThenEqualFunction;
@Resource(name = "lessThenFunction")
private LessThenFunction lessThenFunction;
@Resource(name = "plusFunction")
private PlusFunction plusFunction;
@Resource(name = "multiFunction")
private MultiFunction multiFunction;
@Resource(name = "notEqualFunction")
private NotEqualFunction notEqualFunction;
@Resource(name = "whereNotInFunction")
private WhereNotInFunction whereNotInFunction;
@Resource(name = "sumFunction")
private SumFunction sumFunction;
@Resource(name = "minusFunction")
private MinusFunction minusFunction;
{
AviatorEvaluator.addFunction(whereGreaterThenFunction);
AviatorEvaluator.addFunction(countFunction);
AviatorEvaluator.addFunction(devideFunction);
AviatorEvaluator.addFunction(selectFunction);
AviatorEvaluator.addFunction(whereEqualFunction);
AviatorEvaluator.addFunction(timeFunction);
AviatorEvaluator.addFunction(whereLessThenFunction);
AviatorEvaluator.addFunction(whereGreaterThenEqualFunction);
AviatorEvaluator.addFunction(whereInFunction);
AviatorEvaluator.addFunction(greaterThenFunction);
AviatorEvaluator.addFunction(regexFunction);
AviatorEvaluator.addFunction(maxFunction);
AviatorEvaluator.addFunction(minFunction);
AviatorEvaluator.addFunction(equalFunction);
AviatorEvaluator.addFunction(lessThenEqualFunction);
AviatorEvaluator.addFunction(lessThenFunction);
AviatorEvaluator.addFunction(plusFunction);
AviatorEvaluator.addFunction(multiFunction);
AviatorEvaluator.addFunction(notEqualFunction);
AviatorEvaluator.addFunction(whereNotInFunction);
AviatorEvaluator.addFunction(sumFunction);
AviatorEvaluator.addFunction(minusFunction);
}
容器加载这个functionExecutor这个bean的时候,走到静态代码块发现注入的sumFunction ,minusFunction都是null
这个是为什么呢?
spring 执行代码块的时候,其他的bean 还未注入,会导致注入的bean 是null
如何处理?
如果你想 当spring加载 bean 初始化完成之后,自动执行一些初始化方法
使用
@PostConstruct
注解可以实现
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。