Search code examples
javaspringspring-bootmavenjar

Autowire bean with meta-inf


I have my own jar file with a spring component. I am trying to autowire this bean in another project, but I can't do it without @Import or @ComponentScan. is it possible to build jar and use beans from it in another project without additional annotation? Example what I need:

@Configuration
public class Config {
    @Bean
    public String init(@Autowired DistributorMessageSender distributorMessageSender) {
        WebSocketMessage webSocketMessage = WebSocketMessage.builder().build();
        distributorMessageSender.send(Channel.WEBSOCKET, webSocketMessage);
        return new String();
    }
}

What I want to avoid:

scanBasePackages = {"app", "distributor.client"}

Solution

  • The solution that I find is: build my own auto-configuration class. It allows you to autowire bean from jar without any import annotation. A guide that I follow: https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/html/boot-features-developing-auto-configuration.html#boot-features-locating-auto-configuration-candidates