简体   繁体   English

@Autowire 如何在不使用 @Bean 注释的情况下获取 spring bean

[英]How @Autowire gets the spring bean without using @Bean annotation

I have created on springboot project where there is a JPA class:-我在springboot项目上创建了一个JPA class:-

package com.example.demo.jpa;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.model.Users;

@Repository
public interface AppRepo extends CrudRepository<Users, Integer>, AppRepoCustom {

    public List<Users> findAllByJob(String job);

}

Another AppRepoCustom Interface is like this:另一个 AppRepoCustom 接口是这样的:

package com.example.demo.jpa;

import java.util.List;

public interface AppRepoCustom {
    public List<String> getAllNames();

}

Implementation of the interface:-接口的实现:-

package com.example.demo.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.example.demo.model.Users;

public class AppRepoCustomImpl implements AppRepoCustom {

    @PersistenceContext
    EntityManager entityManager;

    @Override
    public List<String> getAllNames() {
        Query query = entityManager.createNativeQuery("SELECT name FROM springbootdb.Users as em ", Users.class);
        return query.getResultList();
    }

}

Now inside my controller class I am injecting the AppRepo object现在在我的 controller class 里面我正在注入 AppRepo object

@Autowired
AppRepo appRepo;

My question is I haven't specified anywhere that which implementation of AppRepo to be injected then how spring is able to inject it without any errors?我的问题是我没有在任何地方指定要注入哪个 AppRepo 实现,那么 spring 如何能够在没有任何错误的情况下注入它? When we create a object of type interface like Interface objectName = new implClass();当我们创建一个接口类型的 object 时,例如 Interface objectName = new implClass(); where implClass contains all the implementation of the interface methods.but in the above example some implementations are in are in CrudRepository class and some are in AppRepoCustom so How does this object creation works here?其中 implClass 包含接口方法的所有实现。但在上面的示例中,一些实现在 CrudRepository class 中,一些在 AppRepoCustom 中,所以这个 object 创建在这里如何工作? I am confused.我很困惑。 How internally objects in being created when we create object like Interface objectName = new implClass();and in the given scenario.当我们创建 object(如 Interface objectName = new implClass(); 以及在给定的场景中)时,如何在内部创建对象。

It will be ambiguous if you @Autowired AppRepoCustom .如果您@Autowired AppRepoCustom将是模棱两可的。 But in your case you have @Autowired AppRepo which is child of AppRepoCustom interface.但是在您的情况下,您有@Autowired AppRepo ,它是AppRepoCustom接口的子接口。 So Spring knows you have asked to provide bean of child interface and provides it without error.所以 Spring 知道您已经要求提供子接口的 bean 并且没有错误地提供它。

As far as which concrete implementation will be autowired in case of AppRepo , See the following reference from spring documentation.至于在AppRepo的情况下将自动装配哪个具体实现,请参阅 spring 文档中的以下参考。

In this case we instruct Spring to scan com.acme.repositories and all its sub packages for interfaces extending Repository or one of its sub-interfaces.在这种情况下,我们指示 Spring 扫描 com.acme.repositories 及其所有子包,以查找扩展 Repository 的接口或其子接口之一。 For each interface found it will register the persistence technology specific FactoryBean to create the according proxies that handle invocations of the query methods.对于找到的每个接口,它将注册持久性技术特定的 FactoryBean 以创建相应的代理来处理查询方法的调用。

For further details read documentation .有关更多详细信息,请阅读文档

Spring boot application scans all classes with tags ie @repository, @Component, @Bean, @Controller etc and create objects. Spring 启动应用程序扫描所有带有标签的类,即@repository、@Component、@Bean、@Controller 等并创建对象。 Also, in your case, you have @Autowired the class @Apprepo so there is no conflict, it should work perfectly fine.此外,在您的情况下,您已经 @Autowired class @Apprepo 因此没有冲突,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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