0

Require new Spring Boot Project.

Need to include Jar of existing Spring (Non Boot) project as it has a bean which we need to use (much code is there written so can't move it to boot)

@Service("ctdbservice")

I have included dependency of existing jar in my spring boot.

Following is pom of existing spring non boot project that existing project:

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.hcccsc.aom2</groupId>
    <artifactId>am2-parent</artifactId>
    <version>BC20.10-SNAPSHOT</version>
    <relativePath>../am2-parent</relativePath>
  </parent>
  <artifactId>am2-read-write-cts3-impl</artifactId>
  <packaging>jar</packaging>

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    <defaultGoal>clean install</defaultGoal>
  </build>

  <dependencies>

    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>am2-common</artifactId>
      <version>${project.version}</version>
    </dependency>
    
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>am2-reference-impl</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>am2-model</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>am2-cts3-model</artifactId>
      <version>${project.version}</version>
      <exclusions>
        <exclusion>
          <artifactId>am2-cts3-data-model</artifactId>
          <groupId>${project.groupId}</groupId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>am2-read-api</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>am2-write-api</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>am2-read-cts2-impl</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>am2-search-api</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>am2-search-cts3-impl</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.javers</groupId>
      <artifactId>javers-core</artifactId>
    </dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
</dependency>

I have included above in my Spring Boot Maven. But Spring boot is not initiating beans of this JAR.

Can Spring Boot load old jars properly in its context? Will pom file in old jar not impact as it has reference to some parent has spring versions as well as jars using same? or will those jars use spring boot added dependencies only?

Can anyone suggest where am i doing it wrong?

fatherazrael
  • 5,511
  • 16
  • 71
  • 155
  • Does this answer your question? [How can I @Autowire a spring bean that was created from an external jar?](https://stackoverflow.com/questions/29571304/how-can-i-autowire-a-spring-bean-that-was-created-from-an-external-jar) – Milgo Aug 31 '20 at 07:20

2 Answers2

0

So I guess @Autowired does not work correctly, right?

You need a component scan for the packages of your JAR, like:

@Configuration
@ComponentScan("com.package.where.my.class.is")
class Config {
...
}

See How can I @Autowire a spring bean that was created from an external jar?

Milgo
  • 2,617
  • 4
  • 22
  • 37
0

There are two different classes of possible issues that can arise.

  1. Spring boot of any specific version uses very specific versions of dependencies (including spring of course). So First of all make sure that you don't have clashes of dependencies versions in the resulting artifact. If you do - it can lead to unexpected and unwanted behaviors - startup errors and so forth.

  2. Spring boot will detect the main class's package, say com.example.myapp (this is where the class annotated with @SpringBootApplication reside) and it will try to find the beans in com.example.myapp (the same package) and com.example.myapp.* (hierarchially: com.example.myapp.a.b.c will also work), but if your service is not in one of those paths it won't be recognized as a bean.

In general you can use three approaches:

Approach 1: Use @ComponentScan annotation on @Configuration class as @Milgo has already suggested in your spring boot application. It doesn't really matter where exactly this class is put, spring is a runtime framework.

Approach 2: In the module of service create a @Configuration class that will look again as and Import it in spring boot application with @Import annotation

Approach 3: Use spring.factories and again specify the configuration. The difference is that this way the class won't be found in a component scanning process, but rather be resolved during the "factories" scanning.

The effect is that it will be enough to specify a maven dependency in your spring boot app and it will "auto-magically" load your beans. You can read about this approach here

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97