Spring|Spring 实现给Bean属性注入null值

目录

  • 给Bean属性注入null值
  • Spring注入bean 为null的相关问题
    • 问题描述
    • 问题描述
    • 如何处理?

给Bean属性注入null值 【Spring|Spring 实现给Bean属性注入null值】空字符串值可以使用元素可用来表示。例如:

等同于Java代码: exampleBean.setEmail("")。
null值则可以使用元素可用来表示。例如:

等同于Java代码:exampleBean.setEmail(null)。

Spring注入bean 为null的相关问题

问题描述
有一个类注入 发现里面的MongoLogInfoDAO始终为null
@Component("countFunction")public class CountFunction implements AviatorFunction {@Resource(name = "mongoLogInfoDAO")private MongoLogInfoDAO mongoLogInfoDAO; @Overridepublic 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")privateWhereGreaterThenFunction whereGreaterThenFunction; @Resource(name = "countFunction")privateCountFunction countFunction; @Resource(name = "devideFunction")privateDevideFunction devideFunction; @Resource(name = "selectFunction")privateSelectFunction selectFunction; @Resource(name = "whereEqualFunction")privateWhereEqualFunction whereEqualFunction; @Resource(name = "timeFunction")privateTimeFunction timeFunction; @Resource(name = "whereLessThenFunction")privateWhereLessThenFunction whereLessThenFunction; @Resource(name = "whereGreaterThenEqualFunction")privateWhereGreaterThenEqualFunction whereGreaterThenEqualFunction; @Resource(name = "whereInFunction")privateWhereInFunction whereInFunction; @Resource(name = "greaterThenFunction")privateGreaterThenFunction greaterThenFunction; @Resource(name = "regexFunction")privateRegexFunction regexFunction; @Resource(name = "maxFunction")privateMaxFunction maxFunction; @Resource(name = "minFunction")privateMinFunction minFunction; @Resource(name = "equalFunction")privateEqualFunction equalFunction; @Resource(name = "lessThenEqualFunction")privateLessThenEqualFunction lessThenEqualFunction; @Resource(name = "lessThenFunction")privateLessThenFunction lessThenFunction; @Resource(name = "plusFunction")privatePlusFunction plusFunction; @Resource(name = "multiFunction")privateMultiFunction multiFunction; @Resource(name = "notEqualFunction")privateNotEqualFunction notEqualFunction; @Resource(name = "whereNotInFunction")privateWhereNotInFunction whereNotInFunction; @Resource(name = "sumFunction")privateSumFunction sumFunction; @Resource(name = "minusFunction")privateMinusFunction 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
注解可以实现
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读