简体   繁体   English

Spring Boot自动配置

[英]Spring boot auto configuration

In my use case I have 2 projects say projectA and projectB projectB is added as maven dependency in projectA. 在我的用例中,我有2个项目,说projectA和projectB projectB被添加为projectA中的Maven依赖项。 From projectA I am trying to call projectB services. 从projectA我试图调用projectB服务。 For that I have created a config class in projectB : 为此,我在projectB中创建了一个配置类:

@Configuration
public class BeanConfig {

    @Bean
    @ConditionalOnMissingBean
    public MyService<String, Object> myBean(){
        return new MyServiceImpl();
    }
}

and in MyServiceImpl I am autowiring MyDao. 在MyServiceImpl中,我正在自动装配MyDao。 Error is it is not able to create a bean for MyDao. 错误是无法为MyDao创建bean。

In case if I am mentioning below in projectA main class. 如果我在下面在projectA主类中提到。

@SpringBootApplication(scanBasePackages = { "org.my.service" }) 

Everything runs fine. 一切运行正常。

How can I create Bean without mentioning base package in project A. 如何在不提及项目A中的基础包的情况下创建Bean。

You are instantiating the MyServiceImpl 您正在实例化MyServiceImpl

return new MyServiceImpl();

So Spring can't autowire anything in this class. 因此,Spring无法自动连接此类中的任何内容。

This has nothing to do with your multi module project. 这与您的多模块项目无关。

Either you use Autowiring or you have to set the DAO on the service class in the configuration method. 您可以使用自动装配,也可以在配置方法中在服务类上设置DAO。

If you want to scan individual beans without enabling component scan for entire package you can use: 如果要扫描单个Bean而不启用整个软件包的组件扫描,则可以使用:

@Import(MyServiceImpl.class)

Then this particular bean will be spring enabled and scanned for appropriate spring annotations. 然后,将使该特定的bean启用spring并扫描适当的spring注释。

UPDATED: As the other have already pointed out when you create your service through 更新:正如其他人已经指出的那样,当您通过以下方式创建服务时

return new MyServiceImpl();

This type of creation will dissable the autowiring capabilities for the MyServiceImpl. 这种类型的创建将禁用MyServiceImpl的自动装配功能。 The reason for this is that once you call the constructor yourself Spring no longer participates in the creation of the bean and its dependencies. 原因是,一旦您自己调用构造函数,Spring便不再参与Bean及其依赖项的创建。

The mechanism I have pointed you out through @Import declaration presumes that you have not enabled component scanning for your projectB from projectA. 我通过@Import声明指出的机制假定您尚未为projectA中的projectB启用组件扫描。 In that case if we asume that BeanConfig resides in projectA. 在这种情况下,如果我们假设BeanConfig驻留在projectA中。 Then you need to ensure 2 things: 然后,您需要确保两件事:

1.Your repository in projectB should be annotated with @Repository, as your MyServiceImpl should be annotated with @Service 2.In your BeanConfig on a class level you should place the following definitions: 1.您的projectB中的存储库应使用@Repository进行注释,因为MyServiceImpl应使用@Service进行注释。2.在BeanConfig的类级别上,应放置以下定义:

@Configuration
@Import({MyServiceImpl.class, MyDao.class})
public class BeanConfig {


}

Since MyServiceImpl and MyDao are annotated with Service and Repository, you will no longer need the annotated with @Bean method myBean. 由于MyServiceImpl和MyDao用服务和存储库注释,因此您将不再需要使用@Bean方法myBean注释。

For the @Repository annotation in projectB to work you need to have Repositories enabled at projectA. 为了使projectB中的@Repository批注起作用,您需要在projectA上启用存储库。 Make sure that you have it enabled through EnableJpaRepositories either by importing relevant class from projectB containing this annotation, or by annotating a Config class in projectA. 通过从包含该批注的projectB导入相关类,或通过在projectA中注释Config类,确保通过EnableJpaRepositories启用了它。

If you have your spring boot application structured properly, then you should not have to specify a base package to pick up your configuration. 如果您的Spring Boot应用程序的结构正确,则不必指定基本软件包即可进行配置。 Spring boot should naturally scan the package containing your configuration and register your beans accordingly. Spring Boot应该自然地扫描包含您的配置的软件包,并相应地注册您的bean。

Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html 参考: https : //docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html

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

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