简体   繁体   English

将项目A(Spring 4.2.x)添加为项目B(Spring Boot 2,Spring 5)的依赖项

[英]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具有以下规格:

  • Jar
  • Java 7 Java 7
  • Spring 4.2.0 + Spring Security Spring 4.2.0 + Spring安全性
  • Spring Data JPA, Oracle, H2 (w/ scope test) Spring Data JPA,Oracle,H2(带范围测试)

I've Project B with the following specs: 我的Project B具有以下规格:

  • War 战争
  • Java 8 Java 8
  • Tomcat 8.5.x Tomcat 8.5.x
  • Spring Boot 2 春季靴2
  • Spring 5 + Spring Security Spring 5 + Spring安全性

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 文件->项目结构

  • Project -> Ensure Project A's SDK + Project level are set to Java 8 项目->确保项目A的SDK +项目级别设置为Java 8
  • Modules -> Select Project A -> Ensure SDK + Project level are set to Java 8 for Sources + Dependencies 模块->选择项目A->确保“源+依赖关系”的SDK +项目级别设置为Java 8
  • Modules -> Click + button > Import Module -> Select Project A's pom.xml -> Follow import steps 模块->单击+按钮>导入模块->选择项目A的pom.xml->遵循导入步骤
  • Modules -> Select Project B -> Ensure SDK + Project level are set to Java 8 for Sources + Dependencies 模块->选择项目B->确保“源+依赖关系”的SDK +项目级别设置为Java 8
  • Modules -> Select Project A -> Dependencies -> Click + button > Add Module Dependency -> Select Project A 模块->选择项目A->依赖性->单击+按钮>添加模块依赖性->选择项目A
  • Add Project A as dependency into Project B's pom.xml, and match dependency's version with Project A's version (from pom) 将项目A作为依赖项添加到项目B的pom.xml中,并将依赖项的版本与项目A的版本进行匹配(来自pom)

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.

相关问题 spring-ws 2.2.2与spring 4.2.x兼容吗? - Is spring-ws 2.2.2 compatible with spring 4.2.x? 在Spring Boot项目中找不到依赖项 - Dependency not found in Spring Boot project 将 spring boot maven 项目作为依赖添加到另一个项目(本地) - Add spring boot maven project as dependency to another project (locally) Spring Heroku 中的引导项目作为对另一个本地 Spring 引导项目的依赖项? - Spring boot project in Heroku as dependency to another local Spring Boot project? Spring 引导项目 jar 作为另一个项目中的依赖项 - Spring Boot project jar as dependency in another project 如何在另一个Spring启动项目中添加本地spring启动项目作为依赖项 - How to add local spring boot project as a dependency in another spring boot project 无法将 JPA 依赖项添加到 spring-boot 项目中 - Not able to add JPA dependency into spring-boot project 如何在Spring Boot项目中添加多个本地罐子作为依赖项 - How to add in spring boot project multiple local jars as dependency 如何在不改变原有pom的情况下,在新的spring-boot项目中添加spring-boot jar作为依赖 - How to add a spring-boot jar as a dependency in a new spring-boot project without changing the original pom 如何在 spring-boot 项目中向 pom.xml 添加非 spring-boot 依赖 - How to add a non-spring-boot dependency to the pom.xml in a spring-boot project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM