Java @组件扫描无法跨jar和war在Spring引导中使用@Component
我在spring boot应用程序中有一个公共jar文件Java @组件扫描无法跨jar和war在Spring引导中使用@Component,java,spring,maven,spring-boot,war,Java,Spring,Maven,Spring Boot,War,我在spring boot应用程序中有一个公共jar文件 package com.acc.api; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframewo
package com.acc.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
@ComponentScan(basePackages = {"com.acc.api","service"})
public class ApicoreApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(ApicoreApplication.class, args);
for (String name: applicationContext.getBeanDefinitionNames()) {
System.out.println(name);
}
}
}
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.acc.api</groupId>
<artifactId>maintenance</artifactId>
<packaging>war</packaging>
<parent>
<groupId>com.acc.api</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>com.acc.api</groupId>
<artifactId>apicore</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-util</artifactId>
<version>8.5.23</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<finalName>maintenance</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy war</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory></outputDirectory>
<filtering>true</filtering>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<includes>
<include>${project.build.finalName}.war</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
其中的组件是
package com.acc.api.core.utils;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AccUtil {
@Autowired
private WebServiceEngine engine;
}
其中WebServiceEngine是第三方api i cam调用
我的pom参赛作品是这样的
<?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 http://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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.acc.api</groupId>
<artifactId>apicore</artifactId>
<version>0.0.1</version>
<name>apicore</name>
<description>apicore</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-util</artifactId>
<version>8.5.23</version>
</dependency>
<dependency>
<groupId>WebServiceEngine</groupId>
<artifactId>WebServiceEngine</artifactId>
<version>7.10.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.acc.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class MaintenanceApplication {
public static void main(String[] args) {
SpringApplication.run(MaintenanceApplication.class, args);
}
}
package com.acc.api;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MaintenanceApplication.class);
}
}
我在MaintenanceApplication
war文件中使用上述jar的实际控制器是
package com.acc.api.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.acc.api.core.utils.AccUtil;
@Import({AccUtil.class})
@RestController
@RequestMapping("/STATUS")
public class StatusController {
private static Logger logger = LoggerFactory.getLogger(StatusController.class);
@Autowired
private AccUtil accUtil;
@Autowired
private ObjectMapper objMapper;
@RequestMapping(method=RequestMethod.GET, value="/fetchStatus")
public String getStatus(@PathVariable String processId){
String status = "";
try {
status = accUtil.getStats();
return status;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
IDE没有显示任何错误,但我在执行mvn clean install
时遇到了以下生成错误
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project maintenance: Compilation failure: Compilation failure:
[ERROR] /src/main/java/com/acc/hpi/controller/MSRController.java:[13,30] package com.acc.api.core.utils does not exist
[ERROR] symbol: class AccUtils
在这方面有人能帮我吗
提前感谢。使用Spring Boot时,
Spring Boot maven插件将重新打包并重新构造生成的jar文件。此jar文件不能作为依赖项在其他应用程序中使用。相反,您需要指示SpringBootMaven插件重新打包jar并保留原始工件(并将两者发布到您的maven存储库)
《Spring引导参考指南》对如何实现这一点有详细的说明
如果您的jar只是一个依赖项,本身不可运行/可执行,那么只需删除springbootmaven插件
,并生成一个常规jar即可 使用Spring Boot时,Spring Boot maven插件将重新打包并重新构造生成的jar文件。此jar文件不能作为依赖项在其他应用程序中使用。相反,您需要指示SpringBootMaven插件重新打包jar并保留原始工件(并将两者发布到您的maven存储库)
《Spring引导参考指南》对如何实现这一点有详细的说明
如果您的jar只是一个依赖项,本身不可运行/可执行,那么只需删除springbootmaven插件
,并生成一个常规jar即可 您已经在StatusController
class中导入了com.acc.api.core.utils.acutil
class两次:
import com.acc.api.core.utils.AccUtil;
@Import({AccUtil.class})
请删除其中一个,然后重试。您已在StatusController
类中导入com.acc.api.core.utils.acutil
class两次
import com.acc.api.core.utils.AccUtil;
@Import({AccUtil.class})
删除其中一个,然后重试。您的AccUtils
位于错误的目录中。@chrylis是否要我将其移动到jar中的com.acc.api本身?重命名文件夹(包)当前为com/hcc/hpi/将其更改为com/acc/api@chrylis不,不,它的打字错误只出现在com/acc/api中,alo我尝试将AccUtil和springboot文件放在同一个位置com/acc/api,但仍然没有luckYou不能在另一个位置的Spring Boot生成的jar中使用,因为它的打包/结构不同。您不希望您的jar由Spring Boot生成,而是希望使用常规jar。您的AccUtils
位于错误的目录中。@chrylis是否希望我将其移动到jar中的com.acc.api本身?重命名文件夹(包)当前为com/hcc/hpi/将其更改为com/acc/api@chrylis不,不,它的打字错误只出现在com/acc/api中,alo我尝试将AccUtil和springboot文件放在同一个位置com/acc/api,但仍然没有luckYou不能在另一个位置的Spring Boot生成的jar中使用,因为它的打包/结构不同。您不希望您的jar由springboot生成,而是希望使用常规jar。