简体   繁体   中英

including beans into application context

I've created a new Spring Boot project to learn how to include beans which I've declared in an external jar file. I've created a file named ContextApplication where all I want to do is display all those beans that have been scanned by Spring. Note that there isn't a single xml file involved in this process. I like that and would prefer to keep it that way. However, if we have to, I'll try just about anything at this point.

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);
            }

        };
    }
}

When I run this, it certainly works and provides a list of known beans, however, as part of the project, I've referenced a jar file jed-1.6.jar which has other classes in it marked as @Component or @Service.

在此处输入图片说明

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>

Now, when I run the application, the marked classes within the jar file are not being scanned and not showing up in the list in the console. Even though I explicitly provide this annotation: @ComponentScan(basePackages = {"com.tacticalenterprisesltd"}) over the ContextApplication class to scan everything within the jar file. This would include any sub packages I'm hoping. So, the bottom question here is, "Why isn't Spring reading in the classes within the jar file and how do I make it do so?" Please advise.

You should use includeSystemScope parameter to include your jar to classpath of the SpringBoot app

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

Or as another option you can install your jar to the local maven repository

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

And then use it as a standard maven dependency

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM