[英]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)
. 因为您正在使用BeanB
在BeanA
中执行某种实例化, 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.