The point is, in fact, there's no magic in the Spring, all it does is quite simple code challenges.@Autowired It is indeed intended to automatically install values, but I repeat, there is no magic, and for this to happen, the Spring needs to be (sometimes) created and processed in its DI Container. In your case, it's not happening because the spring is not involved:// магии не произошло, потому что @Autowired действует не на уровне языка,
// а на уровне Spring
UsersClass usersClass = new UsersClass();
// JVM создала новый объект с неициниализированным полем, оттуда вы и получаете null
To get a normal copy of the field filled, you need to use the Spring container. Anyway, it happens the same way. @Autowired:@Controller
public class CustomController {
@Autowired
private UsersClass userRepository;
@RequestMapping("/user")
public List<User> getAll() {
userRepository.getAll();
return null;
}
}
In this case, SpringThere will be an annotated @Component on your UsersClass. @ComponentScan)I'll find all the @utowired-annotations in it.Create a new copy of UsersClass. @Autowired- Designers, but not yet.Found in a container SessionFactory (or creates it)Reflects the fieldAdd a copy UsersClass containerAfter which the counter-eller initiates similarly