小编C.K*_*öse的帖子

如何将Bean注入自定义参数解析器?

您好,我使用Spring Boot 1.3.2版本。我有一个自定义参数解析器,名称为ActiveCustomerArgumentResolver。一切都很好,resolveArgument方法工作正常,但是我无法初始化我的自定义arg的服务组件。解析器。生命周期流程有问题吗?这是我的代码:

import org.springframework.beans.factory.annotation.Autowired;
//other import statements

public class ActiveCustomerArgumentResolver implements HandlerMethodArgumentResolver {

@Autowired
private CustomerService customerService;

@Override
public boolean supportsParameter(MethodParameter parameter) {
    if (parameter.hasParameterAnnotation(ActiveCustomer.class) && parameter.getParameterType().equals(Customer.class))
        return true;
    else
        return false;
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    Principal userPrincipal = webRequest.getUserPrincipal();
    if (userPrincipal != null) {
        Long customerId = Long.parseLong(userPrincipal.getName()); 
        return customerService.getCustomerById(customerId).orNull(); //customerService is still NULL here, it keeps me getting NullPointerEx.
    } else { …
Run Code Online (Sandbox Code Playgroud)

dependency-injection autowired spring-boot

1
推荐指数
1
解决办法
1798
查看次数