Answers for "autowired spring return null"

0

autowired spring return null

@Controller
public class Controller {

  @GetMapping("/example")
  public String example() {
    MyService my = new MyService();
    my.doStuff();
  }
}

@Service
public class MyService() {

  @Autowired
  MyRepository repo;

  public void doStuff() {
    repo.findByName( "steve" );
  }
}

 

@Repository
public interface MyRepository extends CrudRepository<My, Long> {

  List<My> findByName( String name );
}
Posted by: Guest on May-07-2021
0

autowired spring return null

@Controller
public class Controller {

  @Autowired
  MyService service;

  @GetMapping("/example")
  public String example() {
    service.doStuff();
  }
}

@Service
public class MyService() {

  @Autowired
  MyRepository repo;

  public void doStuff() {
    repo.findByName( "steve" );
  }
}

@Repository
public interface MyRepository extends CrudRepository<My, Long> {

  List<My> findByName( String name );
}
Posted by: Guest on May-07-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language