惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
腾讯CDC
aimingoo的专栏
aimingoo的专栏
月光博客
月光博客
Engineering at Meta
Engineering at Meta
C
Check Point Blog
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
WordPress大学
WordPress大学
博客园 - 司徒正美

Maciej Walkowiak - Java & Spring

Blog Generating HTTP clients in Spring Boot application from OpenAPI spec PostgreSQL and UUID as primary key Dynamic Projections with Spring Data JPA Container logs with Spring Boot and Testcontainers Reified Generics in Java? Faster integration tests with reusable Testcontainers and Flyway Running one-time jobs with Quartz and Spring Boot The best way to use Testcontainers with Spring Boot Spring Boot & Flyway - clear database between integration tests What's new in Spring? Activate Maven Profile by Operating System Spring Boot with Thymeleaf and Tailwind CSS - Complete Guide How to publish a Java library to Maven Central - Complete Guide Docker Compose - waiting until containers are ready Single file Java applications with JBang Beautiful bash scripts with Gum Running Java on CRaC How to log PostgreSQL queries with Testcontainers Spring Boot 3.0 & GraalVM Native Image - not a free lunch Creating Spring Cloud Function projects with AWS SAM Loading classpath resources to String with a custom JUnit extension Creating Project Templates with Cookiecutter Auto-Registering JUnit 5 extensions Listing Maven dependencies in Spring Boot Actuator Info endpoint Spring Cloud AWS 2.3 RC2 Released How I built vlad-cli - command line interface to Vlad Mihalcea The State of Java Relational Persistence On Choosing a Tech Stack
Spring Boot component scanning without annotations
2022-07-27 · via Maciej Walkowiak - Java & Spring
Published on
  • Spring Boot

Spring Boot has an interesting capability that I haven't discovered till very recently when going through Spring Cloud Function project source code - it can scan for components - meaning go through classpath to find beans - even for classes that are not annotated with any of the Spring stereotype annotations like @Component, @Service, @Repository, @Controller or @Configuration.

@ComponentScan annotation - that can be placed on any @Configuration class (including the main @SpringBootApplication annotated class) - can be configured with a filter that instead of looking for typical Spring annotations, looks for specific Java types.

In the following example, Spring will scan for components in package functions that implement java.util.Function interface:

java

@SpringBootApplication
@ComponentScan(
        basePackages = "functions",
        includeFilters = @ComponentScan.Filter(
                type = FilterType.ASSIGNABLE_TYPE,
                classes = Function.class
        )
)
public class MyApp {

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

Such configuration make it possible to scan for classes from a Spring unaware 3rd party jar without defining beans with @Bean annotation. Or it can save you from defining @Beans in configuration classes in case you want to keep your beans free from Spring classes or annotations.

Spring Cloud Function gives you exactly this option - you can define classes of type Function, Supplier or Consumer in a functions package, without annotating them with Spring annotations, and they will be registered as beans in the container.

While it is not useful in the great majority of regular applications I find it quite interesting and potentially useful when developing something more advanced or less typical than regular Spring Boot application.

Let's stay in touch and follow me on Twitter: @maciejwalkowiak

Subscribe to RSS feed