gpt4 book ai didi

java - 无法在 Spring Boot 项目中使用自定义 jar

转载 作者:行者123 更新时间:2023-12-02 09:31:24 26 4
gpt4 key购买 nike

我正在尝试将 Spring Boot 应用程序的一项服务使用到另一个 Spring Boot 服务中。由于一些限制,我必须使用基于 jar 的方法,即我正在使用命令 ma​​ven build 构建第一个项目并使用为该项目创建的 jar。

我正在将该 jar 添加到其他/依赖项目的构建路径中。但我看不到我的主要项目的服务。我也无法自动连接它们。几天前,不知何故,我能够看到服务,但依赖项目的 Maven 构建失败,因为它无法在依赖项目中找到 Autowiring 服务的源包(这是一个明显的失败,因为该包位于主项目中)。我已经尝试了很多事情,但我不知道现在如何继续。

主要项目

JartestApplication.java

@SpringBootApplication
public class JartestApplication
{

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

}

服务.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>

依赖项目

导入jartestApplication.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"));
}

}

测试.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 并将其添加到依赖项目的构建路径中。

最佳答案

after lots of debugging and reading the docs i found the solution and hereby i'm posting the steps to it....

1. Used the below build configuration in source project

<build>
<finalName>generator</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

2. Included the dependency into target project as system scope

<dependency>
<groupId>generator</groupId>
<artifactId>generator</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${project.basedir}\src\lib\generator.jar
</systemPath>
</dependency>

3. Included the following build configuration in target project


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--- begins - -->
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
<!--- ends - -->
</plugin>
</plugins>
</build>

4. Used component scan at root level in target class

@SpringBootApplication
@ComponentScan("com.example.jar.service")
public class JartestApplication
{
public static void main(String[] args) throws IOException
{
System.out.println("Hello world");
}
}

关于java - 无法在 Spring Boot 项目中使用自定义 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57936772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com