Spring mvc 在Spring MVC中@AutoWrite服务时,指针为空
我使用的是SpringMVC框架 当我在AbstractPdfView中使用@Autowried声明服务时,commonService总是空指针Spring mvc 在Spring MVC中@AutoWrite服务时,指针为空,spring-mvc,Spring Mvc,我使用的是SpringMVC框架 当我在AbstractPdfView中使用@Autowried声明服务时,commonService总是空指针 public class DetailPdfView extends AbstractPdfView { @Autowired private CommonService commonService; protected PdfPCell genQRCodeCell(String content){
public class DetailPdfView extends AbstractPdfView {
@Autowired
private CommonService commonService;
protected PdfPCell genQRCodeCell(String content){
String result="";
try {
Image img = Image.getInstance(commonService.genQRCode(content));
img.scaleToFit(120, 120);
PdfPCell cell = new PdfPCell(img);
return cell;
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}
您必须启用基于注释的配置 例如,如果您使用的是xml配置,那么可以包括
<context:annotation-config />
有关更多详细信息,请参阅
此外,您的bean必须由Spring实例化,而不是简单地使用newdetailpdfview创建,以便Spring处理注释。DetailPdfView是否在Spring配置中的某个地方声明为bean?
只有在xml配置中将DetailPdfView声明为bean时,spring才会处理@Autowired
或者在您的例子中,使用spring的原型注释@component、@Service等进行注释,并启用组件扫描作为您如何获得DetailPdfView的实例?如果您直接使用新的。。。它不会从DI容器中获取,而不会初始化自动连线属性。