[英]Add Project A (Spring 4.2.x) as dependency of Project B (Spring Boot 2, Spring 5)
I've Project A with the following specs: 我的Project A具有以下规格:
I've Project B with the following specs: 我的Project B具有以下规格:
Both projects successfully build and run individually. 两个项目均成功构建并单独运行。 I want Project A be a dependency of Project B. I'm using IntelliJ and followed steps available on the web ( #1 , #2 ), but here's the gist of what I did: 我希望项目A是项目B的依赖项。我正在使用IntelliJ并遵循Web上可用的步骤( #1 , #2 ),但这是我所做的要点:
File -> Project Structure 文件->项目结构
I successfully run "mvn clean install". 我成功运行了“ MVN全新安装”。 When I run Project B on Tomcat, I get: 当我在Tomcat上运行Project B时,我得到:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-08-14 15:01:03.796 ERROR 15888 --- [on(3)-127.0.0.1] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
I'm not sure why Project A's JPA/DB config is causing issues in Project B, even though Project A works fine by itself. 我不确定为什么项目A的JPA / DB配置会在项目B中引起问题,即使项目A本身可以正常工作也是如此。 But, after some research, I added the following annotation to my SpringBoot Application: 但是,经过一番研究,我在SpringBoot应用程序中添加了以下注释:
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
and this is what I ended up with: 这就是我最终得到的结果:
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class ProjectB extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ProjectB.class);
}
public static void main(String[] args) {
SpringApplication.run(ProjectB.class, args);
}
}
I successfully run "mvn clean install". 我成功运行了“ MVN全新安装”。 I run Project B again, and it launches successfully! 我再次运行项目B,它成功启动! when I try to reference anything from Project A (ie A service), it builds fine, but when launching I get the following: 当我尝试引用Project A(即A服务)中的任何内容时,它可以正常运行,但是启动时会得到以下信息:
SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
***************************
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
APPLICATION FAILED TO START
***************************
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)
Description:
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Parameter 0 of constructor in com.projectB.controller.ControllerName required a bean of type 'com.projecta.service.ServiceName' that could not be found.
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
Action:
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
Consider defining a bean of type 'com.projecta.service.ServiceName' in your configuration.
Is it even possible to import Spring module into Spring Boot 2? 甚至可以将Spring模块导入Spring Boot 2吗? If so, what am I missing? 如果是这样,我想念什么? If not, what're my options? 如果没有,我有什么选择?
Edit 1: Add Simple service (Project A) + controller (Project B) 编辑1:添加简单服务(项目A)+控制器(项目B)
// Controller in Project B
@RestController
@RequestMapping("/api")
public class SimpleController {
private SimpleService simpleService;
@Autowired
public SimpleController(SimpleService simpleService) {
this.simpleService = simpleService;
}
@GetMapping
public ResponseEntity<?> generateAndSendEmail() {
boolean success = simpleService.callSimpleService();
if (success) {
return new ResponseEntity<>(OK);
}
return new ResponseEntity<>(INTERNAL_SERVER_ERROR);
}
}
// Service in Project A
@Service
public class SimpleService {
public boolean callSimpleService() {
return true;
}
}
When you include projectA as a maven dependency in Project B, all the class files in projectA become available on your classpath. 当将projectA作为maven依赖项包含在Project B中时,projectA中的所有类文件在您的类路径上都可用。 Nothing more. 而已。
When you add a service as an autowired dependency, the spring container (of project B) expects an implementing bean to be available in the context. 当您将服务添加为自动装配的依赖项时,(项目B的)spring容器期望实现Bean在上下文中可用。
The spring context will not be able to find an implementation of ServiceA, because it does not scan the packages of projectA. Spring上下文将无法找到ServiceA的实现,因为它不会扫描projectA的程序包。 The default implementation of @ComponentScan in projectB will only recognize beans declared in sub packages of Project B. To make the beans from Project A to be available in Project B you need to add an explicit @ComponentScan in Project B's main class instructing spring to scan the packages in Project A. projectB中@ComponentScan的默认实现将仅识别在Project B子包中声明的bean。要使Project A中的bean在Project B中可用,您需要在Project B的主类中添加一个显式@ComponentScan,以指示spring进行扫描项目A中的软件包。
It is also advisable to remove Spring 4.X dependencies from Project A's pom file 还建议从Project A的pom文件中删除Spring 4.X依赖项
Have you tried manually creating the service as a bean your spring boot app? 您是否尝试过将服务作为Spring Boot应用程序的bean手动创建? Inside your main class: 在您的主要课程中:
@Bean
public SimpleService simpleService() {
return new SimpleService();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.