2017-06-13 40 views
0

我创建了一个新的Spring Boot项目,以了解如何在外部jar文件中声明bean。我创建了一个名为ContextApplication的文件,我所要做的就是显示Spring已扫描的所有bean。请注意,此过程中没有涉及单个xml文件。我喜欢这样,宁愿保持这种方式。但是,如果我们必须这样做,我会尝试几乎所有的事情。将bean包含到应用程序上下文中

ContextApplication:

package com.anypackage.jedcontext; 

import java.util.Arrays; 

import org.springframework.boot.CommandLineRunner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 

@SpringBootApplication 
@ComponentScan(basePackages = {"com.tacticalenterprisesltd"}) 
public class JedcontextApplication { 

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

    @Bean 
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) { 
     return args -> { 

      System.out.println("Let's inspect the beans provided by Spring Boot:"); 

      String[] beanNames = ctx.getBeanDefinitionNames(); 
      Arrays.sort(beanNames); 
      for (String beanName : beanNames) { 
       System.out.println(beanName); 
      } 

     }; 
    } 
} 

当我运行这一点,但是肯定和工作提供已知Bean的列表,作为项目的一部分,我引用的jar文件JED-1.6.jar这其中的其他类标记为@Component或@Service。

enter image description here

的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.anypackage</groupId> 
    <artifactId>jedcontext</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>jedcontext</name> 
    <description>An application context test</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.4.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </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-jdbc</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.tacticalenterprisesltd</groupId> 
      <artifactId>jed</artifactId> 
      <version>1.6</version> 
      <systemPath>C:\Users\Owner\Documents\workspace-sts-3.8.4.RELEASE\JED\jed-1.6.jar</systemPath> 
      <scope>system</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 


</project> 

现在,当我运行应用程序,在JAR文件中的标记的类不被扫描并没有显示在控制台的列表了。即使我明确提供了这个注释:@ComponentScan(basePackages = {“com.tacticalenterprisesltd”})通过ContextApplication类来扫描jar文件中的所有内容。这将包括我希望的任何子包。因此,这里最基本的问题是,“为什么Spring不是在jar文件的类中读取的,我该如何让它读取?”请指教。

+0

你的'jed.jar'应该是一个mave不依赖任何添加到类路径。这个jar也是一个普通的jar或用Spring Boot创建的另一个jar。 –

+0

jed-1.6.jar是我创建的库。我试图在“Maven Dependancies”下添加jar文件,如上图所示,但不能。每次我选择Add Jars或Add External Jars时,jar文件都会进入列表顶部,而不是在Maven Dependancies下。 – Alan

+0

当然不会。该jar必须存在于你的maven仓库中,并作为''添加到'pom.xml'中。 –

回答

1

您应该使用includeSystemScope参数,包括您的罐子到SpringBoot应用

<plugin> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-maven-plugin</artifactId> 
    <configuration> 
    <includeSystemScope>true</includeSystemScope> 
    </configuration> 
</plugin> 

或作为另一个选项,您可以安装您的罐子到本地Maven仓库

mvn install:install-file -Dfile=path/to/jed-1.6.jar -DgroupId=com.tacticalenterprisesltd -DartifactId=jed -Dversion=1.6 -Dpackaging=jar 

然后使用类路径它作为一个标准的maven依赖项

<dependency> 
     <groupId>com.tacticalenterprisesltd</groupId> 
     <artifactId>jed</artifactId> 
     <version>1.6</version> 
    </dependency> 
+0

我看到任何人可能会被包命名困惑,所以我用一个完全不同的包名称重复项目,以避免混淆Spring。注意上面的编辑。当我运行该应用程序时,Spring仍然不读取jar文件jed-1.6.jar中的文件。所以我们回到了原点。 – Alan

+0

@Alan我已经更新了答案 –

相关问题