天天看点

Spring的属性依赖检查

spring支持4种依赖检查:默认的是none

none – no dependency checking.

simple – if any properties of primitive type (int, long,double…) and collection types (map, list..) have not been set, unsatisfieddependencyexception will be thrown.

objects – if any properties of object type have not been set, unsatisfieddependencyexception will be thrown.

     all – if any properties of any type have not been set, an unsatisfieddependencyexception will be thrown.

举个例子:

1

2

3

4

5

6

7

8

<code>public class customer</code>

<code>{</code>

<code>    </code><code>private person person;</code>

<code>    </code><code>private int type;</code>

<code>    </code><code>private string action;</code>

<code> </code> 

<code>    </code><code>//getter and setter methods</code>

<code>}</code>

  

<code>public class person</code>

<code>    </code><code>private string name;</code>

<code>    </code><code>private string address;</code>

<code>    </code><code>private int age;</code>

9

10

11

12

13

14

15

16

<code>    </code><code>&lt;</code><code>bean</code> <code>id="customerbean" class="com.mkyong.common.customer" &gt;</code>

<code>        </code><code>&lt;</code><code>property</code> <code>name="action" value="buy" /&gt;</code>

<code>    </code><code>&lt;/</code><code>bean</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>bean</code> <code>id="personbean" class="com.mkyong.common.person"&gt;</code>

<code>        </code><code>&lt;</code><code>property</code> <code>name="name" value="mkyong" /&gt;</code>

<code>        </code><code>&lt;</code><code>property</code> <code>name="address" value="address abc" /&gt;</code>

<code>        </code><code>&lt;</code><code>property</code> <code>name="age" value="29" /&gt;</code>

<code>&lt;/</code><code>beans</code><code>&gt;</code>

17

18

19

<code>    </code><code>&lt;</code><code>bean</code> <code>id="customerbean" class="com.mkyong.common.customer"</code>

<code>         </code><code>dependency-check="simple"&gt;</code>

<code>        </code><code>&lt;</code><code>property</code> <code>name="person" ref="personbean" /&gt;</code>

  注意此处type字段故意没有设置,这样会出现unsatisfieddependencyexception

<code>         </code><code>dependency-check="objects"&gt;</code>

<code>        </code><code>&lt;</code><code>property</code> <code>name="type" value="1" /&gt;</code>

  此处故意没有设置”person“属性,会出现unsatisfieddependencyexception

<code>         </code><code>dependency-check="all"&gt;</code>

<code>    </code><code>default-dependency-check="all"&gt;</code>

<code>    </code><code>&lt;</code><code>bean</code> <code>id="customerbean" class="com.mkyong.common.customer"&gt;</code>