天天看點

Spring的屬性依賴檢查

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>&lt;</code><code>beans</code> <code>xmlns="http://www.springframework.org/schema/beans"</code>

<code>    </code><code>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</code>

<code>    </code><code>xsi:schemaLocation="http://www.springframework.org/schema/beans</code>

<code>    </code><code>http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;</code>

<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>http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"</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>

==============================================================================

本文轉自被遺忘的部落格園部落格,原文連結:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835270.html,如需轉載請自行聯系原作者