我有一个用于实例化对象的通用方法,如下所示:
@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);
我对这个问题模棱两可。