Spring 3 @Autowired 在 Formatter 中抛出 NPE

发布于 2024-11-18 12:51:53 字数 1764 浏览 2 评论 0原文

我已经编写了自己的格式化程序并尝试将服务自动装配到其中,但我收到了 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" />

Registering of formatters:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2024-11-25 12:51:53

您很可能使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文