Skip to content

Introduce specific exceptions for field and method autowiring failures #969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -587,7 +587,7 @@ protected void inject(Object bean, String beanName, PropertyValues pvs) throws T
}
}
catch (Throwable ex) {
throw new BeanCreationException("Could not autowire field: " + field, ex);
throw new FieldAutowiringException(field, ex);
}
}
}
Expand Down Expand Up @@ -675,7 +675,7 @@ protected void inject(Object bean, String beanName, PropertyValues pvs) throws T
throw ex.getTargetException();
}
catch (Throwable ex) {
throw new BeanCreationException("Could not autowire method: " + method, ex);
throw new MethodAutowiringException(method, ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.beans.factory.annotation;

import java.lang.reflect.Field;

import org.springframework.beans.factory.BeanCreationException;

/**
* Exception thrown when a failure occurs when attempting to autowire a field.
*
* @author Andy Wilkinson
* @since 4.3.0
*/
@SuppressWarnings("serial")
public class FieldAutowiringException extends BeanCreationException {

private final Field field;

/**
* Create a new FieldAutowiringException.
* @param field the field that could not be autowired
* @param cause the cause of the failure
*/
public FieldAutowiringException(Field field, Throwable cause) {
super("Could not autowire field: " + field, cause);
this.field = field;
}

/**
* Returns the field that could not be autowired.
*/
public Field getField() {
return field;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.beans.factory.annotation;

import java.lang.reflect.Method;

import org.springframework.beans.factory.BeanCreationException;

/**
* Exception thrown when a failure occurs when attempting to autowire a method.
*
* @author Andy Wilkinson
* @since 4.3.0
*/
@SuppressWarnings("serial")
public class MethodAutowiringException extends BeanCreationException {

private final Method method;

/**
* Create a new MethodAutowiringException.
* @param method the method that could not be autowired
* @param cause the cause of the failure
*/
public MethodAutowiringException(Method method, Throwable cause) {
super("Could not autowire method: " + method, cause);
this.method = method;
}

/**
* Returns the method that could not be autowired.
*/
public Method getMethod() {
return method;
}

}