SpringBoot.Making our first starter and autoconfiguration

STARTER

let’s create some new module and infra package in it

package infra;

public class SomeInfraBean {
    public void sayHello() {
        System.out.println("hello from infra bean");
    }
}
package infra;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SomeInfraConfiguration {
    @Bean
    public SomeInfraBean someInfraBean() {
        return new SomeInfraBean();
    }
}
plugins {
    id("java")
    id("org.springframework.boot") version "3.2.4"
    id("io.spring.dependency-management") version "1.1.4"
}

group = "org.example"
version = "unspecified"

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    compileOnly("org.projectlombok:lombok")
    annotationProcessor("org.projectlombok:lombok")
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
}

//val jar: Jar by tasks
//val bootJar: BootJar by tasks
//
//bootJar.enabled = false
//jar.enabled = true
//
//
//tasks.getByName<BootJar>("bootJar") {
//    enabled = false
//}
//
//tasks.getByName<Jar>("jar") {
//    enabled = true
//}

MAIN APP

in main app we can call and use this bean like this

import infra.SomeInfraBean;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SomeService {

    @Autowired
    private SomeInfraBean someInfraBean;

    @PostConstruct
    public void testInfrabean() {
        someInfraBean.sayHello();
    }


    @Value("${mymessage}")
    private void init(String msg) {
        System.out.println(msg);
    }
}

This entry was posted in Без рубрики. Bookmark the permalink.