1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .beans ;
18
18
19
+ import java .lang .reflect .Constructor ;
20
+ import java .lang .reflect .Method ;
21
+
19
22
/**
20
23
* Exception thrown when instantiation of a bean failed.
21
24
* Carries the offending bean class.
@@ -28,6 +31,10 @@ public class BeanInstantiationException extends FatalBeanException {
28
31
29
32
private Class <?> beanClass ;
30
33
34
+ private Constructor <?> constructor ;
35
+
36
+ private Method constructingMethod ;
37
+
31
38
32
39
/**
33
40
* Create a new BeanInstantiationException.
@@ -49,12 +56,60 @@ public BeanInstantiationException(Class<?> beanClass, String msg, Throwable caus
49
56
this .beanClass = beanClass ;
50
57
}
51
58
59
+ /**
60
+ * Create a new BeanInstantiationException.
61
+ * @param constructor the offending constructor
62
+ * @param msg the detail message
63
+ * @param cause the root cause
64
+ * @since 4.3
65
+ */
66
+ public BeanInstantiationException (Constructor <?> constructor , String msg , Throwable cause ) {
67
+ super ("Failed to instantiate [" + constructor .getDeclaringClass ().getName () + "]: " + msg , cause );
68
+ this .beanClass = constructor .getDeclaringClass ();
69
+ this .constructor = constructor ;
70
+ }
52
71
53
72
/**
54
- * Return the offending bean class.
73
+ * Create a new BeanInstantiationException.
74
+ * @param constructingMethod the delegate for bean construction purposes
75
+ * (typically, but not necessarily, a static factory method)
76
+ * @param msg the detail message
77
+ * @param cause the root cause
78
+ * @since 4.3
79
+ */
80
+ public BeanInstantiationException (Method constructingMethod , String msg , Throwable cause ) {
81
+ super ("Failed to instantiate [" + constructingMethod .getReturnType ().getName () + "]: " + msg , cause );
82
+ this .beanClass = constructingMethod .getReturnType ();
83
+ this .constructingMethod = constructingMethod ;
84
+ }
85
+
86
+
87
+ /**
88
+ * Return the offending bean class (never {@code null}).
89
+ * @return the class that was to be instantiated
55
90
*/
56
91
public Class <?> getBeanClass () {
57
92
return this .beanClass ;
58
93
}
59
94
95
+ /**
96
+ * Return the offending constructor, if known.
97
+ * @return the constructor in use, or {@code null} in case of a
98
+ * factory method or in case of default instantiation
99
+ * @since 4.3
100
+ */
101
+ public Constructor <?> getConstructor () {
102
+ return this .constructor ;
103
+ }
104
+
105
+ /**
106
+ * Return the delegate for bean construction purposes, if known.
107
+ * @return the method in use (typically a static factory method),
108
+ * or {@code null} in case of constructor-based instantiation
109
+ * @since 4.3
110
+ */
111
+ public Method getConstructingMethod () {
112
+ return this .constructingMethod ;
113
+ }
114
+
60
115
}
0 commit comments