Spring Boot独立应用引入Core Module Jar依赖异常问题求助
问题描述
将包含POJO、DAO、Service的core-module模块整合到Spring Boot独立应用时,生成可执行Jar包后出现UnsatisfiedDependencyException错误,且Jar包中不包含core-module的代码。该模块引入Spring Boot Web应用时运行正常,项目使用Java 8,core-module中的Service标注@Service、DAO标注@Repository。
独立应用模块pom.xml
<modelVersion>4.0.0</modelVersion> <groupId>com.project.standalone</groupId> <artifactId>StandaloneApp</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.boot.version>2.3.8.RELEASE</spring.boot.version> <failOnMissingWebXml>false</failOnMissingWebXml> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.project</groupId> <artifactId>core-module</artifactId> <version>1.0-SNAPSHOT</version> <type>jar</type> </dependency> </dependencies> <build> <finalName>api</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.2.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
core-module模块pom.xml
<modelVersion>4.0.0</modelVersion> <groupId>com.project</groupId> <artifactId>core-module</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.boot.version>2.3.8.RELEASE</spring.boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.4.Final</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${spring.boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.2.19.RELEASE</version> <type>jar</type> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> <type>jar</type> </dependency> <!-- ..some more dependencies --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.2.RELEASE</version> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
独立应用代码
@SpringBootApplication @EnableTransactionManagement @EnableScheduling @EnableJpaRepositories(basePackages = {"com.project.core"}) @EntityScan("com.fc.backend") @ComponentScan(basePackages = {"com.project.core", "com.project.standalone"}) public class StandaloneApp{ private final Logger LOGGER = LoggerFactory.getLogger(this.getClass()); public static void main(String[] args) { System.setProperty("spring.config.location", System.getProperty("user.home") + "/properties/application.yml"); SpringApplication.run(StandaloneApp.class, args); StandaloneApp testObj = new StandaloneApp(); testObj.testRunMethod(); } public void testRunMethod(){ System.out.println(".........Method Starts........"); UserService uService = new UserService(); List<User> activeUser = uService.getAllActiveUsers(); for (User user : activeUser) { if (StringUtils.isNotBlank(user.getWritePath()) && StringUtils.isNotBlank(user.getFtpPath())) { System.out.println(".........name........"+distributor); } } System.out.println(".........Method Ends........"); } }
报错信息
2023-06-28 18:18:08.939 WARN 17692 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'markupTypeService': Unsatisfied dependency expressed through field 'dcpsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'distributorClientProductSelectionService': Unsatisfied dependency expressed through field 'exchangeRateService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'exchangeRateService': Unsatisfied dependency expressed through field 'distributorDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'distributorDaoImpl': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.fc.backend.distributor.dao.impl.DistributorDaoImpl] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@31cefde0] 2023-06-28 18:18:08.939 INFO 17692 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
解决方案
1. 修复core-module的打包配置
core-module作为依赖模块,无需使用spring-boot-maven-plugin的repackage目标(该插件会将模块打包为可执行Jar,导致依赖无法被正确引入)。移除core-module pom.xml中的该插件配置:
<!-- 移除以下插件配置 --> <!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.2.RELEASE</version> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> -->
2. 统一Spring Boot插件版本
独立应用模块中spring-boot-maven-plugin版本(2.2.2.RELEASE)与Spring Boot依赖版本(2.3.8.RELEASE)不一致,将插件版本改为${spring.boot.version}:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring.boot.version}</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
3. 修复代码中的依赖注入问题
testRunMethod中直接new UserService()会绕过Spring依赖注入,导致Service内部的DAO无法被注入。改为通过Spring容器获取Bean:
// 修改main方法,获取Spring上下文 public static void main(String[] args) { System.setProperty("spring.config.location", System.getProperty("user.home") + "/properties/application.yml"); ConfigurableApplicationContext context = SpringApplication.run(StandaloneApp.class, args); StandaloneApp testObj = context.getBean(StandaloneApp.class); testObj.testRunMethod(); } // 注入UserService @Autowired private UserService uService; public void testRunMethod(){ System.out.println(".........Method Starts........"); List<User> activeUser = uService.getAllActiveUsers(); for (User user : activeUser) { if (StringUtils.isNotBlank(user.getWritePath()) && StringUtils.isNotBlank(user.getFtpPath())) { System.out.println(".........name........"+user.getName()); // 修复原代码未定义的distributor变量 } } System.out.println(".........Method Ends........"); }
4. 检查包扫描范围
确保@EntityScan包含所有实体类所在的包,若实体类存在于com.project.core下,补充包路径:
@EntityScan(basePackages = {"com.fc.backend", "com.project.core"})
内容的提问来源于stack exchange,提问作者Charmi Chavda
相关产品推荐
相关产品推荐

