简体   繁体   English

如何检查Spring-boot或相关框架的所有合格bean

[英]How can I check all qualifying beans for Spring-boot or related frameworks

I want to know how to discover all qualifying beans that should be implemented when using some component together Spring. 我想知道如何在Spring中使用某些组件时发现应该实现的所有限定bean。

For example, when using JPA and Spring Boot we need beans as like as sessionFactory and dataSource (Any more? I don't know, see It?). 例如,当使用JPA和Spring Boot时,我们需要像sessionFactory和dataSource一样的bean(还有更多?我不知道,看到它?)。

Is there any official place where I can check the pre requisite list of beans? 有没有官方的地方我可以查看预先要求的豆类清单?

I've made many searches through the official schemas and I couldn't find a pattern for bean dependencies. 我通过官方模式进行了很多搜索,但我找不到bean依赖模式。

After some time I've come into some steps that helped me and maybe help others. 过了一段时间,我已经采取了一些措施来帮助我,也许可以帮助别人。 I'm answering this also because there are a lot of material related to Beans in XML files and in a Maven project, but if you try to create a XML free project, things become more rare when using Spring. 我也在回答这个问题,因为在XML文件和Maven项目中有很多与Beans相关的资料,但是如果你尝试创建一个XML免费项目,那么在使用Spring时事情变得更加罕见。

Notice: in deep Spring doesn't cares if the bean is an annotation or inside a XML. 注意:在深度Spring中,不关心bean是注释还是XML内部。 I preferred these configurations as a personal choice. 我更喜欢这些配置作为个人选择。

Some project details: 一些项目细节:

  • Gradle (project configurations are in application.properties file) Gradle(项目配置在application.properties文件中)

  • Java 8 Java 8

  • Bean class(es) for src and for test. 用于src和测试的Bean类。 Note: You can use extends among them. 注意:您可以在其中使用扩展。

  • In case of doubt, https://start.spring.io/ an awesome way to create from nothing a spring project with your dependencies. 如果有疑问, https ://start.spring.io/是一个很棒的方法来创建一个带有依赖项的spring项目。 Gradle format dependencies usually can be found at maven repository nearby the maven format. Gradle格式依赖关系通常可以在maven格式附近的maven存储库中找到。

Resolution: 解析度:

  • Create a unit test (eg JUnit) to just test your layer. 创建一个单元测试(例如JUnit)来测试你的图层。 For example mine was persistence layer. 例如我的是持久层。

  • Be careful to add each framework(that is going to be loaded by Spring) at once. 小心添加每个框架(将由Spring加载)。 Dealing with many unconfigured frameworks at same time would lead to caos since may be hard to figure out from where the Bean class should come from. 同时处理许多未配置的框架会导致问题,因为可能很难弄清楚Bean类应该来自何处。

  • When the test fails look at his bottom of your console. 当测试失败时,请查看控制台的底部。 Some exception will be thrown saying that a Bean with some name is missing. 将抛出一些异常,说缺少某个名称的Bean。 This are the guys you need. 这是你需要的人。

  • Create a method typed with @Bean inside a class typed with @Configuration. 在使用@Configuration键入的类中创建使用@Bean键入的方法。 This method must returns variable/object type complained in the exception message. 此方法必须返回异常消息中抱怨的变量/对象类型。 Additional annotations maybe be required, for example, @EnableTransactionManagement for JPA beans. 可能需要额外的注释,例如,JPA bean的@EnableTransactionManagement。

Bonus: 奖金:

Spring is deeply connected to IoC (dependency injection) and containers. Spring与IoC(依赖注入)和容器密切相关。 Take a look at this contents because when you see that you realizes why to use Beans. 看一下这个内容,因为当你看到你意识到为什么要使用Beans时。

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

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