简体   繁体   English

Spring Boot从另一个模块加载bean之前

[英]Spring Boot load beans from a different module before

I have two maven modules: 我有两个Maven模块:

api-module
commons-module

The api-module contains the package com.example.api and the commons-module contains the package com.example.commons . api模块包含软件包com.example.api ,commons模块包含软件包com.example.commons

When I run the main app com.example.api.ApiMain , the execution fails. 当我运行主应用程序com.example.api.ApiMain时 ,执行失败。 It is because I have Mongo Repository classes defined in the commons package. 这是因为我在commons包中定义了Mongo Repository类。 The API controllers depends on them and since they are not instantiated before the api beans, the execution fails. API控制器依赖于它们,并且由于它们没有在api bean之前实例化,因此执行失败。

Here is the main api app: 这是主要的API应用程序:

package com.example.api;

@SpringBootApplication
@ComponentScan({"com.example.commons", "com.example.api"})
public class ApiMain {
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    public static void main(String[] args) {
        SpringApplication.run(ApiMain.class, args);
    }
}

How do I make sure that com.example.commons components are scanned before the beans in com.example.api are loaded? 如何确保在加载com.example.api中的bean之前已扫描com.example.commons组件?

I can use @DependsOn annoation on every bean in com.example.api but there are several classes and several more will be added in future and it will make the code ugly. 我可以在com.example.api中的每个bean上使用@DependsOn注释,但是有几个类,将来还会添加更多的类,这会使代码变得丑陋。

This problem can be solved if there is a way to instruct spring to load components from the commons-module first. 如果有一种方法可以指示弹簧首先从公共模块加载组件,则可以解决此问题。

You may instruct me on how to perform this. 您可以指导我如何执行此操作。

Whatever package is scanned first should not matter, as Spring will build a dependency graph and figure out the order in which beans are instantiated. 不管首先扫描哪个包都没有关系,因为Spring会构建一个依赖图并找出实例化bean的顺序。 If you want certain beans to be instantiated before others, as in lets say BeanA depends on BeanB , then BeanA will have a constructor @Autowired BeanA(BeanB b) . 如果您希望某些bean在其他bean之前被实例化,比如说BeanA依赖于BeanB ,那么BeanA将具有一个构造函数@Autowired BeanA(BeanB b)

Figuring out the order of instantiating is a fundamental aspect of Spring Dependency Injection, I would suggest you read up a bit more on Spring DI because I don't think you are grasping the concept of inversion of control and what Spring DI does. 弄清楚实例化的顺序是Spring Dependency Injection的基本方面,我建议您进一步阅读Spring DI,因为我不认为您掌握了控制反转的概念以及Spring DI的作用。

But what it sounds like to me is you have something like: 但是对我来说听起来像是您有以下内容:

public class BeanA {
    @Autowired
    BeanB b;
    public BeanA() {
        b.doSomething();
    }
}

But b is still null when BeanA 's constructor is called. 但是当BeanA的构造函数时b仍然为null。 Because you are performing some sort of instantiation in BeanA with BeanB you get a NullPointerException , instead you have to have: BeanA(BeanB b) . 因为您正在使用BeanBBeanA中执行某种实例化, BeanB您将获得NullPointerException ,而必须具有: BeanA(BeanB b)

I was facing the same issue. 我面临着同样的问题。 I used below code for Rest API controller 我将以下代码用于Rest API控制器

@RestController
@EnableOAuth2Sso
@EnableResourceServer
@SpringBootApplication
public class SpringBootWebApplication extends WebSecurityConfigurerAdapter {

    //Dependancy injection using autowire
    @Autowired
    OAuth2ClientContext oauth2ClientContext;
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

Using this code for the normal controller which is rendering my HTML page 将此代码用于呈现我的HTML页面的普通控制器

@Configuration
@EnableAutoConfiguration
@Controller
public class WelcomeController {

Try this and let me know if this works or you still have some issue. 试试这个,让我知道这是否可行,或者您仍有问题。

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

相关问题 Spring Boot从库中的上下文xml加载bean - Spring Boot load beans from context xml in library Spring 引导 2 - 在 bean 初始化之前做一些事情 - Spring Boot 2 - Do something before the beans are initialized 从不同的模块自动装配bean - Autowiring beans from a different module Spring Boot Starter。 如何在启动程序“ x”完成bean创建之前停止初始化其他启动程序? - Spring Boot Starter. How to stop from initialization other starters before starter 'x' finish beans creation? Liquibase 是否在 Spring Boot 应用程序中注册所有 bean 之前运行? - Does Liquibase runs before registering all the beans in spring boot application? 如何使用 spring boot 批处理在不同的类中提供不同的 Bean - How to give different Beans in different classes using spring boot batch Spring在Main中使用任何bean之前从Configuration类加载/运行每个bean? - Spring load/runs every beans from Configuration class before any bean is used in main? 在Maven多模块项目中无法正常运行bean的Spring Boot自动装配 - Spring Boot Autowiring of beans is not working in maven multi module project Spring 引导:在测试中加载实现接口的所有 beans? - Spring Boot: load all beans implementing an interface in test? 在 Spring Boot 中检索 bean? - Retrieving beans in Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM