@Autowired注释不适用于一个@Controller类并且为其他人工作

时间:2018-06-02 15:07:18

标签: spring spring-boot

我有三个@RestController个类,其中两个@Autowired正在注入bean,但是其中一个不是。我不知道问题是什么,因为几个小时前相同的代码工作正常 包com.learn.service;

package com.learn.service;

@Service
@Transactional
public class RoleService {

    @Autowired
    private RoleJpaRepository roleJpaRepository;

    public List<Role> findAll(){
        return roleJpaRepository.findAll();
    }
}

角色控制器

package com.learn.controller;

@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(method = RequestMethod.GET)
    private List<Role> findAll() {
        System.out.println(roleService);  // roleService is null here and NullPointerException is thrown from below method call.
        return roleService.findAll();
    }

}

服务的配置类

package com.learn.springConfig;

@Configuration
@ComponentScan("com.learn.service")
public class ServiceConfig {

    public ServiceConfig() {
        super();
    }

}    

跑步者

@SpringBootApplication
@Import({
    ContextConfig.class,
    PersistenceJpaConfig.class,
    ServiceConfig.class,
    WebConfig.class,
    SecurityConfig.class
})
public class WebservicesLearningApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebservicesLearningApplication.class, args);
    }
}    

对于相同的配置,User的控制器工作正常,其Service层与Role的包存在于同一个包中。

package com.learn.controller;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public List<User> findAll() {
        System.out.println(userService);
        List<User> users = userService.findAll();   
        return users;
    }

服务层

package com.learn.service;

@Service
@Transactional
public class UserService {

    @Autowired
    private UserJpaRepository userJpaRepository;

    public List<User> findAll(){
        return userJpaRepository.findAll();
    }
}

访问localhost:8080/api/users成功,但localhost:8080/api/roles给出NullPointerException

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.learn.controller.RoleController.findAll(RoleController.java:30) ~[classes/:na]............    

UPDATE1:
Web配置类

@Configuration
@ComponentScan(basePackages = {"com.learn.controller"})
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer{

    public WebConfig() {
        super();
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        Optional<HttpMessageConverter<?>> convertFound = converters.stream().filter(c -> c instanceof AbstractJackson2HttpMessageConverter).findFirst();
        if(convertFound.isPresent()) {
            final AbstractJackson2HttpMessageConverter converter = (AbstractJackson2HttpMessageConverter) convertFound.get();
            converter.getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
            converter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        }
    } 


} 

项目结构截图
project structure

Update2:我尝试在jUnit测试用例中使用@Autorired使用相同的UserService,一切都在那里工作。没有nullpointer异常。

@ContextConfiguration(classes = {PersistenceJpaConfig.class, ContextConfig.class, ServiceConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RoleTest {

    @Autowired
    private RoleService roleService ;

    @Test
    public void checkIfAllRolesCanBeRetrieved() {
        List<Role> roles = roleService.findAll();
        Assert.assertNotNull(roles);
    }


}

1 个答案:

答案 0 :(得分:0)

发生在我身上!在我的情况下,我有一个使用服务的控制器,该服务使用了一个没有@服务,@控制器或其他注释的类的方法,我注入服务然后当我使用父服务时出现错误null。

我希望它会被使用