我正在尝试使用java反射和@PostConstruct
spring注释验证方法列表返回的值,如下面的代码片段所示:
@Service("appConfigService")
public class AppConfigServiceImpl implements AppConfigService{
//rest of source code including constructor and other methods is omitted
@Override
public String getServerURL(){//example of a method I am invoking in the postConstruct phase
//method body omitted
}
@PostConstruct
public void postConstruct() {
try {
Class<AppConfigServiceImpl> classObject = AppConfigServiceImpl.class;
AppConfigServiceImpl appConfigServiceImpl = classObject.newInstance();
Method[] methods = classObject.getMethods();
for(Method method: methods)
method.invoke(appConfigServiceImpl);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
e.printStackTrace();
}
}
}
但是当spring执行postConstruct()
方法时,会抛出java.lang.reflect.InvocationTargetException
。我无法弄清楚异常的来源。任何帮助都将受到欢迎。