autowired spring return null

Solutions on MaxInterview for autowired spring return null by the best coders in the world

showing results for - "autowired spring return null"
Sofie
18 Oct 2018
1@Controller
2public class Controller {
3
4  @GetMapping("/example")
5  public String example() {
6    MyService my = new MyService();
7    my.doStuff();
8  }
9}
10
11@Service
12public class MyService() {
13
14  @Autowired
15  MyRepository repo;
16
17  public void doStuff() {
18    repo.findByName( "steve" );
19  }
20}
21
22 
23
24@Repository
25public interface MyRepository extends CrudRepository<My, Long> {
26
27  List<My> findByName( String name );
28}
29
Kevin
25 Jun 2020
1@Controller
2public class Controller {
3
4  @Autowired
5  MyService service;
6
7  @GetMapping("/example")
8  public String example() {
9    service.doStuff();
10  }
11}
12
13@Service
14public class MyService() {
15
16  @Autowired
17  MyRepository repo;
18
19  public void doStuff() {
20    repo.findByName( "steve" );
21  }
22}
23
24@Repository
25public interface MyRepository extends CrudRepository<My, Long> {
26
27  List<My> findByName( String name );
28}
29