简体   繁体   中英

I'm new to Spring, can someone explain the beauty of this @Autowired?

I am new to Spring framwork, try to understand the @Autowired keywork in my example code. I have a user interface which extends from CrudRepository. User is the class of my User instance.

public interface UserRepository extends CrudRepository<User, Integer> { 
}

now in my Junit Test class I have some code:

@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
@Rollback(false)
public class UserRepositoryTests {

    @Autowired
    private UserRepository repo;
    @Autowired
    private TestEntityManager entityManager;

    @Test
    public void testCreatNewUserWithOneRole() {
        Role roleAdmin = entityManager.find(Role.class, 1);
        User userNamHM = new User("nam@codejava.net", "nam2020", "Nam", "Ha Minh");
        userNamHM.addRole(roleAdmin);

        User savedUser = repo.save(userNamHM);
        assertThat(savedUser.getId()).isGreaterThan(0);
    }

I really want to understand how @Autowired doing here and what happenend in backend. UserRepository is just a Interface contain some abstract method right? Why we can do private UserRepository repo; ? did we just create a object of a interface???

Much appreciate guys!

public interface UserRepository extends CrudRepository<User, Integer> { 
}

First the interface you have defined will generate a basic CRUD ( Create, Read, Update, Delete) Repository implementation with basic crud functions on startup. This can be extended by new interface methods and annotations ( Spring Data Repositories ). This generated implementation is not visible as source code, but you can be sure, that it exists during runtime. This implementation of your defined CRUD Repository is generated as Spring component, which basically means, that the Spring Framework will create an instance of this implementation class and manages this instance.

Spring is using the Inversion of Control principle also known as dependency injection as absolute core technology of the spring framework. Spring dependency injection By this dependency injection Spring will automatically set the values of fields to the correlated managed components or methods managed by spring, that are annotated by the @Autowired annotation. You are able to access to any kind of component and bind this to the related field or parameter. In your code you have the following source code:

@Autowired
private UserRepository repo;

This means, that during the instantiation of the class Spring will create a proxy setter for the field repo and assign the existing managed instance of a component of the type UserRepository. This dependency injection is only useable for managed beans. In your case you have the class UserRepositoryTests annotated with the annotation @DataJpaTest , which basically is a component, that will start a spring data jpa test. Thats the magic behind the @Autowired annotation. You may use this for Methods or constructors as well.

It may happen, that you have more than one instance of a class as component. In this case you have to select the correct bean instance by the qualifier annotation as below,

@Qualifier("fooBean")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM