Spring 3 @Autowired 在 Formatter 中抛出 NPE
我已经编写了自己的格式化程序并尝试将服务自动装配到其中,但我收到了 NullPointerException
。
格式化程序:
@Component
public class DepartmentFormatter implements Formatter<Department> {
@Autowired
private DepartmentService departmentService;
@Override
public String print(Department department, Locale locale) {
return department.getName();
}
@Override
public Department parse(String string, Locale locale) throws ParseException {
return departmentService.getByName(string); // NPE thrown here
}
}
服务:
@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentDAO departmentDAO;
/* ... */
}
在我的 spring-servlet.xml 中,我有
<context:annotation-config />
<context:component-scan base-package="net.kurochenko.sampleapp" />
public class FormattingFactory extends FormattingConversionServiceFactoryBean {
@Override
public void installFormatters(FormatterRegistry registry) {
super.installFormatters(registry);
registry.addFormatterForFieldAnnotation(new AuthorAnnotationFormatterFactory());
registry.addFormatterForFieldAnnotation(new DepartmentAnnotationFormatterFactory());
}
}
FormattingFactory bean
<mvc:annotation-driven conversion-service="formattingFactory" />
所有上述类都位于 net.kurochenko.sampleapp 包内。
@Controller
中的自动装配服务工作正常。我在谷歌上寻找解决方案并尝试了其中的一些,但异常仍然存在。我做错了什么?感谢您的建议。
i've written my own formatter and tried to autowire service into it, but i'm getting NullPointerException
.
Formatter:
@Component
public class DepartmentFormatter implements Formatter<Department> {
@Autowired
private DepartmentService departmentService;
@Override
public String print(Department department, Locale locale) {
return department.getName();
}
@Override
public Department parse(String string, Locale locale) throws ParseException {
return departmentService.getByName(string); // NPE thrown here
}
}
Service:
@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentDAO departmentDAO;
/* ... */
}
In my spring-servlet.xml i've got
<context:annotation-config />
<context:component-scan base-package="net.kurochenko.sampleapp" />
public class FormattingFactory extends FormattingConversionServiceFactoryBean {
@Override
public void installFormatters(FormatterRegistry registry) {
super.installFormatters(registry);
registry.addFormatterForFieldAnnotation(new AuthorAnnotationFormatterFactory());
registry.addFormatterForFieldAnnotation(new DepartmentAnnotationFormatterFactory());
}
}
FormattingFactory bean
<mvc:annotation-driven conversion-service="formattingFactory" />
All aforementioned classes are inside net.kurochenko.sampleapp package.
Autowiring service in @Controller
works fine. I was searching for solution on google and tried some of them, but exception still remains. What am I doing wrong? Thanks for advises.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能使用
new DepartmentFormatter()
注册您的格式化程序。它不会那样工作——spring没有机会注入依赖项。您应该注册 spring bean 实例(由 spring 创建)。无论是通过编程还是通过 xml。
You are most likely registering your formatter with
new DepartmentFormatter()
. It won't work that way - spring doesn't get the chance to inject dependencies.You should register the spring bean instance (created by spring). Be it programatically or via xml.