Home > Blockchain >  Unable to create Custom Spring Boot Starter/AutoConfiguration
Unable to create Custom Spring Boot Starter/AutoConfiguration

Time:02-04

Custom starter project named as: hello-service-spring-boot-start

Project Directory Structure is: hello-service-spring-boot-start directory structure

In 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 https://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>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.javadevjournal</groupId>
    <artifactId>hello-service-spring-boot-start</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-service-spring-boot-start</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


In Starter Class or HelloServiceSpringBootStartApplication.java

package com.javadevjournal.helloservicespringbootstart;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloServiceSpringBootStartApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloServiceSpringBootStartApplication.class, args);
        System.out.println("hello-service-spring-boot-start-application started...");
    }

}

In HelloService.java

package com.javadevjournal.helloservicespringbootstart.service;

public interface HelloService {
    void sayHello();
}

In HelloServiceImpl.java

package com.javadevjournal.helloservicespringbootstart.service;

public class HelloServiceImpl implements HelloService {
    @Override
    public void sayHello() {
        System.out.println("Hello from the default starter hello service");
    }
}

In HelloServiceAutoConfiguration.java

package com.javadevjournal.helloservicespringbootstart.config;

import com.javadevjournal.helloservicespringbootstart.service.HelloService;
import com.javadevjournal.helloservicespringbootstart.service.HelloServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public HelloService helloService() {
        return new HelloServiceImpl();
    }
}

In spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.javadevjournal.helloservicespringbootstart.HelloServiceSpringBootStartApplication

Then, I run mvn install to create jar file or to build the above starter.

The project in which I have used above starter named as: custom-app

Project Directory Structure is: custom-app project directory structure

In 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 https://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>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.javadevjournal</groupId>
    <artifactId>custom-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>custom-app</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.javadevjournal</groupId>
            <artifactId>hello-service-spring-boot-start</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

In Starter class or CustomAppApplication.java class

package com.javadevjournal.customapp;

import com.javadevjournal.helloservicespringbootstart.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CustomAppApplication implements CommandLineRunner {

    @Autowired
    HelloService helloService;

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

    @Override
    public void run(String... args) throws Exception {
        helloService.sayHello();
    }
}

Output: Console.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.2)

2023-02-01T21:49:48.215 05:30  INFO 11332 --- [  restartedMain] c.j.customapp.CustomAppApplication       : Starting CustomAppApplication using Java 18.0.2.1 with PID 11332 (G:\Java\IntelliJ Workspace\Learning\Spring Boot By Raghu\CustomStarterAutoConfigurationActual\custom-app\target\classes started by himan in G:\Java\IntelliJ Workspace\Learning\Spring Boot By Raghu\CustomStarterAutoConfigurationActual\hello-service-spring-boot-start)
2023-02-01T21:49:48.217 05:30  INFO 11332 --- [  restartedMain] c.j.customapp.CustomAppApplication       : No active profile set, falling back to 1 default profile: "default"
2023-02-01T21:49:48.261 05:30  INFO 11332 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-02-01T21:49:48.623 05:30  WARN 11332 --- [  restartedMain] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customAppApplication': Unsatisfied dependency expressed through field 'helloService': No qualifying bean of type 'com.javadevjournal.helloservicespringbootstart.service.HelloService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2023-02-01T21:49:48.629 05:30  INFO 11332 --- [  restartedMain] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-02-01T21:49:48.653 05:30 ERROR 11332 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field helloService in com.javadevjournal.customapp.CustomAppApplication required a bean of type 'com.javadevjournal.helloservicespringbootstart.service.HelloService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.javadevjournal.helloservicespringbootstart.service.HelloService' in your configuration.


Process finished with exit code 0

Screenshot. output: console

I am trying to create custom starter project in spring boot and use it in another. But I am getting Error which explains that the bean that has been created inside custom-starter is not initialized. I am expecting to print hello message of starter in the console.

CodePudding user response:

spring-boot-3 no longer uses spring.factories to register autoconfiguration classes.

Create a file named org.springframework.boot.autoconfigure.AutoConfiguration.imports in folder META-INF/spring where you can put (all) your Configuration classes.

In your case this would be

/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

com.javadevjournal.helloservicespringbootstart.config.HelloServiceAutoConfiguration

Note this should not be a SpringBootApplication

Reference: Creating Your Own Auto-configuration

  • Related