我有一個通用的方法,實例化一個對象如下:我歧義與通用方法
@Override
public <T> T createRawObject(Class<?> raw_type,
ProviderParam param)
{
SpringProviderParam spring_param = (SpringProviderParam) param;
ApplicationContext ctx = SpringContextGenericProvider.getInstance()
.generate(param,
ApplicationContext.class,
(Object[]) spring_param.getContextPaths());
ValidateUtility.notNull(ctx, "Target Application_Context is null");
T raw_object= (T) ctx.getBean((spring_param.getBeanName()!=null)?spring_param.getBeanName():raw_type);
ValidateUtility.sameType(raw_object, raw_type, "Target object isn't instance of a {} class", raw_type);
return raw_object;
}
我的問題是下面一行:
T raw_object= (T) ctx.getBean((spring_param.getBeanName()!=null)?spring_param.getBeanName():raw_type);
此行並沒有編譯和展示以下編譯錯誤:
The method getBean(String) in the type BeanFactory is not applicable for the arguments (Serializable)
但是當我將這一行更改爲以下行並編譯罰款:
T raw_object= null;
if(spring_param.getBeanName()!=null)
raw_object= (T) ctx.getBean(spring_param.getBeanName());
else
raw_object= (T) ctx.getBean(raw_type);
我含糊不清這個問題。