好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring 实现给Bean属性注入null值

给Bean属性注入null值

空字符串值可以使用<value/>元素可用来表示。例如:

?

1

2

3

< bean class = "ExampleBean" >

   < property name = "email" >< value /></ property >

</ bean >

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

null值则可以使用<null/>元素可用来表示。例如:

?

1

2

3

< bean class = "ExampleBean" >

   < property name = "email" >< null /></ property >

</ bean >

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

Spring注入bean 为null的相关问题

问题描述

有一个类注入 发现里面的MongoLogInfoDAO始终为null

?

1

2

3

4

5

6

7

8

9

@Component ( "countFunction" )

public class CountFunction implements AviatorFunction {   

     @Resource (name = "mongoLogInfoDAO" )

     private MongoLogInfoDAO mongoLogInfoDAO;

 

     @Override

     public String getName() {

         return "count" ;

     }

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

?

1

AviatorEvaluator.addFunction( new CountFunction());

这个是不对的, 为啥?

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

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

如何处理?

应对这个问题的最好办法是 不要new

还有一种情况spring 注入的时候是null

问题描述

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

@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

注解可以实现

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/xiaojia1100/article/details/51595362

查看更多关于Spring 实现给Bean属性注入null值的详细内容...

  阅读:17次