【发布时间】:2019-09-14 15:12:12
【问题描述】:
我正在尝试将 Spring Boot 应用程序的一项服务用于另一个 Spring Boot 服务。由于一些限制,我必须使用基于 jar 的方法,即,我正在使用命令 maven build 构建第一个项目,并使用为该项目创建的 jar。
我正在将该 jar 添加到其他/依赖项目的构建路径中。但是我看不到我的主要项目的服务。我也无法自动装配它们。几天前,不知何故,我能够看到服务,但依赖项目的 maven 构建失败,因为它无法在依赖项目中找到自动装配服务的源包(一个明显的失败,因为该包在主项目中)。我已经尝试了很多事情,但我不知道现在如何进行。
主项目
JartestApplication.java
@SpringBootApplication
public class JartestApplication
{
public static void main(String[] args) throws Exception
{
SpringApplication.run(JartestApplication.class, args);
}
}
Service.java
@FunctionalInterface
public interface DbService
{
public BigInteger getRowCount(String tableName) throws Exception;
}
ServiceImpl.java
@Service
public class DbServiceImpl implements DbService
{
@Autowired
EntityManager em;
@Override
public BigInteger getRowCount(String tableName) throws Exception
{
return (BigInteger) em.createNativeQuery("select count(*) from "+tableName).getSingleResult();
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>jartest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jartest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
依赖项目
ImportjartestApplication.java
@SpringBootApplication
public class ImportjartestApplication
{
public static void main(String[] args)
{
ApplicationContext context = SpringApplication.run(ImportjartestApplication.class, args);
Test test = context.getBean(Test.class);
System.err.println(test.check("test"));
}
}
Test.java
@FunctionalInterface
public interface Test
{
public BigInteger check(String name);
}
TestImpl.java
@Service
public class TestImpl implements Test
{
//@Autowired --- what i want to do
//DbService service; --- service is not visible even after i have added the jar of main project into the build path of this project
@Override
public BigInteger check(String name)
{
return null;
//return service.getRowCount(name); -- my actual aim
}
}
有没有其他方法可以在不共享代码的情况下共享我的服务?
由于某些限制,我无法将我的服务公开为休息服务,因此我尝试将主服务部署为 jar 并将其添加到依赖项目的构建路径中。
【问题讨论】:
标签: java spring-boot