Spring 实现给Bean属性注入null值

网友投稿 653 2022-12-14

Spring 实现给Bean属性注入null值

Spring 实现给Bean属性注入null值

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

给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;

@Override

public String getName() {

return "count";

}

原因是竟然是因为使用了 new 方式使用 CountFunction 导致类中的mongoLogInfoDao 无法注入

AviatorEvaluator.addFunction(new CountFunction());

这个是不对的, 为啥?

这个对象是你new出来的 ,不是spring创建的 当然无法DI依赖注入

反之,如果你这个Bean是交易Spring IOC 容器管理的,那也应该报错NoSuchBeanDefinitionException异常

如何处理?

应对这个问http://题的最好办法是 不要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 whereGreaterThenEquZIlFkPkuwyalFunction;

@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

注解可以实现

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决Properties属性文件中的值有等号和换行的小问题
下一篇:SpringBoot调用第三方WebService接口的操作技巧(.wsdl与.asmx类型)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~