Java AutoWired JPA存储库在控制器的某些方法中为空

Java AutoWired JPA存储库在控制器的某些方法中为空,java,spring,spring-boot,spring-mvc,spring-data-jpa,Java,Spring,Spring Boot,Spring Mvc,Spring Data Jpa,我有一个像这样的假设 @Repository public interface RestaurantRepo extends JpaRepository<Restaurant, String> { Optional<Restaurant> findByName(String name); Optional<List<Restaurant>> findAllByMerchantId(String merchantId); } @A

我有一个像这样的假设

@Repository
public interface RestaurantRepo extends JpaRepository<Restaurant, String> {
    Optional<Restaurant> findByName(String name);

    Optional<List<Restaurant>> findAllByMerchantId(String merchantId);
}
@Autowired
RestaurantRepo restaurantRepo;
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
    @GetMapping("/disable")
    public ResponseEntity<CustomResponse> disableUser(String username, boolean disable) {

        Restaurant restaurant = restaurantRepo.findById(username).get();
        restaurant.setDisabled(disable);

        CustomResponse er = new CustomResponse();
        er.setStatus("Successful");
        if (disable) {
            er.setMessage("Restaurant is disabled");
        } else {
            er.setMessage("Restaurant is enabled");
        }
        restaurantRepo.save(restaurant);
        return new ResponseEntity<>(er, new HttpHeaders(), HttpStatus.OK);
    }
@PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN','ROLE_RESTAURANT')")
    @GetMapping("/get")
    private ResponseEntity<Restaurant> getRestaurant(Principal principal) {
        String username = principal.getName();
        System.out.println("username "+username); //prints the valid username
        System.out.println("RestRepo "+restaurantRepo); //prints null
        Restaurant restaurant;
        Optional<Restaurant> opt = restaurantRepo.findById(username); //prints Exception here
        if (opt.isPresent()) {
            restaurant = opt.get();
            return new ResponseEntity<>(restaurant, HttpStatus.OK);
        } else {
            restaurant = new Restaurant();
            return new ResponseEntity<>(restaurant, HttpStatus.NOT_FOUND);
        }

    }
然后我有一种方法,我使用的存储库如下

@Repository
public interface RestaurantRepo extends JpaRepository<Restaurant, String> {
    Optional<Restaurant> findByName(String name);

    Optional<List<Restaurant>> findAllByMerchantId(String merchantId);
}
@Autowired
RestaurantRepo restaurantRepo;
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
    @GetMapping("/disable")
    public ResponseEntity<CustomResponse> disableUser(String username, boolean disable) {

        Restaurant restaurant = restaurantRepo.findById(username).get();
        restaurant.setDisabled(disable);

        CustomResponse er = new CustomResponse();
        er.setStatus("Successful");
        if (disable) {
            er.setMessage("Restaurant is disabled");
        } else {
            er.setMessage("Restaurant is enabled");
        }
        restaurantRepo.save(restaurant);
        return new ResponseEntity<>(er, new HttpHeaders(), HttpStatus.OK);
    }
@PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN','ROLE_RESTAURANT')")
    @GetMapping("/get")
    private ResponseEntity<Restaurant> getRestaurant(Principal principal) {
        String username = principal.getName();
        System.out.println("username "+username); //prints the valid username
        System.out.println("RestRepo "+restaurantRepo); //prints null
        Restaurant restaurant;
        Optional<Restaurant> opt = restaurantRepo.findById(username); //prints Exception here
        if (opt.isPresent()) {
            restaurant = opt.get();
            return new ResponseEntity<>(restaurant, HttpStatus.OK);
        } else {
            restaurant = new Restaurant();
            return new ResponseEntity<>(restaurant, HttpStatus.NOT_FOUND);
        }

    }
@预授权(“hasRole('ROLE\u SUPER\u ADMIN'))
@GetMapping(“/disable”)
公共响应属性禁用用户(字符串用户名,布尔禁用){
Restaurant Restaurant=restaurantRepo.findById(用户名).get();
餐厅。设置禁用(禁用);
CustomResponse er=新CustomResponse();
er.setStatus(“成功”);
如果(禁用){
er.setMessage(“餐厅被禁用”);
}否则{
er.setMessage(“餐厅已启用”);
}
拯救餐厅(餐厅);
返回新的ResponseEntity(呃,newhttpheaders(),HttpStatus.OK);
}
每当我使用这个api时,它都可以正常工作。 然后我有另一种方法像这样

@Repository
public interface RestaurantRepo extends JpaRepository<Restaurant, String> {
    Optional<Restaurant> findByName(String name);

    Optional<List<Restaurant>> findAllByMerchantId(String merchantId);
}
@Autowired
RestaurantRepo restaurantRepo;
@PreAuthorize("hasRole('ROLE_SUPER_ADMIN')")
    @GetMapping("/disable")
    public ResponseEntity<CustomResponse> disableUser(String username, boolean disable) {

        Restaurant restaurant = restaurantRepo.findById(username).get();
        restaurant.setDisabled(disable);

        CustomResponse er = new CustomResponse();
        er.setStatus("Successful");
        if (disable) {
            er.setMessage("Restaurant is disabled");
        } else {
            er.setMessage("Restaurant is enabled");
        }
        restaurantRepo.save(restaurant);
        return new ResponseEntity<>(er, new HttpHeaders(), HttpStatus.OK);
    }
@PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN','ROLE_RESTAURANT')")
    @GetMapping("/get")
    private ResponseEntity<Restaurant> getRestaurant(Principal principal) {
        String username = principal.getName();
        System.out.println("username "+username); //prints the valid username
        System.out.println("RestRepo "+restaurantRepo); //prints null
        Restaurant restaurant;
        Optional<Restaurant> opt = restaurantRepo.findById(username); //prints Exception here
        if (opt.isPresent()) {
            restaurant = opt.get();
            return new ResponseEntity<>(restaurant, HttpStatus.OK);
        } else {
            restaurant = new Restaurant();
            return new ResponseEntity<>(restaurant, HttpStatus.NOT_FOUND);
        }

    }
@预授权(“hasAnyRole('ROLE\u SUPER\u ADMIN','ROLE\u RESTAURANT'))
@GetMapping(“/get”)
私人餐厅(主要负责人){
字符串username=principal.getName();
System.out.println(“用户名”+用户名);//打印有效的用户名
System.out.println(“RestRepo”+restaurantRepo);//打印空值
食肆;;
可选opt=restaurantRepo.findById(用户名);//此处打印异常
if(opt.isPresent()){
restaurant=opt.get();
返回新的ResponseEntity(餐厅,HttpStatus.OK);
}否则{
餐厅=新餐厅();
返回新的ResponseEntity(餐厅、HttpStatus.未找到);
}
}
在这里,我在这一行中得到一个NullPointerException
Optional=restaurantRepo.findById(用户名)

用户名是有效的用户名,因此当我打印
restaurantRepo
时,我得到
null


我不明白为什么变量在一个方法中不为null,而在另一个方法中为null。任何帮助都将不胜感激。谢谢,我不确定。但请尝试将第二种方法中的访问修饰符更改为public
请参考这个


也使用构造函数注入而不是@Autowired annotation。

方法disableUser和getRestaurant在同一控制器中吗?@Niyas是的,它们在同一控制器中